资源桥接器实现IResourceHelper

资源桥接器实现IResourceHelper
This commit is contained in:
ALEXTANG
2022-09-01 14:18:44 +08:00
parent adc2cbd3db
commit 5f6054c986
27 changed files with 492 additions and 49 deletions

View File

@@ -259,11 +259,6 @@ PrefabInstance:
propertyPath: m_Name
value: TEngine
objectReference: {fileID: 0}
- target: {fileID: 3463045026180535779, guid: 161ff7c8132079c4a95e2e4e70ddd41b,
type: 3}
propertyPath: m_RootOrder
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3463045026180535779, guid: 161ff7c8132079c4a95e2e4e70ddd41b,
type: 3}
propertyPath: m_LocalPosition.x
@@ -279,11 +274,6 @@ PrefabInstance:
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3463045026180535779, guid: 161ff7c8132079c4a95e2e4e70ddd41b,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3463045026180535779, guid: 161ff7c8132079c4a95e2e4e70ddd41b,
type: 3}
propertyPath: m_LocalRotation.x
@@ -299,6 +289,16 @@ PrefabInstance:
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3463045026180535779, guid: 161ff7c8132079c4a95e2e4e70ddd41b,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3463045026180535779, guid: 161ff7c8132079c4a95e2e4e70ddd41b,
type: 3}
propertyPath: m_RootOrder
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3463045026180535779, guid: 161ff7c8132079c4a95e2e4e70ddd41b,
type: 3}
propertyPath: m_LocalEulerAnglesHint.x

View File

@@ -29,7 +29,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 3463045026180535779}
m_RootOrder: 0
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &3463045025307533305
MonoBehaviour:
@@ -95,7 +95,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 3463045026180535779}
m_RootOrder: 3
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &3463045025737563820
MonoBehaviour:
@@ -151,7 +151,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 3463045026180535779}
m_RootOrder: 4
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &3463045025836799504
MonoBehaviour:
@@ -195,7 +195,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 3463045026180535779}
m_RootOrder: 1
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &3463045025974401033
MonoBehaviour:
@@ -209,7 +209,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: f010c29093f111146bfc776d19c8fb27, type: 3}
m_Name:
m_EditorClassIdentifier:
m_BuildInfoTextAsset: {fileID: 4900000, guid: d231f7a19087f02418363f9a6d5cb2c7, type: 3}
m_BuildInfoTextAsset: {fileID: 4900000, guid: d231f7a19087f02418363f9a6d5cb2c7,
type: 3}
--- !u!1 &3463045026010536328
GameObject:
m_ObjectHideFlags: 0
@@ -239,7 +240,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 3463045026180535779}
m_RootOrder: 5
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &3463045026010536330
MonoBehaviour:
@@ -254,7 +255,8 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
ResourceMode: 1
EditorResourceMode: 1
m_ResourceHelperTypeName: TEngine.Runtime.DefaultResourceHelper
m_CustomResourceHelper: {fileID: 0}
--- !u!1 &3463045026180535776
GameObject:
m_ObjectHideFlags: 0
@@ -282,12 +284,12 @@ Transform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 3463045025307533286}
- {fileID: 3463045025974401142}
- {fileID: 3463045026377943188}
- {fileID: 3463045025737563821}
- {fileID: 3463045025836799505}
- {fileID: 3463045026010536331}
- {fileID: 3463045025307533286}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -320,7 +322,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 3463045026180535779}
m_RootOrder: 2
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &3463045026377943191
MonoBehaviour:

View 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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 094265dd50994f4e94edbb3efe3e2bd4
timeCreated: 1662009511

View File

@@ -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();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 25466079bd094bc98a00b58d89febcf3
timeCreated: 1662009376

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1913b7e1eaeb40f482e8d266edb4f37e
timeCreated: 1662006234

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 66d70c0290ef4134b129edc6d77c5bed
timeCreated: 1662006279

View File

@@ -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)
{

View 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;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 43465b53b14046bd897a19f294a19685
timeCreated: 1662005857

View File

@@ -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;
}
}

View File

@@ -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
}
}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7fc4d5869442aff4d9ffc623d24c792f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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();
}
}

View File

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

View File

@@ -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);