【常用】加载配置文件管理资源路径
生活随笔
收集整理的這篇文章主要介紹了
【常用】加载配置文件管理资源路径
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;/// <summary>
/// 資源管理器:管理資源路徑
/// </summary>
public static class ResourceManager
{private static Dictionary<string, string> map;//類第一次加載時執(zhí)行static ResourceManager(){LoadMap();}private static void LoadMap(){//加載資源配置文件string txtMap = Resources.Load<TextAsset>("ResourcesConfig").text;//文本文件轉換為數(shù)據(jù)結構(字典) map = new Dictionary<string, string>();using (StringReader reader = new StringReader(txtMap)){//selected=FX/Player/selectedstring line;//讀取一行,如果不為空則添加到字典while ((line = reader.ReadLine()) != null){string[] keyValue = line.Split('=');map.Add(keyValue[0], keyValue[1]);}}}public static string GetPath(string resName){//根據(jù)鍵(資源名稱)讀取值(路徑)return map.ContainsKey(resName) ? map[resName] : null;}/// <summary>/// 加載配置文件中指定的資源/// </summary>/// <typeparam name="T">需要加載的資源類型</typeparam>/// <param name="resName">資源名稱</param>/// <returns></returns>public static T Load<T>(string resName) where T : Object{//查找路徑string path = GetPath(resName);//加載數(shù)據(jù)return Resources.Load<T>(path);}
}
?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的【常用】加载配置文件管理资源路径的全部內容,希望文章能夠幫你解決所遇到的問題。