unity 商业游戏底层资源管理加载框架——个人学习记录(7)

类对象池

包含创建对象池,取对象池中的内容,回收。

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

public class ClassObjectPool where T : class, new()
{
    /// 
    /// 偏底层的东西,尽量别使用list
    /// 
    protected Stack m_Pool = new Stack();
    /// 
    /// 最大对象个数 小于等于0 表示不限个数
    /// 
    protected int m_MaxCount = 0;

    protected int m_NoRecycleCount = 0;

    public ClassObjectPool(int maxCount)
    {
        m_MaxCount = maxCount;
        for (int i = 0; i < maxCount; i++)
        {
            m_Pool.Push(new T());
        }
    }
    /// 
    /// 从池子里去对象
    /// 
    /// 如果为空是否需要new对象
    /// 
    public T Spawn(bool createPoolEmpty)
    {
        if (m_Pool.Count>0)
        {
            T rtn = m_Pool.Pop();
            if (rtn==null)
            {
                if (createPoolEmpty)
                {
                    rtn = new T();
                }
            }
            m_NoRecycleCount++;
            return rtn;
        }
        else
        {
            if (createPoolEmpty)
            {
                T rtn = new T();
                m_NoRecycleCount ++;
                return rtn;
            }
        }
        return null;
    }
    /// 
    /// 回收类对象
    /// 
    /// 
    /// 
    public bool Recycle(T obj)
    {
        if (obj==null)
        {
            return false;
        }
        m_NoRecycleCount--;
        if (m_Pool.Count>m_MaxCount&&m_MaxCount>=0)
        {
            obj = null;
            return false;
        }
        m_Pool.Push(obj);
        return true;
    }
}
//单例类

public class Singleton where T:new()
{
    private static T m_instance;
    public static T Instance
    {
        get
        {
            if (m_instance==null)
            {
                m_instance = new T();
            }
            return m_instance;
        }
    }
}

对象管理:

因为使用加载AB包的时候可能会频繁创建类,但是new类的时候会产生一定的GC,这会有一定的卡顿,因此提前缓存一些基础类的对象,在其他管理类要使用时从类对象池加载就行了。但是注意类对象池没有提供还原的方法,要自行还原,比如一个类有四个对象,还给类对象池的时候需要自行清空还原成默认值,一般在管理类中做。

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

public class ObjectManager:Singleton
{
    #region 测试
    //public ClassObjectPool testLoadPool = ObjectManager.Instance. GetOrCreateClassPool< TestLoad>(1000);
    //void Test()
    //{
    //   TestLoad temp= testLoadPool.Spawn(true);
    //    testLoadPool.Recycle(temp);
    //}
    #endregion

    #region 类对象池的使用
    protected Dictionary m_ClassPoolDict = new Dictionary();

    /// 
    /// 创建类对象池,创建完以后可以在外边保存ClassObjectPool,然后可以调用Spwan和Recycle来创建或回收对象。
    /// 
    /// 
    /// 
    /// 
    public ClassObjectPool GetOrCreateClassPool (int maxCount) where T :class,new()
    {
        Type type = typeof(T);
        object outObj = null;
        if (!m_ClassPoolDict.TryGetValue(type,out outObj)||outObj==null)
        {
            ClassObjectPool newPool = new ClassObjectPool(maxCount);
            m_ClassPoolDict.Add(type, newPool);
            return newPool;
        }
        return outObj as ClassObjectPool;
    }
    /// 
    /// 从对象池中取对象
    /// 
    /// 
    /// 
    /// 
    public T NewClassObjectFromPool(int maxCount) where T : class, new()
    {
        ClassObjectPool pool= GetOrCreateClassPool(maxCount);
        if (pool==null)
        {
            return null;
        }
        return pool.Spawn(true);
    }
    #endregion


    
}

 Assetbundle管理 :

读配置表,依赖加载管理

 基于Assetbundle的资源管理池(ResourceManager) :

