Unity平台路径工具类

最近整理出一个Unity平台路径的工具类,使用的时候直接调用该类的方法就好,不需要在你的代码中加各种判断了,也不需要根据平台加”file:///“了,总之根据自己的需求扩展吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/** 
*FileName: AppPlatform.cs
*Author: Sorpcboy
*Description: 获取平台路径
*USE: 游戏开始时调用Initialize()进行初始化。平台判断:AppPlatform.PlatformCurrent == Platform.Android 获得该平台AB包路径直接调用GetRuntimeAssetBundleUrl()方法即可
*/
using UnityEngine;
using System.Collections;

public enum Platform
{
OSXEditor = 0,
OSXPlayer,
WindowsPlayer,
WindowsEditor,
IPhonePlayer,
Android,
Unkown,
}

public static class AppPlatform
{
public static string[] PlatformPathPrefixs =
{
"file:///", //OSXEditor
"file:///", //OSXPlayer
"file:///", //WindowsPlayer
"file:///", //WindowsEditor
"file://", //IphonePlayer
"file:///", //Android
};

public static string[] PlatformNames =
{
"Windows",
"OSX",
"Windows",
"Windows",
"IOS",
"Android"
};

public static bool[] PlatformIsEditor =
{
true,
false,
false,
true,
false,
false
};

public static Platform PlatformCurrent { set; get; }

//运行时资源路径,解包后
public static string RuntimeAssetsPath
{
get
{
//移动平台走沙盒
if (Application.isMobilePlatform)
{
return Application.persistentDataPath + "/";
}

if (Global.IsSandboxMode)
{
return Application.dataPath + "/../";

}
return Application.streamingAssetsPath + "/";
}
}

public static string GetRuntimePackagePath()
{
return RuntimeAssetsPath + GetPackageName() + "/";
}

/// <summary>
/// 运行平台
/// </summary>
/// <returns></returns>
public static string GetRuntimeModelPackgePath()
{
return RuntimeAssetsPath + GetModelPackageName() + "/";
}

public static string GetPackagePath()
{
return Application.streamingAssetsPath + "/" + GetPackageName() + "/";
}

public static string GetPackageName()
{
int index = (int)AppPlatform.PlatformCurrent;
return "Package_" + AppPlatform.PlatformNames[index];
}

/// <summary>
/// 运行平台
/// </summary>
/// <returns></returns>
public static string GetModelPackageName()
{
int index = (int)AppPlatform.PlatformCurrent;
return AppPlatform.PlatformNames[index];
}

public static string PlatformCurrentName
{
get { return PlatformNames[(int)AppPlatform.PlatformCurrent]; }
}

public static string GetAssetBundleDirName()
{
return "AssetBundles";
}

public static string GetAssetBundleDirModelName()
{
return "ModelBundles"; //Windows
}

public static string GetRuntimeChecklist
{
get
{
int index = (int)AppPlatform.PlatformCurrent;
return AppPlatform.PlatformPathPrefixs[index] + GetStreamingAssetsPath;
}
}

public static string GetRuntimeAssetBundleUrl()
{
int index = (int)AppPlatform.PlatformCurrent;
return AppPlatform.PlatformPathPrefixs[index] + RuntimeAssetsPath + GetPackageName() + "/" + GetAssetBundleDirName() + "/";
}

public static string GetServerAssetBundleUrl()
{
int index = (int)AppPlatform.PlatformCurrent;
return AppPlatform.PlatformNames[index].ToLower() + "/";
}

public static void Initialize()
{
AppPlatform.PlatformCurrent = RuntimePlatform_To_AppPlaform(Application.platform);
}

public static string GetImgAndViedoPath()
{
if (Application.platform == RuntimePlatform.Android)
{
return "/sdcard/DCIM/Camera/";
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
return "";
}

return Application.streamingAssetsPath + "/";
}

private static Platform RuntimePlatform_To_AppPlaform(RuntimePlatform runtimePlatform)
{
switch (runtimePlatform)
{
case UnityEngine.RuntimePlatform.Android: return Platform.Android;
case UnityEngine.RuntimePlatform.IPhonePlayer: return Platform.IPhonePlayer;
case UnityEngine.RuntimePlatform.OSXEditor: return Platform.OSXEditor;
case UnityEngine.RuntimePlatform.OSXPlayer: return Platform.OSXPlayer;
case UnityEngine.RuntimePlatform.WindowsEditor: return Platform.WindowsEditor;
case UnityEngine.RuntimePlatform.WindowsPlayer: return Platform.WindowsPlayer;
default: return Platform.Unkown;
}
}
}