@@ -119,7 +119,6 @@ static void InitResourcePath()
119
119
case RuntimePlatform . OSXEditor :
120
120
case RuntimePlatform . LinuxEditor :
121
121
{
122
- ApplicationPath = fileProtocol + editorProductPath ;
123
122
if ( AppConfig . ReadStreamFromEditor )
124
123
{
125
124
AppBasePath = Application . streamingAssetsPath + "/" ;
@@ -136,24 +135,21 @@ static void InitResourcePath()
136
135
case RuntimePlatform . OSXPlayer :
137
136
{
138
137
string path = Application . streamingAssetsPath . Replace ( '\\ ' , '/' ) ;
139
- ApplicationPath = fileProtocol + Application . dataPath ;
140
138
AppBasePath = path + "/" ;
141
139
AppBasePathWithProtocol = fileProtocol + AppBasePath ;
142
140
}
143
141
break ;
144
142
case RuntimePlatform . Android :
145
143
{
146
- //https://docs.unity3d.com/Manual/StreamingAssets.html
147
- ApplicationPath = string . Concat ( "jar:" , fileProtocol , Application . dataPath , "!/assets" ) ;
148
- AppBasePathWithProtocol = ApplicationPath + "/" ;
149
- // 注意,StramingAsset在Android平台中,是在壓縮的apk里,无法通过File读取
150
- AppBasePath = "jar:file://" + Application . dataPath + "!/assets/" ;
144
+ //文档:https://docs.unity3d.com/Manual/StreamingAssets.html
145
+ //注意,StramingAsset在Android平台是在apk中,无法通过File读取请使用LoadAssetsSync,如果要同步读取ab请使用GetAbFullPath
146
+ AppBasePath = Application . dataPath + "!/assets/" ;
147
+ AppBasePathWithProtocol = fileProtocol + AppBasePath ;
151
148
}
152
149
break ;
153
150
case RuntimePlatform . IPhonePlayer :
154
151
{
155
152
// MacOSX下,带空格的文件夹,空格字符需要转义成%20
156
- ApplicationPath = System . Uri . EscapeUriString ( fileProtocol + Application . streamingAssetsPath ) ;
157
153
// only iPhone need to Escape the fucking Url!!! other platform works without it!!!
158
154
AppBasePath = System . Uri . EscapeUriString ( Application . dataPath + "/Raw" ) ;
159
155
AppBasePathWithProtocol = fileProtocol + AppBasePath ;
@@ -229,11 +225,6 @@ public static string AppDataPath
229
225
/// </summary>
230
226
public static string BundlesPathRelative { get ; private set ; }
231
227
232
- /// <summary>
233
- /// Application.dataPath
234
- /// </summary>
235
- public static string ApplicationPath { get ; private set ; }
236
-
237
228
/// <summary>
238
229
/// On Windows, file protocol has a strange rule that has one more slash
239
230
/// </summary>
@@ -271,14 +262,23 @@ public static string EditorAssetBundleFullPath
271
262
#endregion
272
263
273
264
/// <summary>
274
- /// 获取ab文件的完整路径,默认会加上ab文件的后缀
265
+ /// 获取ab文件的完整路径,做的处理:会加上ab格式的后缀,如果是在apk包体内则会加上jar:file://前缀
275
266
/// </summary>
276
- /// <param name="path"></param>
267
+ /// <param name="path">相对路径 </param>
277
268
/// <returns></returns>
278
269
public static string GetAbFullPath ( string path )
279
270
{
280
271
if ( ! path . EndsWith ( AppConfig . AssetBundleExt ) ) path = path + AppConfig . AssetBundleExt ;
281
- return GetResourceFullPath ( BundlesPathRelative + path , false ) ;
272
+ var _fullUrl = GetResourceFullPath ( BundlesPathRelative + path , false ) ;
273
+ if ( ! string . IsNullOrEmpty ( _fullUrl ) )
274
+ {
275
+ if ( Application . platform == RuntimePlatform . Android && _fullUrl . StartsWith ( "/data/app" ) )
276
+ {
277
+ return "jar:file://" + _fullUrl ; //如果apk内则添加前缀,经测试unity2019.3.7f1+android6.0加在Appbase无效
278
+ }
279
+ }
280
+
281
+ return _fullUrl ;
282
282
}
283
283
284
284
/// <summary>
@@ -310,7 +310,7 @@ public static string GetResourceFullPath(string url, bool withFileProtocol = fal
310
310
}
311
311
312
312
/// <summary>
313
- /// 根据相对路径,获取到完整路径
313
+ /// 根据相对路径,获取到完整路径,優先从下載資源目录找,没有就读本地資源目錄
314
314
/// 根路径:Product
315
315
/// </summary>
316
316
/// <param name="url">相对路径</param>
@@ -326,19 +326,17 @@ public static GetResourceFullPathType GetResourceFullPath(string url, bool withF
326
326
fullPath = null ;
327
327
return GetResourceFullPathType . Invalid ;
328
328
}
329
-
330
329
string docUrl ;
331
330
bool hasDocUrl = TryGetAppDataUrl ( url , withFileProtocol , out docUrl ) ;
332
331
if ( hasDocUrl )
333
332
{
334
333
fullPath = docUrl ;
335
334
return GetResourceFullPathType . InDocument ;
336
335
}
337
-
338
- // 優先下載資源,但又沒有下載資源文件!使用本地資源目錄
336
+
339
337
string inAppUrl ;
340
338
bool hasInAppUrl = TryGetInAppStreamingUrl ( url , withFileProtocol , out inAppUrl ) ;
341
- if ( ! hasInAppUrl ) // 连本地资源都没有,直接失败吧 ?? 沒有本地資源但又遠程資源?竟然!!?
339
+ if ( ! hasInAppUrl ) // 连本地资源都没有,直接失败吧 ??
342
340
{
343
341
if ( raiseError ) Log . Error ( $ "[Not Found] StreamingAssetsPath Url Resource: { url } ,fullPath:{ inAppUrl } ") ;
344
342
fullPath = null ;
@@ -349,7 +347,24 @@ public static GetResourceFullPathType GetResourceFullPath(string url, bool withF
349
347
350
348
return GetResourceFullPathType . InApp ;
351
349
}
350
+
351
+ /// <summary>
352
+ /// 获取一个资源的完整路径是在apk压缩包内还是在可读写路径内
353
+ /// </summary>
354
+ /// <param name="fullPath"></param>
355
+ public static GetResourceFullPathType GetResFullPathType ( string fullPath )
356
+ {
357
+ if ( string . IsNullOrEmpty ( fullPath ) )
358
+ {
359
+ Log . Error ( "无法识别一个空的资源路径!" ) ;
360
+ return GetResourceFullPathType . Invalid ;
361
+ }
352
362
363
+ if ( Application . platform == RuntimePlatform . Android )
364
+ return fullPath . StartsWith ( "/data/app" ) ? GetResourceFullPathType . InApp : GetResourceFullPathType . InDocument ;
365
+ return fullPath . StartsWith ( AppDataPath ) ? GetResourceFullPathType . InApp : GetResourceFullPathType . InDocument ;
366
+ }
367
+
353
368
/// <summary>
354
369
/// use AssetDatabase.LoadAssetAtPath insead of load asset bundle, editor only
355
370
/// </summary>
@@ -381,21 +396,21 @@ public static bool TryGetAppDataUrl(string url, bool withFileProtocol, out strin
381
396
public static bool TryGetInAppStreamingUrl ( string url , bool withFileProtocol , out string newUrl )
382
397
{
383
398
newUrl = Path . GetFullPath ( ( withFileProtocol ? AppBasePathWithProtocol : AppBasePath ) + url ) ;
384
- // 注意,StreamingAssetsPath在Android平台時,壓縮在apk里面,不要使用File做文件檢查
385
- if ( ! Application . isEditor && Application . platform == RuntimePlatform . Android )
386
- {
387
- return KEngineAndroidPlugin . IsAssetExists ( url ) ;
388
- }
389
- else if ( Application . isEditor )
399
+
400
+ if ( Application . isEditor )
390
401
{
391
- // Editor, 非android运行,直接进行文件检查
402
+ // Editor进行文件检查
392
403
if ( ! File . Exists ( Path . GetFullPath ( AppBasePath + url ) ) )
393
404
{
394
405
return false ;
395
406
}
396
407
}
408
+ else if ( Application . platform == RuntimePlatform . Android ) // 注意,StreamingAssetsPath在Android平台時,壓縮在apk里面,不要使用File做文件檢查
409
+ {
410
+ return KEngineAndroidPlugin . IsAssetExists ( url ) ;
411
+ }
397
412
398
- // Windows/Edtiro平台下 ,进行大小敏感判断
413
+ // Windows/Editor平台下 ,进行大小敏感判断
399
414
if ( Application . isEditor )
400
415
{
401
416
var result = FileExistsWithDifferentCase ( AppBasePath + url ) ;
0 commit comments