ResourceManager中的资源传到ObjectManager中进行实例化,将对应东西储存在对象池,(不是在创建的时候加入对象池,而是在回收的时候判断是不是频繁使用的资源来决定是否回收到对象池),分为同步加载,异步加载,预加载。

1、频繁使用的资源缓存下来,提高效率。
2、分为同步加载,异步加载,预加载。
    同步加载 :资源太大时容易造成卡顿。
    异步加载 :使用协程+循环,控制每一帧加载的个数,是的加载资源时不那么卡顿。
    预加载: 同步异步针对的是资源,文字Text,声音,视频,未实例化的prefab等不需要实例化的资源都要给上层提供接口。

AssetBundle资源池管理

  1. 读取AssetBundle配置表
  2. 设置中间类进行引用计数
  3. 根据路径加载AssetBundle
  4. 根据路径卸载AssetBundle
  5. 为ResourceManager提供加载中间类,根据中间类释放资源,查找中间类等方法

项目中新建一个空物体,名字为#StartCRT#,挂上以下脚本,用来加载配置文件:

using System.Collections;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

/// 
/// 加载AssetBundleConfig.bytes文件,这个必须在最开始运行
/// 
public class StartCRT : MonoBehaviour
{
    [HideInInspector]
    public byte[] bytes = null;
    [HideInInspector]
    public AssetBundleConfig config = null;
    // Use this for initialization
    void Start()
    {
        StartCoroutine(Load());
    }

    IEnumerator Load()
    {
        string path = Application.streamingAssetsPath + "/AssetBundleConfig.bytes";
        WWW www = new WWW(path);
        yield return www;
        if (www.isDone)
        {
            bytes = www.bytes;
            MemoryStream memoryStream = new MemoryStream(bytes);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            config = (AssetBundleConfig)binaryFormatter.Deserialize(memoryStream);
            memoryStream.Close();
        }
    }
}
using System.Collections.Generic;
using System.IO;
using UnityEngine;


public class AssetBundleManager : Singleton
{
    private StartCRT m_startCRT;//为了开启协成加载bytes文件

