初始化
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
public class ResMgr : Singleton<ResMgr>
|
||||
{
|
||||
public T Load<T>(string name)where T:Object
|
||||
{
|
||||
T res = Resources.Load<T>(name);
|
||||
if (res is GameObject)
|
||||
return Object.Instantiate(res);
|
||||
else
|
||||
return res;
|
||||
}
|
||||
public T Load<T>(string name,Vector3 position,Quaternion rotation) where T : Object
|
||||
{
|
||||
T res = Resources.Load<T>(name);
|
||||
if (res is GameObject)
|
||||
return Object.Instantiate(res, position, rotation);
|
||||
else
|
||||
return res;
|
||||
}
|
||||
public void LoadAsync<T>(string name,UnityAction<T> callback)where T:Object
|
||||
{
|
||||
MonoMgr.Instance.StartCoroutine(ReallyLoadAsync(name,callback));
|
||||
}
|
||||
private IEnumerator ReallyLoadAsync<T>(string name,UnityAction<T> callback)where T:Object
|
||||
{
|
||||
ResourceRequest r = Resources.LoadAsync<T>(name);
|
||||
yield return r;
|
||||
if (r.asset is GameObject)
|
||||
{
|
||||
callback(Object.Instantiate(r.asset) as T);
|
||||
}
|
||||
else
|
||||
{
|
||||
callback(r.asset as T);
|
||||
}
|
||||
}
|
||||
#region 异步加载使用方法(得到加载物体并设定位置等)
|
||||
//需要注意的是,异步加载中,调用异步加载后,物体并没有被加载,需要等n帧才会被加载,所以我们不能直接返回物体,而是在方法中将物体传出去。
|
||||
//private void Update()
|
||||
//{
|
||||
// if (Input.GetMouseButtonDown(1))
|
||||
// {
|
||||
// ResMgr.Instance.LoadAsync<GameObject>("cube", DoSome);
|
||||
// }
|
||||
//}
|
||||
//private void DoSome(GameObject obj)
|
||||
//{
|
||||
// Debug.Log("调用了回调函数");
|
||||
//}
|
||||
////为了方便,我们还可以直接使用Lambda表达式来方便书写
|
||||
//private void Update()
|
||||
//{
|
||||
// if (Input.GetMouseButtonDown(1))
|
||||
// {
|
||||
// ResMgr.GetInstance().LoadAsync<GameObject>("cube", (obj) =>
|
||||
// {
|
||||
// Debug.Log("调用了回调函数");
|
||||
// });
|
||||
// }
|
||||
//}
|
||||
#endregion
|
||||
|
||||
|
||||
//Addressable
|
||||
public void AALoadAsync<T>(string name, UnityAction<T> callback) where T : Object
|
||||
{
|
||||
MonoMgr.Instance.StartCoroutine(AAReallyLoadAsync(name, callback));
|
||||
}
|
||||
private IEnumerator AAReallyLoadAsync<T>(string name, UnityAction<T> callback) where T : Object
|
||||
{
|
||||
AsyncOperationHandle async = Addressables.LoadAssetAsync<T>(name);
|
||||
yield return async;
|
||||
if (async.Result is GameObject)
|
||||
{
|
||||
callback(Object.Instantiate(async.Result as GameObject) as T);
|
||||
}
|
||||
else
|
||||
{
|
||||
callback(async.Result as T);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user