mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
资源桥接器实现IResourceHelper
资源桥接器实现IResourceHelper
This commit is contained in:
90
Assets/TEngine/Scripts/Editor/Helper/HelperInfo.cs
Normal file
90
Assets/TEngine/Scripts/Editor/Helper/HelperInfo.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using TEngine.Runtime;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TEngine.Editor
|
||||
{
|
||||
internal sealed class HelperInfo<T> where T : MonoBehaviour
|
||||
{
|
||||
private const string CustomOptionName = "<Custom>";
|
||||
|
||||
private readonly string m_Name;
|
||||
|
||||
private SerializedProperty m_HelperTypeName;
|
||||
private SerializedProperty m_CustomHelper;
|
||||
private string[] m_HelperTypeNames;
|
||||
private int m_HelperTypeNameIndex;
|
||||
|
||||
public HelperInfo(string name)
|
||||
{
|
||||
m_Name = name;
|
||||
|
||||
m_HelperTypeName = null;
|
||||
m_CustomHelper = null;
|
||||
m_HelperTypeNames = null;
|
||||
m_HelperTypeNameIndex = 0;
|
||||
}
|
||||
|
||||
public void Init(SerializedObject serializedObject)
|
||||
{
|
||||
m_HelperTypeName = serializedObject.FindProperty(Utility.Text.Format("m_{0}HelperTypeName", m_Name));
|
||||
m_CustomHelper = serializedObject.FindProperty(Utility.Text.Format("m_Custom{0}Helper", m_Name));
|
||||
}
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
string displayName = FieldNameForDisplay(m_Name);
|
||||
int selectedIndex = EditorGUILayout.Popup(Utility.Text.Format("{0} Helper", displayName), m_HelperTypeNameIndex, m_HelperTypeNames);
|
||||
if (selectedIndex != m_HelperTypeNameIndex)
|
||||
{
|
||||
m_HelperTypeNameIndex = selectedIndex;
|
||||
m_HelperTypeName.stringValue = selectedIndex <= 0 ? null : m_HelperTypeNames[selectedIndex];
|
||||
}
|
||||
|
||||
if (m_HelperTypeNameIndex <= 0)
|
||||
{
|
||||
EditorGUILayout.PropertyField(m_CustomHelper);
|
||||
if (m_CustomHelper.objectReferenceValue == null)
|
||||
{
|
||||
EditorGUILayout.HelpBox(Utility.Text.Format("You must set Custom {0} Helper.", displayName), MessageType.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
List<string> helperTypeNameList = new List<string>
|
||||
{
|
||||
CustomOptionName
|
||||
};
|
||||
|
||||
helperTypeNameList.AddRange(Type.GetRuntimeTypeNames(typeof(T)));
|
||||
m_HelperTypeNames = helperTypeNameList.ToArray();
|
||||
|
||||
m_HelperTypeNameIndex = 0;
|
||||
if (!string.IsNullOrEmpty(m_HelperTypeName.stringValue))
|
||||
{
|
||||
m_HelperTypeNameIndex = helperTypeNameList.IndexOf(m_HelperTypeName.stringValue);
|
||||
if (m_HelperTypeNameIndex <= 0)
|
||||
{
|
||||
m_HelperTypeNameIndex = 0;
|
||||
m_HelperTypeName.stringValue = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string FieldNameForDisplay(string fieldName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(fieldName))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
string str = Regex.Replace(fieldName, @"^m_", string.Empty);
|
||||
str = Regex.Replace(str, @"((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))", @" $1").TrimStart();
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/TEngine/Scripts/Editor/Helper/HelperInfo.cs.meta
Normal file
3
Assets/TEngine/Scripts/Editor/Helper/HelperInfo.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 094265dd50994f4e94edbb3efe3e2bd4
|
||||
timeCreated: 1662009511
|
@@ -0,0 +1,85 @@
|
||||
using System.Reflection;
|
||||
using TEngine.Runtime;
|
||||
using UnityEditor;
|
||||
|
||||
namespace TEngine.Editor
|
||||
{
|
||||
[CustomEditor(typeof(ResourceComponent))]
|
||||
internal sealed class ResourceComponentInspector :TEngineInspector
|
||||
{
|
||||
private static readonly string[] ResourceModeNames = new string[] { "Package", "Updatable", "Updatable While Playing" };
|
||||
|
||||
private SerializedProperty m_ResourceMode = null;
|
||||
|
||||
private HelperInfo<ResourceHelperBase> m_ResourceHelperInfo = new HelperInfo<ResourceHelperBase>("Resource");
|
||||
|
||||
private int m_ResourceModeIndex = 0;
|
||||
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
m_ResourceMode = serializedObject.FindProperty("ResourceMode");
|
||||
|
||||
m_ResourceHelperInfo.Init(serializedObject);
|
||||
|
||||
RefreshModes();
|
||||
RefreshTypeNames();
|
||||
}
|
||||
|
||||
private void RefreshModes()
|
||||
{
|
||||
m_ResourceModeIndex = m_ResourceMode.enumValueIndex > 0 ? m_ResourceMode.enumValueIndex - 1 : 0;
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
base.OnInspectorGUI();
|
||||
|
||||
serializedObject.Update();
|
||||
|
||||
ResourceComponent t = (ResourceComponent)target;
|
||||
|
||||
EditorGUI.BeginDisabledGroup(EditorApplication.isPlayingOrWillChangePlaymode);
|
||||
{
|
||||
if (EditorApplication.isPlaying && IsPrefabInHierarchy(t.gameObject))
|
||||
{
|
||||
EditorGUILayout.EnumPopup("Resource Mode", t.ResourceMode);
|
||||
}
|
||||
else
|
||||
{
|
||||
int selectedIndex = EditorGUILayout.Popup("Resource Mode", m_ResourceModeIndex, ResourceModeNames);
|
||||
if (selectedIndex != m_ResourceModeIndex)
|
||||
{
|
||||
m_ResourceModeIndex = selectedIndex;
|
||||
m_ResourceMode.enumValueIndex = selectedIndex + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
EditorGUI.EndDisabledGroup();
|
||||
|
||||
EditorGUI.BeginDisabledGroup(EditorApplication.isPlayingOrWillChangePlaymode);
|
||||
{
|
||||
m_ResourceHelperInfo.Draw();
|
||||
}
|
||||
|
||||
EditorGUI.EndDisabledGroup();
|
||||
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
|
||||
Repaint();
|
||||
}
|
||||
|
||||
protected override void OnCompileComplete()
|
||||
{
|
||||
base.OnCompileComplete();
|
||||
|
||||
RefreshTypeNames();
|
||||
}
|
||||
|
||||
private void RefreshTypeNames()
|
||||
{
|
||||
m_ResourceHelperInfo.Refresh();
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 25466079bd094bc98a00b58d89febcf3
|
||||
timeCreated: 1662009376
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1913b7e1eaeb40f482e8d266edb4f37e
|
||||
timeCreated: 1662006234
|
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TEngine.Runtime
|
||||
{
|
||||
public class DefaultResourceHelper:ResourceHelperBase
|
||||
{
|
||||
public override GameObject Load(string path)
|
||||
{
|
||||
return ResMgr.Instance.Load(path);
|
||||
}
|
||||
|
||||
public override GameObject Load(string path, Transform parent)
|
||||
{
|
||||
var obj = Load(path);
|
||||
|
||||
if (obj != null && parent != null)
|
||||
{
|
||||
obj.transform.SetParent(parent);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public override T Load<T>(string path)
|
||||
{
|
||||
return ResMgr.Instance.Load<T>(path);
|
||||
}
|
||||
|
||||
public override void LoadAsync(string path, Action<GameObject> callBack)
|
||||
{
|
||||
ResMgr.Instance.GetGameObjectAsync(path, callBack);
|
||||
}
|
||||
|
||||
public override void LoadAsync<T>(string path, Action<T> callBack, bool withSubAsset = false)
|
||||
{
|
||||
ResMgr.Instance.GetAssetAtPathAsync(path, withSubAsset, callBack);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 66d70c0290ef4134b129edc6d77c5bed
|
||||
timeCreated: 1662006279
|
@@ -225,6 +225,37 @@ namespace TEngine.Runtime
|
||||
onComplete(GetGameObject(path));
|
||||
#endif
|
||||
}
|
||||
|
||||
public void GetAssetAtPathAsync<T>(string path, bool withSubAssets, System.Action<T> onComplete) where T : class
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
onComplete(null);
|
||||
}
|
||||
#if ASSETBUNDLE_ENABLE
|
||||
void CallBack(AssetData assetData)
|
||||
{
|
||||
if (assetData != null)
|
||||
{
|
||||
if (assetData.AssetObject is GameObject)
|
||||
{
|
||||
GameObject go = Object.Instantiate(assetData.AssetObject) as GameObject;
|
||||
BindAssetData(go, assetData);
|
||||
}
|
||||
else
|
||||
{
|
||||
assetData.AddRef();
|
||||
}
|
||||
assetData.OnAsyncLoadComplete -= CallBack;
|
||||
|
||||
onComplete(assetData.AssetObject as T);
|
||||
}
|
||||
_assetConfig.GetAssetAtPathAsync(path, withSubAssets, CallBack);
|
||||
}
|
||||
#else
|
||||
onComplete(GetAsset(path,withSubAssets)?.AssetObject as T);
|
||||
#endif
|
||||
}
|
||||
|
||||
public void GetAssetAtPathAsync(string path, bool withSubAssets, System.Action<AssetData> onComplete)
|
||||
{
|
95
Assets/TEngine/Scripts/Runtime/Core/Res/IResourceHelper.cs
Normal file
95
Assets/TEngine/Scripts/Runtime/Core/Res/IResourceHelper.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TEngine.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// 游戏资源加载辅助器
|
||||
/// </summary>
|
||||
public interface IResourceHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 同步加载GameObject
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
GameObject Load(string path);
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载GameObject
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="parent"></param>
|
||||
/// <returns></returns>
|
||||
GameObject Load(string path, Transform parent);
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载泛型
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
T Load<T>(string path) where T : UnityEngine.Object;
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载GameObject
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="callBack"></param>
|
||||
void LoadAsync(string path, Action<GameObject> callBack);
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载泛型
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="callBack"></param>
|
||||
/// <param name="withSubAsset"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
void LoadAsync<T>(string path, Action<T> callBack, bool withSubAsset = false) where T : class;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 游戏资源加载辅助器基类
|
||||
/// </summary>
|
||||
public abstract class ResourceHelperBase : UnityEngine.MonoBehaviour, IResourceHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 同步加载GameObject
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public abstract GameObject Load(string path);
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载GameObject
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="parent"></param>
|
||||
/// <returns></returns>
|
||||
public abstract GameObject Load(string path, Transform parent);
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载泛型
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public abstract T Load<T>(string path) where T : UnityEngine.Object;
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载GameObject
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="callBack"></param>
|
||||
public abstract void LoadAsync(string path, Action<GameObject> callBack);
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载泛型
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="callBack"></param>
|
||||
/// <param name="withSubAsset"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract void LoadAsync<T>(string path, Action<T> callBack, bool withSubAsset = false) where T : class;
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 43465b53b14046bd897a19f294a19685
|
||||
timeCreated: 1662005857
|
@@ -12,18 +12,29 @@ namespace TEngine.Runtime
|
||||
{
|
||||
[SerializeField] public ResourceMode ResourceMode = ResourceMode.Package;
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[SerializeField] public bool EditorResourceMode = true;
|
||||
#else
|
||||
private bool _editorResourceMode = false;
|
||||
public bool EditorResourceMode => _editorResourceMode;
|
||||
#endif
|
||||
|
||||
|
||||
public override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
ResourceHelperBase resourceHelper = null;
|
||||
|
||||
resourceHelper = Helper.CreateHelper(m_ResourceHelperTypeName, m_CustomResourceHelper);
|
||||
if (resourceHelper == null)
|
||||
{
|
||||
Log.Error("Can not create resource helper.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (resourceHelper != null)
|
||||
{
|
||||
TResources.SetResourceHelper(resourceHelper);
|
||||
}
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
private string m_ResourceHelperTypeName = "TEngine.Runtime.DefaultResourceHelper";
|
||||
|
||||
[SerializeField]
|
||||
private ResourceHelperBase m_CustomResourceHelper = null;
|
||||
}
|
||||
}
|
@@ -6,39 +6,68 @@ namespace TEngine.Runtime
|
||||
/// <summary>
|
||||
/// 通用资源加载接口
|
||||
/// </summary>
|
||||
public static class TResources
|
||||
public class TResources
|
||||
{
|
||||
#region 同步加载
|
||||
private static IResourceHelper m_ResourceHelper;
|
||||
/// <summary>
|
||||
/// 设置游戏资源加载辅助器。
|
||||
/// </summary>
|
||||
/// <param name="resourceHelper">游戏资源加载辅助器。</param>
|
||||
public static void SetResourceHelper(IResourceHelper resourceHelper)
|
||||
{
|
||||
if (resourceHelper == null)
|
||||
{
|
||||
throw new Exception("Resources helper is invalid.");
|
||||
}
|
||||
|
||||
m_ResourceHelper = resourceHelper;
|
||||
}
|
||||
|
||||
public static GameObject Load(string path)
|
||||
{
|
||||
return ResMgr.Instance.Load(path);
|
||||
if (m_ResourceHelper == null)
|
||||
{
|
||||
Log.Error("Resources helper is invalid.");
|
||||
return null;
|
||||
}
|
||||
return m_ResourceHelper.Load(path);
|
||||
}
|
||||
public static GameObject Load(string path, Transform parent)
|
||||
{
|
||||
var obj = Load(path);
|
||||
|
||||
if (obj != null && parent != null)
|
||||
if (m_ResourceHelper == null)
|
||||
{
|
||||
obj.transform.SetParent(parent);
|
||||
Log.Error("Resources helper is invalid.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return obj;
|
||||
return m_ResourceHelper.Load(path,parent);
|
||||
}
|
||||
public static T Load<T>(string path) where T : UnityEngine.Object
|
||||
{
|
||||
return ResMgr.Instance.Load<T>(path);
|
||||
if (m_ResourceHelper == null)
|
||||
{
|
||||
Log.Error("Resources helper is invalid.");
|
||||
return null;
|
||||
}
|
||||
return m_ResourceHelper.Load<T>(path);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 异步加载
|
||||
public static void LoadAsync(string path, Action<GameObject> callBack)
|
||||
{
|
||||
ResMgr.Instance.GetGameObjectAsync(path, callBack);
|
||||
if (m_ResourceHelper == null)
|
||||
{
|
||||
Log.Error("Resources helper is invalid.");
|
||||
return;
|
||||
}
|
||||
m_ResourceHelper.LoadAsync(path,callBack);
|
||||
}
|
||||
public static void LoadAsync(string path, Action<AssetData> callBack, bool withSubAsset = false)
|
||||
{
|
||||
ResMgr.Instance.GetAssetAtPathAsync(path, withSubAsset, callBack);
|
||||
if (m_ResourceHelper == null)
|
||||
{
|
||||
Log.Error("Resources helper is invalid.");
|
||||
return;
|
||||
}
|
||||
m_ResourceHelper.LoadAsync(path,callBack,withSubAsset);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7fc4d5869442aff4d9ffc623d24c792f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using TEngine.Runtime;
|
||||
using UnityEngine;
|
||||
|
||||
public class TestResourceHelper : ResourceHelperBase
|
||||
{
|
||||
public override GameObject Load(string path)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override GameObject Load(string path, Transform parent)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override T Load<T>(string path)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void LoadAsync(string path, Action<GameObject> callBack)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void LoadAsync<T>(string path, Action<T> callBack, bool withSubAsset = false)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86337800b6513e44ba3d9a553b4f6b47
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -27,12 +27,7 @@ namespace TEngine.Runtime
|
||||
|
||||
// TODO: 这里可以播放一个 Splash 动画
|
||||
// ...
|
||||
if (ResourceComponent.Instance.EditorResourceMode == true)
|
||||
{
|
||||
Log.Info("编辑器模式 ChangeState<ProcedureStartGame>");
|
||||
ChangeState<ProcedureStartGame>(procedureOwner);
|
||||
}
|
||||
else if (ResourceComponent.Instance.ResourceMode == ResourceMode.Package)
|
||||
if (ResourceComponent.Instance.ResourceMode == ResourceMode.Package)
|
||||
{
|
||||
Log.Info("单机模式 ChangeState<ProcedureInitResources>");
|
||||
ChangeState<ProcedureResourcesInit>(procedureOwner);
|
||||
|
Reference in New Issue
Block a user