StreamingAssetsHelper2.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //-------------------------------------
  2. // 作者:Stark
  3. //-------------------------------------
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using YooAsset;
  7. /*
  8. /// <summary>
  9. /// 资源文件查询服务类
  10. /// </summary>
  11. public class GameQueryServices2 : IQueryServices
  12. {
  13. public DeliveryFileInfo GetDeliveryFileInfo(string packageName, string fileName)
  14. {
  15. throw new System.NotImplementedException();
  16. }
  17. public bool QueryDeliveryFiles(string packageName, string fileName)
  18. {
  19. return false;
  20. }
  21. public bool QueryStreamingAssets(string packageName, string fileName)
  22. {
  23. return StreamingAssetsHelper2.FileExists($"{StreamingAssetsDefine.RootFolderName}/{packageName}/{fileName}");
  24. }
  25. }
  26. /// <summary>
  27. /// StreamingAssets目录下资源查询帮助类
  28. /// </summary>
  29. public sealed class StreamingAssetsHelper2
  30. {
  31. private static readonly Dictionary<string, bool> _cacheData = new Dictionary<string, bool>(1000);
  32. #if UNITY_ANDROID && !UNITY_EDITOR
  33. private static AndroidJavaClass _unityPlayerClass;
  34. public static AndroidJavaClass UnityPlayerClass
  35. {
  36. get
  37. {
  38. if (_unityPlayerClass == null)
  39. _unityPlayerClass = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer");
  40. return _unityPlayerClass;
  41. }
  42. }
  43. private static AndroidJavaObject _currentActivity;
  44. public static AndroidJavaObject CurrentActivity
  45. {
  46. get
  47. {
  48. if (_currentActivity == null)
  49. _currentActivity = UnityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
  50. return _currentActivity;
  51. }
  52. }
  53. /// <summary>
  54. /// 利用安卓原生接口查询内置文件是否存在
  55. /// </summary>
  56. public static bool FileExists(string filePath)
  57. {
  58. if (_cacheData.TryGetValue(filePath, out bool result) == false)
  59. {
  60. result = CurrentActivity.Call<bool>("CheckAssetExist", filePath);
  61. _cacheData.Add(filePath, result);
  62. }
  63. return result;
  64. }
  65. #else
  66. public static bool FileExists(string filePath)
  67. {
  68. if (_cacheData.TryGetValue(filePath, out bool result) == false)
  69. {
  70. result = System.IO.File.Exists(System.IO.Path.Combine(Application.streamingAssetsPath, filePath));
  71. _cacheData.Add(filePath, result);
  72. }
  73. return result;
  74. }
  75. #endif
  76. }
  77. #if UNITY_ANDROID && UNITY_EDITOR
  78. /// <summary>
  79. /// 为Github对开发者的友好,采用自动补充UnityPlayerActivity.java文件的通用姿势满足各个开发者
  80. /// </summary>
  81. internal class AndroidPost : UnityEditor.Android.IPostGenerateGradleAndroidProject
  82. {
  83. public int callbackOrder => 99;
  84. public void OnPostGenerateGradleAndroidProject(string path)
  85. {
  86. path = path.Replace("\\", "/");
  87. string untityActivityFilePath = $"{path}/src/main/java/com/unity3d/player/UnityPlayerActivity.java";
  88. var readContent = System.IO.File.ReadAllLines(untityActivityFilePath);
  89. string postContent =
  90. " //auto-gen-function \n" +
  91. " public boolean CheckAssetExist(String filePath) \n" +
  92. " { \n" +
  93. " android.content.res.AssetManager assetManager = getAssets(); \n" +
  94. " try \n" +
  95. " { \n" +
  96. " java.io.InputStream inputStream = assetManager.open(filePath); \n" +
  97. " if (null != inputStream) \n" +
  98. " { \n" +
  99. " inputStream.close(); \n" +
  100. " return true; \n" +
  101. " } \n" +
  102. " } \n" +
  103. " catch(java.io.IOException e) \n" +
  104. " { \n" +
  105. " } \n" +
  106. " return false; \n" +
  107. " } \n" +
  108. "}";
  109. if (CheckFunctionExist(readContent) == false)
  110. readContent[readContent.Length - 1] = postContent;
  111. System.IO.File.WriteAllLines(untityActivityFilePath, readContent);
  112. }
  113. private bool CheckFunctionExist(string[] contents)
  114. {
  115. for (int i = 0; i < contents.Length; i++)
  116. {
  117. if (contents[i].Contains("CheckAssetExist"))
  118. {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. }
  125. #endif
  126. */
  127. /*
  128. // 以下代码为安卓原生代码,不需要解除注释
  129. //auto-gen-function
  130. public boolean CheckAssetExist(String filePath)
  131. {
  132. android.content.res.AssetManager assetManager = getAssets();
  133. try
  134. {
  135. java.io.InputStream inputStream = assetManager.open(filePath);
  136. if(null != inputStream)
  137. {
  138. inputStream.close();
  139. return true;
  140. }
  141. }
  142. catch(java.io.IOException e)
  143. {
  144. //e.printStackTrace();
  145. }
  146. return false;
  147. }
  148. */