[Unity基础] 资源映射表(代码形成)

根据资源名称获取资源

GenerateResConfig类

在这里插入代码片
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

/*
1. 编译器代码:继承自Editor类,只需要在Unity编译器中执行的代码。
2. 菜单项 特性 [MenuItem("……")]:用于修饰需要在Unity编译器中产生菜单按钮的方法。
3. AssetDatabase:包含了只适用于编译器中操作资源的相关功能。
4. StreamingAssets:Unity特殊目录之一,该目录中的文件不会被压缩,适合在移动端读取资源(在PC端还可以写入)。
    持久化路径 Application.persistentDataPath 可以在运行时进行读写操作,Unity外部目录(安装程序时才产生)。
*/


/// <summary>
/// 生成配置文件类
/// </summary>
public class GenerateResConfig : Editor
{
    //菜单下生成子项目
    [MenuItem("Tools/Resources/Generate ResConfig File")]
    public static void Generate()
    {
        //生成资源配置文件
        //1.查找Resouces目录下所有预制件完整的路径
        string[] resFiles = AssetDatabase.FindAssets("t:prefab",new string[] {"Assets/Resources"});
        //2.GUID编码转换成路径
        for (int i = 0; i < resFiles.Length; i++)
        {
            resFiles[i] = AssetDatabase.GUIDToAssetPath(resFiles[i]);
            //Assets/Resource/Skills/BaseMEleeAttackSkill.prefab
            //3.生成对应关系(名称=路径)
            string fileName = Path.GetFileNameWithoutExtension(resFiles[i]);
            string filePath = resFiles[i].Replace("Assets/Resource/",string.Empty).Replace(".prefab",string.Empty);
            resFiles[i] = fileName + "=" + filePath;
        }

        //4.写入文件
        File.WriteAllLines("Assets/SteamingAssets/ConfigMap.txt",resFiles);
        //5.刷新
        AssetDatabase.Refresh();
    }


}

ResourceManager类

在这里插入代码片
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;


namespace Common
{
    //namespace 域名.项目名称.模块

    /// <summary>
    /// 资源管理器
    /// </summary>
    public class ResourceManager
    {
        //配置字典,存储资源名称和其对应关系的完整路径
        private static Dictionary<string,string> configMap;

        //1.构造函数
        //2.作用:初始化类的静态数据成员
        //3.时机:类被加载时自动首先执行一次
        //4.功能:加载文件,解析文件
        static ResourceManager()
        {
            //new一个,分配内存空间
            configMap = new Dictionary<string, string>();

            //加载文件,使用配置文件读取器
            string fileContent= ConfigurationReader.GetConfigFile("ConfigMap.txt"); ;

            //解析文件string---> Dictionary<string,string>
            //BuildMap(fileContent)
            ConfigurationReader.Reader(fileContent,BuildMap);
        }

        //拆分,并加入配置字典
        private static void BuildMap(string line)
        {
            string[] keyValue = line.Split('=');
            configMap.Add(keyValue[0], keyValue[1]);
        }


        //private static void BuildMap(string fileContent)
        //{
        //    configMap = new Dictionary<string, string>();
        //    //文件名=路径\r\n文件名=路径
        //    //StringReader 字符串读取器,提供了逐行读取字符串功能
        //    using (StringReader reader = new StringReader(fileContent))
        //    {
        //        string line;
        //        //1.读一行,满足条件则解析
        //        while ((line =reader.ReadLine()) != null)
        //        {
        //            //解析行数据
        //            string[] keyValue = line.Split('=');
        //            //文件名  keyValue[0]   路径 keyValue[1]   
        //            configMap.Add(keyValue[0], keyValue[1]); 
        //        }

        //        //1先读一行
        //        //string line = reader.ReadLine();
        //        //2不为空则解析/4再判断条件
        //        //while (line !=null)
        //        //{ 
        //        //    string[] keyValue = line.Split('=');
        //        //    //文件名  keyValue[0]   路径 keyValue[1]   
        //        //    configMap.Add(keyValue[0], keyValue[1]);
        //        //3再读一行
        //        //    line = reader.ReadLine();
        //        //}
        //    }//当程序退出using代码块,将自动调用 reader.Dispose() 方法
        //}



        //读取文件
        public static T Load<T>(string prefabName) where T: UnityEngine.Object
        {
            //prefabName---> prefabPath
            string prefabPath = configMap[prefabName];
            return Resources.Load<T>(prefabPath);
        }
    }
}

ConfigurationReader类

在这里插入代码片
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace Common
{
    //namespace 域名.项目名称.模块

    /// <summary>
    /// 配置文件读取器
    /// </summary>
    public class ConfigurationReader
    {
        /// <summary>
        /// 获取配置文件
        /// </summary>
        /// <param name="fileName"></param>
        public static string GetConfigFile(string fileName)
        {
            string url;
            //性能低
            //string url2 = "file://" + Application.streamingAssetsPath + "/" + fileName;
            //如果在编译器 或者 单机中
            //if (Application.platform==RuntimePlatform.Android)

            //应当根据各平台来判断路径
            #region Different Platform
            //Unity宏标签

#if UNITY_EDITOR || UNITY_STANDALONE
            url= "file://" + Application.dataPath + "SteamingAssets/" + fileName;
#elif UNITY_IPHONE
            url= "file://" + Application.dataPath + "/Raw/" + fileName;
#elif UNITY_ANDROID
            url= "jar:file://" + Application.dataPath + "!/assets/" + fileName;
#endif

            #endregion


            WWW www = new WWW(url);
            while (true)
            {
                if (www.isDone)
                {
                    return www.text;
                }
            }

        }

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="fileContent">文件内容</param>
        /// <param name="handler">行处理逻辑(委托)</param>
        public static void Reader(string fileContent , Action<string> handler)
        {
            //StringReader 字符串读取器,提供了逐行读取字符串功能
            using (StringReader reader = new StringReader(fileContent))
            {
                string line;
                while ((line=reader.ReadLine())!=null)
                {
                    handler(line);
                }
            }
        }


    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值