2020-11-02 Unity ResourcesLoad脚本

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// 
/// 预制体加载类
/// 
public class ResourceLoader : BaseClass
{
    #region Load

    public Object Load(string filePath)
    {
        return Resources.Load(filePath);
    }

    public Object[] LoadAll(string filePath)
    {
        return Resources.LoadAll(filePath);
    }

    public T Load(string filePath) where T : Object
    {
        return Resources.Load(filePath);
    }

    public T LoadAsyn(string filePath,System.Action action) where T : Object
    {
        return Resources.Load(filePath);
    }

    public Dictionary LoadAll(string filePath) where T : Object
    {
        Dictionary result = new Dictionary();

        T[] ts = Resources.LoadAll(filePath);

        if (ts.Length == 0)
        {
#if UNITY_EDITOR
            Debug.LogError("资源加载失败!路径为'Resources/" + filePath + "'!");
#endif
            return null;
        }

        for (int i = 0; i < ts.Length; i++)
        {
            result.Add(ts[i].name, ts[i]);
        }

        return result;
    }

    #endregion

    #region API

    /// 
    /// 同步加载并实例化一个预制体
    /// 
    /// 预制体对应的GameObject
    /// 预制体存储目录
    /// GameObject名称
    public GameObject InstantiatePrefab(string filePath, string name)
    {
        Object prefab = Load("Prefabs/" + filePath);

        if (null == prefab)
        {
#if UNITY_EDITOR
            Debug.LogError("预制体文件未找到,路径为:'Prefabs/" + filePath + "'!");
#endif
            return null;
        }

        GameObject newObj = Object.Instantiate(prefab) as GameObject;
        newObj.name = name;

        return newObj;
    }

    /// 
    /// 同步加载并实例化一个预制体,预制体会被初始化
    /// 
    /// 预制体对应的GameObject
    /// 预制体存储目录
    /// GameObject名称
    /// GameObject父物体
    /// GameObject是否进行一致化
    /// 是否为UIPanel预制体
    public GameObject InstantiateIdentityPrefab(string filePath, string name, Transform parent, bool isUIPanel = false)
    {
        GameObject newObj = InstantiatePrefab(filePath, name);

        if (newObj == null)
        {
            return null;
        }

        Transform newTrans = newObj.transform;

        newTrans.SetParent(parent);

        newTrans.SetAsLastSibling();

        newTrans.localPosition = Vector3.zero;
        newTrans.localRotation = Quaternion.identity;
        newTrans.localScale = Vector3.one;

        if (isUIPanel)
        {
            RectTransform rect = newTrans.GetComponent();
            rect.offsetMin = Vector2.zero;
            rect.offsetMax = Vector2.zero;
        }

        return newObj;
    }

    #endregion


    #region GC

    private bool _GCing = false;

    public void CallGC()
    {
        if (_GCing)
        {
            return;
        }

        Coroutiner.Start(_ResourcesGC());
    }

    private IEnumerator _ResourcesGC()
    {
        _GCing = true;

        yield return null;
        yield return Resources.UnloadUnusedAssets();

        _GCing = false;
    }

    #endregion
}

你可能感兴趣的:(2020-11-02 Unity ResourcesLoad脚本)