资源加载模块
This commit is contained in:
parent
00773b56d3
commit
7dd4942be3
@ -113,7 +113,7 @@ MonoBehaviour:
|
|||||||
m_PrefabInstance: {fileID: 0}
|
m_PrefabInstance: {fileID: 0}
|
||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_GameObject: {fileID: 3745365020364595915}
|
m_GameObject: {fileID: 3745365020364595915}
|
||||||
m_Enabled: 1
|
m_Enabled: 0
|
||||||
m_EditorHideFlags: 0
|
m_EditorHideFlags: 0
|
||||||
m_Script: {fileID: 11500000, guid: 278639fb612259b4890f4f8615f9305f, type: 3}
|
m_Script: {fileID: 11500000, guid: 278639fb612259b4890f4f8615f9305f, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
|
8
Assets/Scripts/ProjectBase/Res.meta
Normal file
8
Assets/Scripts/ProjectBase/Res.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7c531973b0ca77a47b429cce002fa775
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
52
Assets/Scripts/ProjectBase/Res/ResMgr.cs
Normal file
52
Assets/Scripts/ProjectBase/Res/ResMgr.cs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Events;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源加载模块
|
||||||
|
/// 1异步加载,2委托和lambda表达式,3协程,4泛型
|
||||||
|
/// </summary>
|
||||||
|
public class ResMgr : BaseManager<ResMgr>
|
||||||
|
{
|
||||||
|
//同步加载资源
|
||||||
|
public T Load<T>(string name)where T:Object
|
||||||
|
{
|
||||||
|
T res = Resources.Load<T>(name);
|
||||||
|
//如果对象是一个GameObject类型的 把他实例化后返回出去 外部直接使用即可
|
||||||
|
if(res is GameObject)
|
||||||
|
{
|
||||||
|
return GameObject.Instantiate(res);
|
||||||
|
}
|
||||||
|
else//TextAsset AudioClip 不需要实例化,只需要赋值给某些对象
|
||||||
|
{
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//异步加载资源
|
||||||
|
public void LoadAsync<T>(string name, UnityAction<T> callback ) where T : Object
|
||||||
|
{
|
||||||
|
//开启异步加载的协程
|
||||||
|
MonoMgr.GetInstance().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(GameObject.Instantiate(r.asset) as T);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
callback(r.asset as T);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
11
Assets/Scripts/ProjectBase/Res/ResMgr.cs.meta
Normal file
11
Assets/Scripts/ProjectBase/Res/ResMgr.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 684e92a517a01854c8297ba0bb997d97
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -25,13 +25,23 @@ public class Test : MonoBehaviour
|
|||||||
{
|
{
|
||||||
if(Input.GetMouseButtonDown(0))
|
if(Input.GetMouseButtonDown(0))
|
||||||
{
|
{
|
||||||
PoolMgr.GetInstance().GetObj("Test/Cube");
|
// PoolMgr.GetInstance().GetObj("Test/Cube");
|
||||||
|
GameObject obj = ResMgr.GetInstance().Load<GameObject>("Test/Cube");
|
||||||
|
obj.transform.localScale = Vector3.one*2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Input.GetMouseButtonDown(1))
|
if (Input.GetMouseButtonDown(1))
|
||||||
{
|
{
|
||||||
PoolMgr.GetInstance().GetObj("Test/Sphere");
|
// PoolMgr.GetInstance().GetObj("Test/Sphere");
|
||||||
|
ResMgr.GetInstance().LoadAsync<GameObject>("Test/Cube", (obj) => obj.transform.localScale = Vector3.one * 2) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//等异步资源加载完成后,再进入这个函数里面. //放到资源加载的委托参数里面 ResMgr.GetInstance().LoadAsync<GameObject>("Test/Cube", DoSomthing);
|
||||||
|
private void DoSomthing(GameObject obj)
|
||||||
|
{
|
||||||
|
//做一些资源真正加载出来后想做的事情
|
||||||
|
obj.transform.localScale = Vector3.one * 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user