少年修仙传客户端代码仓库
client_Wu Xijin
2018-12-04 31efa47c430874f95f4ff5503372f0cf6ecb53ae
Core/ResModule/MaterialLoader.cs
@@ -1,30 +1,9 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MaterialLoader
{
    public static Material Load(string modelName, string materialName)
    {
        string _bundleName = StringUtility.Contact(ResourcesPath.MOB_FOLDER_NAME,
                                                   ResourcesPath.MOB_SUFFIX,
                                                   modelName);
        string _assetName = materialName;
        AssetInfo _assetInfo = new AssetInfo(_bundleName, _assetName);
        Material _controller = AssetBundleUtility.Instance.Sync_LoadAsset(_assetInfo) as Material;
        if (_controller == null)
        {
            DebugEx.LogErrorFormat("MaterialLoader.Load() => 加载不到资源: {0}.", _assetName);
        }
        return _controller;
    }
    readonly static Dictionary<string, ObjectPool<Material>> s_MatDict = new Dictionary<string, ObjectPool<Material>>();
    public static Material LoadClothesMaterial(int id, bool isUI, bool isSuit)
    {
@@ -65,6 +44,19 @@
            return null;
        }
        //Debug.LogFormat("想要加载的材质的名称: {0}", _assetName);
        ObjectPool<Material> _pool = null;
        if (s_MatDict.TryGetValue(_assetName, out _pool))
        {
            if (_pool.inactivedCount > 0)
            {
                //Debug.Log(" |-- 池里有, 从池里取");
                return _pool.Get();
            }
        }
        string _name = _modelResConfig.ResourcesName;
        int _index = _name.IndexOf('/');
        if (_index != -1)
@@ -72,13 +64,15 @@
            _name = _name.Substring(_index + 1);
        }
        var _parentDirName = string.Empty;
        if (_name.Contains("A_Zs"))
        {
            _name = "A_Zs";
            _parentDirName = "A_Zs";
        }
        else if (_name.Contains("A_Fs"))
        {
            _name = "A_Fs";
            _parentDirName = "A_Fs";
        }
        Material _mat = null;
@@ -87,7 +81,7 @@
#if UNITY_EDITOR
            string _resourcePath = StringUtility.Contact(ResourcesPath.ResourcesOutAssetPath,
                                                    "Mob/",
                                                    _name,
                                                    _parentDirName,
                                                    "/Materials/",
                                                    _assetName,
                                                    ".mat");
@@ -96,16 +90,37 @@
        }
        else
        {
            string _bundleName = StringUtility.Contact(ResourcesPath.MOB_FOLDER_NAME, _name.ToLower(), "_materials");
            string _bundleName = StringUtility.Contact(ResourcesPath.MOB_FOLDER_NAME, InstanceResourcesLoader.raceSuffix, _name.ToLower());
            AssetInfo _assetInfo = new AssetInfo(_bundleName, _assetName);
            _mat = AssetBundleUtility.Instance.Sync_LoadAsset(_assetInfo) as Material;
        }
        if (_mat == null)
        {
            DebugEx.LogErrorFormat("MaterialLoader.Load() => 加载不到资源: {0}.", _assetName);
            DebugEx.LogErrorFormat("MaterialLoader.Load() => 加载不到资源: {0}", _assetName);
        }
        return _mat;
    }
    public static void Release(Material mat)
    {
        if (!mat)
        {
            return;
        }
        var _name = mat.name.Replace(" (Instance)", "");
        //Debug.LogFormat("要释放的材质的名称: {0}, 修改后的名称: {1}", mat.name, _name);
        ObjectPool<Material> _pool = null;
        if (!s_MatDict.TryGetValue(_name, out _pool))
        {
            _pool = new ObjectPool<Material>(null, null);
            s_MatDict.Add(_name, _pool);
        }
        _pool.Add(mat);
    }
}