update samples

pull/70/head
hevinci 2023-02-22 15:29:00 +08:00
parent 521e3e2587
commit 26ffb829d0
2 changed files with 58 additions and 21 deletions

View File

@ -13,27 +13,51 @@ internal class AndroidPost : UnityEditor.Android.IPostGenerateGradleAndroidProje
path = path.Replace("\\", "/"); path = path.Replace("\\", "/");
string untityActivityFilePath = $"{path}/src/main/java/com/unity3d/player/UnityPlayerActivity.java"; string untityActivityFilePath = $"{path}/src/main/java/com/unity3d/player/UnityPlayerActivity.java";
var readContent = System.IO.File.ReadAllLines(untityActivityFilePath); var readContent = System.IO.File.ReadAllLines(untityActivityFilePath);
string postContent = " //auto-gen-function\n" + string postContent =
" public boolean CheckAssetExist(String filePath){\n" + " //auto-gen-function \n" +
" android.content.res.AssetManager assetManager = getAssets();\n" + " public boolean CheckAssetExist(String filePath) \n" +
" java.io.InputStream inputStream = null;\n" + " { \n" +
" try {\n" + " android.content.res.AssetManager assetManager = getAssets(); \n" +
" inputStream = assetManager.open(filePath);\n" + " try \n" +
" if(null != inputStream)return true;\n" + " { \n" +
" }catch(java.io.IOException e) {\n" + " java.io.InputStream inputStream = assetManager.open(filePath); \n" +
" e.printStackTrace();\n" + " if (null != inputStream) \n" +
" }finally{\n" + " { \n" +
" try {\n" + " inputStream.close(); \n" +
" inputStream.close();\n" + " return true; \n" +
" } catch (java.io.IOException e) {\n" + " } \n" +
" e.printStackTrace();\n" + " } \n" +
" }\n" + " catch(java.io.IOException e) \n" +
" }\n" + " { \n" +
" return false;\n" + " e.printStackTrace(); \n" +
" }\n" + " } \n" +
" return false; \n" +
" } \n" +
"}"; "}";
if (!readContent[readContent.Length - 18].Contains("CheckAssetExist")) if (!readContent[readContent.Length - 18].Contains("CheckAssetExist"))
readContent[readContent.Length - 1] = postContent; readContent[readContent.Length - 1] = postContent;
System.IO.File.WriteAllLines(untityActivityFilePath, readContent); System.IO.File.WriteAllLines(untityActivityFilePath, readContent);
} }
} }
/*
//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;
}
*/

View File

@ -1,10 +1,13 @@
//------------------------------------- //-------------------------------------
// 作者Stark // 作者Stark
//------------------------------------- //-------------------------------------
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public sealed class StreamingAssetsHelper public sealed class StreamingAssetsHelper
{ {
private static readonly Dictionary<string, bool> _cacheData = new Dictionary<string, bool>(1000);
#if UNITY_ANDROID && !UNITY_EDITOR #if UNITY_ANDROID && !UNITY_EDITOR
private static AndroidJavaClass _unityPlayerClass; private static AndroidJavaClass _unityPlayerClass;
public static AndroidJavaClass UnityPlayerClass public static AndroidJavaClass UnityPlayerClass
@ -33,12 +36,22 @@ public sealed class StreamingAssetsHelper
/// </summary> /// </summary>
public static bool FileExists(string filePath) public static bool FileExists(string filePath)
{ {
return CurrentActivity.Call<bool>("CheckAssetExist", filePath); if (_cacheData.TryGetValue(filePath, out bool result) == false)
{
result = CurrentActivity.Call<bool>("CheckAssetExist", filePath);
_cacheData.Add(filePath, result);
}
return result;
} }
#else #else
public static bool FileExists(string filePath) 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 #endif
} }