Skip to content

Commit 37a6f67

Browse files
committed
#fixed 热更新界面在android平台从apk内读取时路径错误
1 parent 5ccc917 commit 37a6f67

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

KSFramework/Assets/KSFramework/KEngine/KEngine/CoreModules/ResourceModule/KAssetBundleLoader.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,14 @@ private IEnumerator LoadAssetBundle(string relativeUrl)
176176
#endif
177177
if (AppConfig.IsLogAbLoadCost) beginTime = Time.realtimeSinceStartup;
178178

179-
string _fullUrl;
180-
var pathType = KResourceModule.GetResourceFullPath(KResourceModule.BundlesPathRelative + relativeUrl, false, out _fullUrl);
179+
string _fullUrl = KResourceModule.GetAbFullPath(relativeUrl);
181180

182-
if (pathType == KResourceModule.GetResourceFullPathType.Invalid)
181+
if (string.IsNullOrEmpty(_fullUrl))
183182
{
184183
OnFinish(null);
185184
yield break;
186185
}
187-
if(Application.platform == RuntimePlatform.Android && pathType == KResourceModule.GetResourceFullPathType.InApp)
188-
{
189-
_fullUrl = "jar:file://" + _fullUrl;
190-
}
191-
186+
192187
AssetBundle assetBundle = null;
193188
if (_loaderMode == LoaderMode.Sync)
194189
{

KSFramework/Assets/KSFramework/KEngine/KEngine/CoreModules/ResourceModule/KResourceModule.cs

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ static void InitResourcePath()
119119
case RuntimePlatform.OSXEditor:
120120
case RuntimePlatform.LinuxEditor:
121121
{
122-
ApplicationPath = fileProtocol + editorProductPath;
123122
if (AppConfig.ReadStreamFromEditor)
124123
{
125124
AppBasePath = Application.streamingAssetsPath + "/";
@@ -136,24 +135,21 @@ static void InitResourcePath()
136135
case RuntimePlatform.OSXPlayer:
137136
{
138137
string path = Application.streamingAssetsPath.Replace('\\', '/');
139-
ApplicationPath = fileProtocol + Application.dataPath;
140138
AppBasePath = path + "/";
141139
AppBasePathWithProtocol = fileProtocol + AppBasePath;
142140
}
143141
break;
144142
case RuntimePlatform.Android:
145143
{
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;
151148
}
152149
break;
153150
case RuntimePlatform.IPhonePlayer:
154151
{
155152
// MacOSX下,带空格的文件夹,空格字符需要转义成%20
156-
ApplicationPath = System.Uri.EscapeUriString(fileProtocol + Application.streamingAssetsPath);
157153
// only iPhone need to Escape the fucking Url!!! other platform works without it!!!
158154
AppBasePath = System.Uri.EscapeUriString(Application.dataPath + "/Raw");
159155
AppBasePathWithProtocol = fileProtocol + AppBasePath;
@@ -229,11 +225,6 @@ public static string AppDataPath
229225
/// </summary>
230226
public static string BundlesPathRelative { get; private set; }
231227

232-
/// <summary>
233-
/// Application.dataPath
234-
/// </summary>
235-
public static string ApplicationPath { get; private set; }
236-
237228
/// <summary>
238229
/// On Windows, file protocol has a strange rule that has one more slash
239230
/// </summary>
@@ -271,14 +262,23 @@ public static string EditorAssetBundleFullPath
271262
#endregion
272263

273264
/// <summary>
274-
/// 获取ab文件的完整路径,默认会加上ab文件的后缀
265+
/// 获取ab文件的完整路径,做的处理:会加上ab格式的后缀,如果是在apk包体内则会加上jar:file://前缀
275266
/// </summary>
276-
/// <param name="path"></param>
267+
/// <param name="path">相对路径</param>
277268
/// <returns></returns>
278269
public static string GetAbFullPath(string path)
279270
{
280271
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;
282282
}
283283

284284
/// <summary>
@@ -310,7 +310,7 @@ public static string GetResourceFullPath(string url, bool withFileProtocol = fal
310310
}
311311

312312
/// <summary>
313-
/// 根据相对路径,获取到完整路径
313+
/// 根据相对路径,获取到完整路径,優先从下載資源目录找,没有就读本地資源目錄
314314
/// 根路径:Product
315315
/// </summary>
316316
/// <param name="url">相对路径</param>
@@ -326,19 +326,17 @@ public static GetResourceFullPathType GetResourceFullPath(string url, bool withF
326326
fullPath = null;
327327
return GetResourceFullPathType.Invalid;
328328
}
329-
330329
string docUrl;
331330
bool hasDocUrl = TryGetAppDataUrl(url, withFileProtocol, out docUrl);
332331
if (hasDocUrl)
333332
{
334333
fullPath = docUrl;
335334
return GetResourceFullPathType.InDocument;
336335
}
337-
338-
// 優先下載資源,但又沒有下載資源文件!使用本地資源目錄
336+
339337
string inAppUrl;
340338
bool hasInAppUrl = TryGetInAppStreamingUrl(url, withFileProtocol, out inAppUrl);
341-
if (!hasInAppUrl) // 连本地资源都没有,直接失败吧 ?? 沒有本地資源但又遠程資源?竟然!!?
339+
if (!hasInAppUrl) // 连本地资源都没有,直接失败吧 ??
342340
{
343341
if (raiseError) Log.Error($"[Not Found] StreamingAssetsPath Url Resource: {url} ,fullPath:{inAppUrl}");
344342
fullPath = null;
@@ -349,7 +347,24 @@ public static GetResourceFullPathType GetResourceFullPath(string url, bool withF
349347

350348
return GetResourceFullPathType.InApp;
351349
}
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+
}
352362

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+
353368
/// <summary>
354369
/// use AssetDatabase.LoadAssetAtPath insead of load asset bundle, editor only
355370
/// </summary>
@@ -381,21 +396,21 @@ public static bool TryGetAppDataUrl(string url, bool withFileProtocol, out strin
381396
public static bool TryGetInAppStreamingUrl(string url, bool withFileProtocol, out string newUrl)
382397
{
383398
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)
390401
{
391-
// Editor, 非android运行,直接进行文件检查
402+
// Editor进行文件检查
392403
if (!File.Exists(Path.GetFullPath(AppBasePath + url)))
393404
{
394405
return false;
395406
}
396407
}
408+
else if(Application.platform == RuntimePlatform.Android) // 注意,StreamingAssetsPath在Android平台時,壓縮在apk里面,不要使用File做文件檢查
409+
{
410+
return KEngineAndroidPlugin.IsAssetExists(url);
411+
}
397412

398-
// Windows/Edtiro平台下,进行大小敏感判断
413+
// Windows/Editor平台下,进行大小敏感判断
399414
if (Application.isEditor)
400415
{
401416
var result = FileExistsWithDifferentCase(AppBasePath + url);

0 commit comments

Comments
 (0)