05
2017
10

ResourceManager(四)—— ResourceUnit

目录为:Assets/Scripts/ResourceManager
ResourceUnit.cs

ResourceUnit.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;

//ResourceType
public enum ResourceType
{
    ASSET,
    PREFAB,
    LEVELASSET,
    LEVEL,
}

//这个和AssetUnit挺像的,就是一个资源集合类
//继承Idisposable
//自己实现释放非托管资源,手动进行垃圾回收
public class ResourceUnit: IDisposable
{
    private string mPath;
    //保留了主要的Asset
    private Object mAsset;
    private ResourceType mResourceType;
    private List<ResourceUnit> mNextLevelAssets;
    //保留了AssetBundle
    private AssetBundle mAssetBundle;
    private int mAssetBundleSize;
    private int mReferenceCount;

    //构造函数
    internal ResourceUnit(AssetBundle assetBundle, int assetBundleSize, Object asset, string path, ResourceType resourceType)
    {
        mPath = path;
        mAsset = asset;
        mResourceType = resourceType;
        mNextLevelAssets = new List<ResourceUnit> ();
        mAssetBundle = assetBundle;
        mAssetBundleSize = assetBundleSize;
        mReferenceCount = 0;
    }

    public Object Asset
    {
        get
        {
            return mAsset;
        }

        internal set
        {
            mAsset = value;
        }
    }

    public ResourceType resourceType
    {
        get
        {
            return mResourceType;
        }
    }

    public List<ResourceUnit> NextLevelAssets
    {
        get
        {
            return mNextLevelAssets;
        }

        internal set
        {
            foreach (ResourceUnit asset in value)
            {
                mNextLevelAssets.Add (asset);
            }
        }
    }

    public AssetBundle Assetbundle
    {
        get
        {
            return mAssetBundle;
        }

        set
        {
            mAssetBundle = value;
        }
    }

    public int AssetBundleSize
    {
        get
        {
            return mAssetBundleSize;
        }
    }

    public int ReferenceCount
    {
        get
        {
            return mReferenceCount;
        }
    }

    //输出NextLevel的Assets信息
    public void dumpNextLevel()
    {
        string info = mPath + "the mReferenceCount : " + mReferenceCount + "\n";
        foreach (ResourceUnit ru in mNextLevelAssets)
        {
            ru.dumpNextLevel ();
            info += ru.mPath + "\n";
        }

        Debug.Log (info, ResourceCommon.DEBUGTYPENAME);
    }

    //本身RefCount增加,下一级也要递归增加
    public void addReferencCount()
    {
        ++mReferenceCount;
        foreach (ResourceUnit asset in mNextLevelAssets)
        {
            asset.addReferencCount ();
        }
    }

    //减少ReferenceCount,下一级也要递归减少
    public void reduceReferenceCount()
    {
        --mReferenceCount;
        foreach (ResourceUnit asset in mNextLevelAssets)
        {
            asset.reduceReferenceCount ();
        }

        if (isCanDestroy())
        {
            Dispose ();
        }
    }

    //ReferenceCount为0时就可以释放掉了
    public bool isCanDestroy()
    {
        return (0 == mReferenceCount);
    }

    //释放资源
    public void Dispose()
    {
        ResourceCommon.Log ("Destroy" + mPath);

        if (null != mAssetBundle)
        {
            //mAssetBundle.Unload(true);
            mAssetBundle = null;
        }

        mNextLevelAssets.Clear ();
        mAsset = null;
    }
}
上一篇:Android应用层通信机制 下一篇:React Native项目结构