1. 程式人生 > >Unity讀取只讀和可寫目錄的方法

Unity讀取只讀和可寫目錄的方法

使用Unity時,讀取程式包內部資源的時候,如果使用了www的方法,需要注意在手機平臺上的字首轉換.

以下的程式碼可以獲取程式包的目錄

	public static string AppContentPath() {
		string path = string.Empty;
		switch (Application.platform) {
		case RuntimePlatform.Android:
			path = "jar:file://" + Application.dataPath + "!/assets/";
			break;
		case RuntimePlatform.IPhonePlayer:
			path = "file://" + Application.dataPath + "/Raw/";
			break;
		default:
			path = "file://" + Application.dataPath + "/StreamingAssets/";
			break;
		}
		return path;
	}

一下程式碼獲取可寫的目錄
	public static string DataPath {
		get {
			string game = SimpleFramework.AppConst.AppName.ToLower();
			if (Application.platform == RuntimePlatform.IPhonePlayer || 
			    Application.platform == RuntimePlatform.Android ||
			    Application.platform == RuntimePlatform.WP8Player) 
			{
				return Application.persistentDataPath + "/" + game + "/"; //"/sdcard/" + game + "/";
			} 
			//Test for lua update in the editor env, after debug, it should be if (Const.SimulateMode)
			if (SimpleFramework.AppConst.SimulationMode) 
			{
				string target = string.Empty;
				target = game;
				return Application.dataPath + "/../SimulatorDataPath/" + target + "/";
			}
			
			return "c:/" + game + "/";
		}