泛型约束

泛型约束
This commit is contained in:
ALEXTANG
2022-09-01 15:00:09 +08:00
parent 5f6054c986
commit c6691d9e2f
6 changed files with 99 additions and 8 deletions

View File

@@ -3,7 +3,7 @@ using UnityEngine;
namespace TEngine.Runtime
{
public class DefaultResourceHelper:ResourceHelperBase
public class DefaultResourceHelper : ResourceHelperBase
{
public override GameObject Load(string path)
{

View File

@@ -45,7 +45,7 @@ namespace TEngine.Runtime
/// <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;
void LoadAsync<T>(string path, Action<T> callBack, bool withSubAsset = false) where T : UnityEngine.Object;
}
/// <summary>
@@ -90,6 +90,7 @@ namespace TEngine.Runtime
/// <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;
public abstract void LoadAsync<T>(string path, Action<T> callBack, bool withSubAsset = false)
where T : UnityEngine.Object;
}
}

View File

@@ -9,6 +9,7 @@ namespace TEngine.Runtime
public class TResources
{
private static IResourceHelper m_ResourceHelper;
/// <summary>
/// 设置游戏资源加载辅助器。
/// </summary>
@@ -30,8 +31,10 @@ namespace TEngine.Runtime
Log.Error("Resources helper is invalid.");
return null;
}
return m_ResourceHelper.Load(path);
}
public static GameObject Load(string path, Transform parent)
{
if (m_ResourceHelper == null)
@@ -39,8 +42,10 @@ namespace TEngine.Runtime
Log.Error("Resources helper is invalid.");
return null;
}
return m_ResourceHelper.Load(path,parent);
return m_ResourceHelper.Load(path, parent);
}
public static T Load<T>(string path) where T : UnityEngine.Object
{
if (m_ResourceHelper == null)
@@ -48,6 +53,7 @@ namespace TEngine.Runtime
Log.Error("Resources helper is invalid.");
return null;
}
return m_ResourceHelper.Load<T>(path);
}
@@ -58,16 +64,20 @@ namespace TEngine.Runtime
Log.Error("Resources helper is invalid.");
return;
}
m_ResourceHelper.LoadAsync(path,callBack);
m_ResourceHelper.LoadAsync(path, callBack);
}
public static void LoadAsync(string path, Action<AssetData> callBack, bool withSubAsset = false)
public static void LoadAsync<T>(string path, Action<T> callBack, bool withSubAsset = false)
where T : UnityEngine.Object
{
if (m_ResourceHelper == null)
{
Log.Error("Resources helper is invalid.");
return;
}
m_ResourceHelper.LoadAsync(path,callBack,withSubAsset);
m_ResourceHelper.LoadAsync<T>(path, callBack, withSubAsset);
}
}
}

View File

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

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections;
using UnityEngine;
namespace TEngine.Runtime
{
public class UnityResourceHelper : ResourceHelperBase
{
public override GameObject Load(string path)
{
return Resources.Load<GameObject>(path);
}
public override GameObject Load(string path, Transform parent)
{
var obj = Load(path);
if (obj == null)
{
return null;
}
if (parent != null)
{
obj.transform.SetParent(parent);
}
return obj;
}
public override T Load<T>(string path)
{
return Resources.Load<T>(path);
}
public override void LoadAsync(string path, Action<GameObject> callBack)
{
MonoUtility.StartCoroutine(ReallyLoadAsync(name, callBack));
}
private IEnumerator ReallyLoadAsync<T>(string name, Action<T> callback = null) where T : UnityEngine.Object
{
ResourceRequest request = Resources.LoadAsync<T>(name);
yield return request;
if (request.asset is GameObject)
{
callback?.Invoke(GameObject.Instantiate(request.asset) as T);
}
else
{
callback?.Invoke(request.asset as T);
}
}
public override void LoadAsync<T>(string path, Action<T> callBack, bool withSubAsset = false)
{
MonoUtility.StartCoroutine(ReallyLoadAsync<T>(name, callBack));
}
}
}

View File

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