    /// 
    /// 资源关系配置表
    /// 可以根据CRC找到对应资源块
    ///                                               
    protected Dictionary m_ResourceItemDict = new Dictionary();
    /// 
    /// 储存已加载的AB包
    /// key为CRC
    /// 
    protected Dictionary m_AssetBundleItemDict = new Dictionary();
    /// 
    /// AssetBundleItem类对象池
    /// 
    protected ClassObjectPool m_AssetBundleItemPool = ObjectManager.Instance.GetOrCreateClassPool(500);
    /// 
    /// 加载AB配置表,返回是否正确加载,在游戏开始场景调用
    /// 
    /// 
    public bool LoadAssetBundleConfig()
    {
        m_ResourceItemDict.Clear();
        m_startCRT = GameObject.Find("#StartCRT#").GetComponent();
        if (m_startCRT.bytes == null)
        {
            Debug.LogError("AssetBundleConfig is not exist!");
            return false;
        }

        for (int i = 0; i < m_startCRT.config.ABList.Count; i++)
        {
            ABBase abBase = m_startCRT.config.ABList[i];
            //使用中间类进行保存
            ResourceItem item = new ResourceItem();
            item.m_Crc = abBase.Crc;
            item.m_AssetName = abBase.AssetName;
            item.m_ABName = abBase.ABName;
            item.m_DependedAssetBundle = abBase.ABDpendece;
            if (m_ResourceItemDict.ContainsKey(item.m_Crc))
            {
                Debug.LogError("重复的CRC,资源名:" + item.m_AssetName + "AB包名:" + item.m_ABName);
            }
            else
            {
                m_ResourceItemDict.Add(item.m_Crc, item);
            }
        }
        return true;
    }
    /// 
    /// 根据路径的CRC加载中间类RecourseItem
    /// 
    /// 
    /// 
    public ResourceItem LoadResourceAssetBundle(uint crc)
    {
        ResourceItem item = null;
        if (!m_ResourceItemDict.TryGetValue(crc, out item) || item == null)
        {
            Debug.Log(string.Format("LoadResourceAssetBundle Error:can not find crc {0} in AssetBundleConfig", crc.ToString()));
            return item;
        }
        if (item.m_AssetBundle != null)
        {
            return item;
        }
        item.m_AssetBundle = LoadAssetBundle(item.m_ABName);
        if (item.m_DependedAssetBundle != null)
        {
            for (int i = 0; i < item.m_DependedAssetBundle.Count; i++)
            {
                LoadAssetBundle(item.m_DependedAssetBundle[i]);
            }
        }
        return item;
    }
    /// 
    /// 根据名字加载单个AssetBundle
    /// 有可能会出现这种情况,A和B都依赖了C资源,加载A和B的话就会加载两次,卸载也会卸载两次
    /// 因此采用引用计数,加载时+1,判断卸载的时候引用计数为0的时候才让卸载
    /// 
    /// 
    /// 
    private AssetBundle LoadAssetBundle(string name)
    {
        AssetBundleItem item = null;
        uint crc = Crc32.GetCrc32(name);
        if (!m_AssetBundleItemDict.TryGetValue(crc, out item))
        {
            AssetBundle ab = null;
            string fullPath = Application.streamingAssetsPath + "/" + name;
            if (File.Exists(fullPath))
            {
                ab = AssetBundle.LoadFromFile(fullPath);
            }
            if (ab == null)
            {
                Debug.LogError("LoadAssetBundle error:" + fullPath);
            }
            item = m_AssetBundleItemPool.Spawn(true);
            item.assetBundle = ab;
            item.refCount++;
            m_AssetBundleItemDict.Add(crc, item);
        }
        else
        {
            item.refCount++;
        }

        return item.assetBundle;
    }
    /// 
    /// 释放资源
    /// 
    /// 
    public void ReleaseAsset(ResourceItem item)
    {
        if (item == null)
        {
            return;
        }
        if (item.m_DependedAssetBundle != null && item.m_DependedAssetBundle.Count >= 0)
        {
            for (int i = 0; i < item.m_DependedAssetBundle.Count; i++)
            {
                UnLoadAssetBundle(item.m_DependedAssetBundle[i]);
            }
        }
        UnLoadAssetBundle(item.m_ABName);
    }
    private void UnLoadAssetBundle(string name)
    {
        AssetBundleItem item = null;
        uint crc = Crc32.GetCrc32(name);
        if (m_AssetBundleItemDict.TryGetValue(crc, out item) && item != null)
        {
            item.refCount--;
            if (item.refCount <= 0 && item.assetBundle != null)
            {
                item.assetBundle.Unload(true);
                item.Reset();
                m_AssetBundleItemPool.Recycle(item);
                m_ResourceItemDict.Remove(crc);
            }
        }
    }
    /// 
    /// 根据crc查找ResourcesItem
    /// 
    /// 
    /// 
    public ResourceItem FindResourceItem(uint crc)
    {
        return m_ResourceItemDict[crc];
    }
}
public class AssetBundleItem
{
    public AssetBundle assetBundle = null;

    public int refCount;

    public void Reset()
    {
        assetBundle = null;
        refCount = 0;
    }
}

public class ResourceItem
{
    /// 
    /// 资源路径CRC
    /// 
    public uint m_Crc = 0;
    /// 
    /// 该资源文件名字
    /// 
    public string m_AssetName = string.Empty;
    /// 
    /// 该资源所在的AssetBundle名字
    /// 
    public string m_ABName = string.Empty;
    /// 
    /// 该资源所依赖的AssetBundle
    /// 
    public List m_DependedAssetBundle = null;
    /// 
    /// 该资源所在的AssetBundle名字
    /// 
    public AssetBundle m_AssetBundle = null;
}

end
 

你可能感兴趣的:(学习记录)