From 26ffb829d02a2ee363c4b6d0d5e2b8ea26786cc0 Mon Sep 17 00:00:00 2001 From: hevinci Date: Wed, 22 Feb 2023 15:29:00 +0800 Subject: [PATCH] update samples --- .../Editor/StreamingAssetsHelperEditor.cs | 62 +++++++++++++------ .../Runtime/StreamingAssetsHelper.cs | 17 ++++- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/Editor/StreamingAssetsHelperEditor.cs b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/Editor/StreamingAssetsHelperEditor.cs index ab12c0f..ca39468 100644 --- a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/Editor/StreamingAssetsHelperEditor.cs +++ b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/Editor/StreamingAssetsHelperEditor.cs @@ -13,27 +13,51 @@ internal class AndroidPost : UnityEditor.Android.IPostGenerateGradleAndroidProje path = path.Replace("\\", "/"); string untityActivityFilePath = $"{path}/src/main/java/com/unity3d/player/UnityPlayerActivity.java"; var readContent = System.IO.File.ReadAllLines(untityActivityFilePath); - string postContent = " //auto-gen-function\n" + - " public boolean CheckAssetExist(String filePath){\n" + - " android.content.res.AssetManager assetManager = getAssets();\n" + - " java.io.InputStream inputStream = null;\n" + - " try {\n" + - " inputStream = assetManager.open(filePath);\n" + - " if(null != inputStream)return true;\n" + - " }catch(java.io.IOException e) {\n" + - " e.printStackTrace();\n" + - " }finally{\n" + - " try {\n" + - " inputStream.close();\n" + - " } catch (java.io.IOException e) {\n" + - " e.printStackTrace();\n" + - " }\n" + - " }\n" + - " return false;\n" + - " }\n" + + string postContent = + " //auto-gen-function \n" + + " public boolean CheckAssetExist(String filePath) \n" + + " { \n" + + " android.content.res.AssetManager assetManager = getAssets(); \n" + + " try \n" + + " { \n" + + " java.io.InputStream inputStream = assetManager.open(filePath); \n" + + " if (null != inputStream) \n" + + " { \n" + + " inputStream.close(); \n" + + " return true; \n" + + " } \n" + + " } \n" + + " catch(java.io.IOException e) \n" + + " { \n" + + " e.printStackTrace(); \n" + + " } \n" + + " return false; \n" + + " } \n" + "}"; if (!readContent[readContent.Length - 18].Contains("CheckAssetExist")) readContent[readContent.Length - 1] = postContent; System.IO.File.WriteAllLines(untityActivityFilePath, readContent); } -} \ No newline at end of file +} + +/* +//auto-gen-function +public boolean CheckAssetExist(String filePath) +{ + android.content.res.AssetManager assetManager = getAssets(); + try + { + java.io.InputStream inputStream = assetManager.open(filePath); + if(null != inputStream) + { + inputStream.close(); + return true; + } + } + catch(java.io.IOException e) + { + e.printStackTrace(); + } + return false; +} +*/ \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/Runtime/StreamingAssetsHelper.cs b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/Runtime/StreamingAssetsHelper.cs index 96292a4..026f597 100644 --- a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/Runtime/StreamingAssetsHelper.cs +++ b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/Runtime/StreamingAssetsHelper.cs @@ -1,10 +1,13 @@ //------------------------------------- // 作者:Stark //------------------------------------- +using System.Collections.Generic; using UnityEngine; public sealed class StreamingAssetsHelper { + private static readonly Dictionary _cacheData = new Dictionary(1000); + #if UNITY_ANDROID && !UNITY_EDITOR private static AndroidJavaClass _unityPlayerClass; public static AndroidJavaClass UnityPlayerClass @@ -33,12 +36,22 @@ public sealed class StreamingAssetsHelper /// public static bool FileExists(string filePath) { - return CurrentActivity.Call("CheckAssetExist", filePath); + if (_cacheData.TryGetValue(filePath, out bool result) == false) + { + result = CurrentActivity.Call("CheckAssetExist", filePath); + _cacheData.Add(filePath, result); + } + return result; } #else public static bool FileExists(string filePath) { - return System.IO.File.Exists(System.IO.Path.Combine(Application.streamingAssetsPath, filePath)); + if (_cacheData.TryGetValue(filePath, out bool result) == false) + { + result = System.IO.File.Exists(System.IO.Path.Combine(Application.streamingAssetsPath, filePath)); + _cacheData.Add(filePath, result); + } + return result; } #endif } \ No newline at end of file