ConfigReader(四十四)—— ReadReleaseResourceConfig

本文介绍了一个Unity项目中用于释放指定纹理资源的配置文件及其解析方法。通过读取XML配置文件,该工具能够找到并释放预定义的一系列纹理资源,帮助开发者有效管理游戏内存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录为:Assets/Scripts/ConfigReader/目录下
ReadReleaseResourceConfig.cs

这里主要就是把config配置文件中记录的texture全部都释放掉

对应XML配置文件:
Assets/Resources/Config/ReleaseResource.xml

<ResourceList>
    <Resource type="texture">
        <Tex name ="HalfPhoto"/>
        <Tex name ="UIatlas2"/>
        <Tex name ="UIatlas4"/>
        <Tex name ="UIatlas17"/>
        <Tex name ="UIatlas14"/>
        <Tex name ="UIatlas12"/>
        <Tex name ="UIatlas3"/>
        <Tex name ="UIatlas1"/>
        <Tex name ="PVPload001"/>
        <Tex name ="Wooden Atlas"/>
    </Resource>
</ResourceList>

ReadReleaseResourceConfig.cs

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

//这里主要就是把config配置文件中记录的texture全部都释放掉
//读取需要强制释放的资源信息
class ReadReleaseResourceConfig: Singleton<ReadReleaseResourceConfig>
{
    //需要强制释放的资源
    private Dictionary<string, List<string>> mResList = new Dictionary<string, List<string>> ();

    XmlDocument xmlDoc = null;

    public ReadReleaseResourceConfig()
    {

    }

    //需要强制释放的资源
    public void Init()
    {
        LoadConfig ("Config/ReleaseResource");
    }

    //强制释放资源asset
    public void ForceReleaseResource()
    {
        //残留的贴图资源
        //所有已加载的Texture,disable的也会返回
        Texture[] textures = Resources.FindObjectsOfTypeAll<Texture> ();

        if (textures.Length == 0)
        {
            return;
        }

        foreach (KeyValuePair<string, List<string>> typeList in mResList)
        {
            string type = typeList.Key;

            if (type == "texture")
            {
                List<string> texList = typeList.Value;

                foreach (Texture texture in textures)
                {
                    string textureName = texture.name;

                    //匹配需要强制删除的贴图资源
                    if (texList.Contains(textureName))
                    {
                        Resources.UnloadAsset (texture);
                        //Debug.Log("unload texture asset" + textureName);
                    }
                }
            }
        }
    }

    //读取到mResList中
    private void LoadConfig(string xmlFilePath)
    {
        mResList.Clear ();

        ResourceUnit xmlfileUnit = ResourcesManager.Instance.loadImmediate (xmlFilePath, ResourceType.ASSET);
        TextAsset xmlfile = xmlfileUnit.Asset as TextAsset;

        xmlDoc = new XmlDocument ();
        xmlDoc.LoadXml (xmlfile.text);

        XmlNode resources = xmlDoc.SelectSingleNode ("ResourceList");
        if (resources != null)
        {
            XmlNodeList resourceTypeList = resources.ChildNodes;
            //遍历所有类型资源
            foreach (XmlNode resourceType in resourceTypeList)
            {
                string typeName = resourceType.Attributes ["type"].Value;

                List<string> sList = new List<string> ();

                //遍历所有资源
                foreach (XmlNode res in resourceType)
                {
                    string resName = res.Attributes ["name"].Value;

                    sList.Add (resName);
                }

                mResList.Add (typeName, sList);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值