ResourceManager(二)—— AssetInfoManager

目录为:Assets/Scripts/ResourceManager/
AssetInfoManager.cs
using System;
using System.Xml;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//这里其实是和打包的时候记录的xml信息对应的
//AssetInfo.bytes这个文件
public class AssetInfo
{
    public string mName;            //asset名称
    public int mIndex;              //asset索引
    public int mLevel;              //asset层级
    public int mSize;               //assetbundle所占大小
    public List<int> mDependencies = new List<int> ();  //asset关联的asset索引

    //构造函数
    public AssetInfo()
    {
        mSize = 0;
    }

    //解析AssetInfo信息
    public void Import(XmlElement element)
    {
        mName = element.GetAttribute ("name");
        mIndex = Int32.Parse (element.GetAttribute ("index"));
        mLevel = Int32.Parse (element.GetAttribute ("level"));

        //这里面其实存的是dependency的index,用逗号隔开的
        //看一下AssetInfo.bytes就明白了
        string depencyStr = element.GetAttribute ("depency");
        if (depencyStr != "")
        {
            //分开
            string[] depencyStrList = depencyStr.Split (',');
            //访问每一个depency的index,并添加
            for (int i = 0; i < depencyStrList.Length; i++)
            {
                int depencyIndex = Int32.Parse (depencyStrList [i]);
                mDependencies.Add (depencyIndex);
            }
        }

        mSize = Int32.Parse (element.GetAttribute ("bundlesize"));
    }
}

//AssetInfo Manager
public class AssetInfoManager
{
    //保存名称对应的AssetInfo
    public Dictionary<string, AssetInfo> mNameAssetInfos = new Dictionary<string, AssetInfo>();
    //保存索引对应的AssetInfo
    public Dictionary<int, AssetInfo> mIndexAssetInfos = new Dictionary<int, AssetInfo>();

    //上面两个Dic保存的AssetInfo是一样的,只是一个用string索引,一个用index索引

    //构造函数
    public AssetInfoManager()
    {

    }

    //加载AssetInfo信息
    public void LoadAssetInfo()
    {
        //打开文件
        StreamReader sr = ResourcesManager.OpenText("AssetInfo");
        XmlDocument doc = new XmlDocument ();
        doc.LoadXml (sr.ReadToEnd ());
        XmlElement root = doc.DocumentElement;
        IEnumerator iter = root.GetEnumerator ();
        //遍历每一个element
        while (iter.MoveNext())
        {
            XmlElement child_root = iter.Current as XmlElement;

            //解析AssetInfo信息
            AssetInfo assetInfo = new AssetInfo ();
            //导入信息
            assetInfo.Import (child_root);

            //保存asset信息到列表
            mNameAssetInfos.Add (assetInfo.mName, assetInfo);
            mIndexAssetInfos.Add (assetInfo.mIndex, assetInfo);
        }

        sr.Close ();
    }

    //根据名称获取Asset信息
    public AssetInfo GetAssetInfo(string name)
    {
        if (mNameAssetInfos.ContainsKey(name))
        {
            return mNameAssetInfos [name];
        }
        else
        {
            return null;
        }
    }

    //根据id获取Asset信息
    public AssetInfo GetAssetInfo(int index)
    {
        if (mIndexAssetInfos.ContainsKey(index))
        {
            return mIndexAssetInfos [index];
        }
        else
        {
            return null;
        }
    }

    //获取该Asset包含denpencyAsset的总大小
    public int GetAllAssetSize(AssetInfo assetInfo)
    {
        int totalSize = 0;

        foreach (int index in assetInfo.mDependencies)
        {
            AssetInfo info = GetAssetInfo (index);
            totalSize = totalSize + info.mSize;
        }

        //加上本包大小
        totalSize = totalSize + assetInfo.mSize;

        return totalSize;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值