update space shooter

启用了新的内置资源查询机制。
main
hevinci 2023-06-14 18:52:59 +08:00
parent f1a5965b4c
commit 78693deed6
7 changed files with 254 additions and 129 deletions

View File

@ -92,41 +92,29 @@ internal class FsmInitialize : IStateNode
{ {
//string hostServerIP = "http://10.0.2.2"; //安卓模拟器地址 //string hostServerIP = "http://10.0.2.2"; //安卓模拟器地址
string hostServerIP = "http://127.0.0.1"; string hostServerIP = "http://127.0.0.1";
string gameVersion = "v1.0"; string appVersion = "v1.0";
#if UNITY_EDITOR #if UNITY_EDITOR
if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android) if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android)
return $"{hostServerIP}/CDN/Android/{gameVersion}"; return $"{hostServerIP}/CDN/Android/{appVersion}";
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS) else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS)
return $"{hostServerIP}/CDN/IPhone/{gameVersion}"; return $"{hostServerIP}/CDN/IPhone/{appVersion}";
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL) else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL)
return $"{hostServerIP}/CDN/WebGL/{gameVersion}"; return $"{hostServerIP}/CDN/WebGL/{appVersion}";
else else
return $"{hostServerIP}/CDN/PC/{gameVersion}"; return $"{hostServerIP}/CDN/PC/{appVersion}";
#else #else
if (Application.platform == RuntimePlatform.Android) if (Application.platform == RuntimePlatform.Android)
return $"{hostServerIP}/CDN/Android/{gameVersion}"; return $"{hostServerIP}/CDN/Android/{appVersion}";
else if (Application.platform == RuntimePlatform.IPhonePlayer) else if (Application.platform == RuntimePlatform.IPhonePlayer)
return $"{hostServerIP}/CDN/IPhone/{gameVersion}"; return $"{hostServerIP}/CDN/IPhone/{appVersion}";
else if (Application.platform == RuntimePlatform.WebGLPlayer) else if (Application.platform == RuntimePlatform.WebGLPlayer)
return $"{hostServerIP}/CDN/WebGL/{gameVersion}"; return $"{hostServerIP}/CDN/WebGL/{appVersion}";
else else
return $"{hostServerIP}/CDN/PC/{gameVersion}"; return $"{hostServerIP}/CDN/PC/{appVersion}";
#endif #endif
} }
/// <summary>
/// 内置文件查询服务类
/// </summary>
private class GameQueryServices : IQueryServices
{
public bool QueryStreamingAssets(string fileName)
{
string buildinFolderName = YooAssets.GetStreamingAssetBuildinFolderName();
return StreamingAssetsHelper.FileExists($"{buildinFolderName}/{fileName}");
}
}
/// <summary> /// <summary>
/// 资源文件解密服务类 /// 资源文件解密服务类
/// </summary> /// </summary>

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 内置资源清单
/// </summary>
public class BuildinFileManifest : ScriptableObject
{
public List<string> BuildinFiles = new List<string>();
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 71b02dfa7aa9d4545b3417a18477fbee
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,134 +1,89 @@
//------------------------------------- using System.IO;
// 作者Stark
//-------------------------------------
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using YooAsset;
/// <summary>
/// 内置文件查询服务类
/// </summary>
public class GameQueryServices : IQueryServices
{
public bool QueryStreamingAssets(string fileName)
{
// 注意fileName包含文件格式
return StreamingAssetsHelper.FileExists(fileName);
}
}
/// <summary> /// <summary>
/// StreamingAssets目录下资源查询帮助类 /// StreamingAssets目录下资源查询帮助类
/// </summary> /// </summary>
public sealed class StreamingAssetsHelper public sealed class StreamingAssetsHelper
{ {
private static readonly Dictionary<string, bool> _cacheData = new Dictionary<string, bool>(1000); private static bool _isInit = false;
private static readonly HashSet<string> _cacheData = new HashSet<string>();
#if UNITY_ANDROID && !UNITY_EDITOR /// <summary>
private static AndroidJavaClass _unityPlayerClass; /// 初始化
public static AndroidJavaClass UnityPlayerClass /// </summary>
public static void Init()
{ {
get if (_isInit == false)
{ {
if (_unityPlayerClass == null) _isInit = true;
_unityPlayerClass = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer"); var manifest = Resources.Load<BuildinFileManifest>("BuildinFileManifest");
return _unityPlayerClass; foreach (string fileName in manifest.BuildinFiles)
} {
} _cacheData.Add(fileName);
}
private static AndroidJavaObject _currentActivity;
public static AndroidJavaObject CurrentActivity
{
get
{
if (_currentActivity == null)
_currentActivity = UnityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
return _currentActivity;
} }
} }
/// <summary> /// <summary>
/// 利用安卓原生接口查询内置文件是否存在 /// 内置文件查询方法
/// </summary> /// </summary>
public static bool FileExists(string filePath) public static bool FileExists(string fileName)
{ {
if (_cacheData.TryGetValue(filePath, out bool result) == false) if (_isInit == false)
{ Init();
result = CurrentActivity.Call<bool>("CheckAssetExist", filePath);
_cacheData.Add(filePath, result); return _cacheData.Contains(fileName);
}
return result;
} }
#else
public static bool FileExists(string 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
} }
#if UNITY_EDITOR
#if UNITY_ANDROID && UNITY_EDITOR internal class PreprocessBuild : UnityEditor.Build.IPreprocessBuildWithReport
/// <summary>
/// 为Github对开发者的友好采用自动补充UnityPlayerActivity.java文件的通用姿势满足各个开发者
/// </summary>
internal class AndroidPost : UnityEditor.Android.IPostGenerateGradleAndroidProject
{ {
public int callbackOrder => 99; public int callbackOrder { get { return 0; } }
public void OnPostGenerateGradleAndroidProject(string path)
{
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" +
" { \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" +
" } \n" +
" return false; \n" +
" } \n" +
"}";
if (CheckFunctionExist(readContent) == false) /// <summary>
readContent[readContent.Length - 1] = postContent; /// 在构建应用程序前处理
System.IO.File.WriteAllLines(untityActivityFilePath, readContent); /// </summary>
} public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
private bool CheckFunctionExist(string[] contents)
{ {
for (int i = 0; i < contents.Length; i++) var manifest = ScriptableObject.CreateInstance<BuildinFileManifest>();
string folderPath = $"{Application.dataPath}/StreamingAssets/BuildinFiles";
DirectoryInfo root = new DirectoryInfo(folderPath);
FileInfo[] files = root.GetFiles();
foreach (var fileInfo in files)
{ {
if (contents[i].Contains("CheckAssetExist")) if (fileInfo.Extension == ".meta")
{ continue;
return true; if (fileInfo.Name.StartsWith("PackageManifest_"))
} continue;
manifest.BuildinFiles.Add(fileInfo.Name);
} }
return false;
string saveFilePath = "Assets/Resources/BuildinFileManifest.asset";
if (File.Exists(saveFilePath))
File.Delete(saveFilePath);
if (Directory.Exists("Assets/Resources") == false)
Directory.CreateDirectory("Assets/Resources");
UnityEditor.AssetDatabase.CreateAsset(manifest, saveFilePath);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
Debug.Log($"内置资源清单保存成功 : {saveFilePath}");
} }
} }
#endif #endif
/*
//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,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 358145d67e230b34883002b08b23cba3 guid: ca0617f5ec2b4504b923e3205dc77f54
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,150 @@
//-------------------------------------
// 作者Stark
//-------------------------------------
using System.Collections.Generic;
using UnityEngine;
using YooAsset;
/*
/// <summary>
/// 内置文件查询服务类
/// </summary>
public class GameQueryServices : IQueryServices
{
public bool QueryStreamingAssets(string fileName)
{
string buildinFolderName = YooAssets.GetStreamingAssetBuildinFolderName();
return StreamingAssetsHelper.FileExists($"{buildinFolderName}/{fileName}");
}
}
/// <summary>
/// StreamingAssets目录下资源查询帮助类
/// </summary>
public sealed class StreamingAssetsHelper
{
private static readonly Dictionary<string, bool> _cacheData = new Dictionary<string, bool>(1000);
#if UNITY_ANDROID && !UNITY_EDITOR
private static AndroidJavaClass _unityPlayerClass;
public static AndroidJavaClass UnityPlayerClass
{
get
{
if (_unityPlayerClass == null)
_unityPlayerClass = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer");
return _unityPlayerClass;
}
}
private static AndroidJavaObject _currentActivity;
public static AndroidJavaObject CurrentActivity
{
get
{
if (_currentActivity == null)
_currentActivity = UnityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
return _currentActivity;
}
}
/// <summary>
/// 利用安卓原生接口查询内置文件是否存在
/// </summary>
public static bool FileExists(string filePath)
{
if (_cacheData.TryGetValue(filePath, out bool result) == false)
{
result = CurrentActivity.Call<bool>("CheckAssetExist", filePath);
_cacheData.Add(filePath, result);
}
return result;
}
#else
public static bool FileExists(string 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
}
#if UNITY_ANDROID && UNITY_EDITOR
/// <summary>
/// 为Github对开发者的友好采用自动补充UnityPlayerActivity.java文件的通用姿势满足各个开发者
/// </summary>
internal class AndroidPost : UnityEditor.Android.IPostGenerateGradleAndroidProject
{
public int callbackOrder => 99;
public void OnPostGenerateGradleAndroidProject(string path)
{
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" +
" { \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" +
" } \n" +
" return false; \n" +
" } \n" +
"}";
if (CheckFunctionExist(readContent) == false)
readContent[readContent.Length - 1] = postContent;
System.IO.File.WriteAllLines(untityActivityFilePath, readContent);
}
private bool CheckFunctionExist(string[] contents)
{
for (int i = 0; i < contents.Length; i++)
{
if (contents[i].Contains("CheckAssetExist"))
{
return true;
}
}
return false;
}
}
#endif
*/
/*
//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

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 358145d67e230b34883002b08b23cba3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: