59 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using UnityEngine;
 | |
| using UnityEngine.Networking;
 | |
| 
 | |
| namespace Touka
 | |
| {
 | |
|     public class TKGFileTools
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// 通过UnityWebRequest获取本地StreamingAssets文件夹中的文件
 | |
|         ///
 | |
|         /// only for android .properties
 | |
|         /// </summary>
 | |
|         /// <param name="fileName">文件名称</param>
 | |
|         /// <returns></returns>
 | |
|         public static string UnityWebRequestFile(string fileName)
 | |
|         {
 | |
|             string url = GetFileURL(fileName);
 | |
|             TKGDebugger.LogError("Warning: If an exception occurs, check whether the 「Assets/StreamingAssets/Configs/tkg_config.properties」 file exists : " + url);
 | |
|             UnityWebRequest request = UnityWebRequest.Get(url);
 | |
|             request.SendWebRequest();//读取数据
 | |
|             while (true)
 | |
|             {
 | |
|                 if (request.downloadHandler.isDone)//是否读取完数据
 | |
|                 {
 | |
|                     return request.downloadHandler.text;
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// get file url
 | |
|         /// </summary>
 | |
|         /// <param name="fileName"></param>
 | |
|         /// <returns></returns>
 | |
|         public static string GetFileURL(string fileName)
 | |
|         {
 | |
|             string url = "";
 | |
| 
 | |
|             #region 分平台判断 StreamingAssets 路径
 | |
|             //如果在编译器或者单机中
 | |
|             #endregion
 | |
| 
 | |
| #if UNITY_EDITOR || UNITY_STANDALONE
 | |
|             url = "file://" + Application.dataPath + "/StreamingAssets/" + fileName;
 | |
|             //否则如果在Iphone下
 | |
| #elif UNITY_IPHONE
 | |
|         url = "file://" + Application.dataPath + "/Raw/"+ fileName;
 | |
|             //否则如果在android下
 | |
| #elif UNITY_ANDROID
 | |
|         url = "jar:file://" + Application.dataPath + "!/assets/"+ fileName;
 | |
| #endif
 | |
|             return url;
 | |
|         }
 | |
| 
 | |
|     }
 | |
| }
 |