原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1130/217.html
Unity客户端架构-Resource
继续Unity客户端架构系列的介绍,上一篇中给大家介绍了io的使用,这一篇我们常州手机游戏开发设计-幻天网络带大家就来看看Resource的使用。
using UnityEngine;
using System.Collections;
using System.IO;
using System;
public class Resource
{
private static Hashtable texts = new Hashtable();
private static Hashtable images = new Hashtable();
private static Hashtable prefabs = new Hashtable();
public static GameObject LoadPrefab(string path)
{
object obj = Resource.prefabs[path];
if (obj == null)
{
Resource.prefabs.Remove(path);
GameObject gameObject = (GameObject)Resources.LoadAssetAtPath(path, typeof(GameObject));
Resource.prefabs.Add(path, gameObject);
return gameObject;
}
return obj as GameObject;
}
public static string LoadTextFile(string path, string ext)
{
object obj = Resource.texts[path];
if (obj == null)
{
Resource.texts.Remove(path);
string text = string.Empty;
#if UINTY_EDITOR
string pathstr = Util.GetDataDir() + "/StreamingAssets/" + path + ext;
#else
string pathstr = Util.AppContentDataUri + path + ext;
#endif
text = File.ReadAllText(pathstr);
Resource.texts.Add(pathstr, ext);
return text;
}
return obj as string;
}
public static Texture2D LoadTexture(string path)
{
object obj = Resource.images[path];
if(obj == null)
{
Resource.images.Remove(path);
Texture2D texture2D = (Texture2D)Resources.Load(path, typeof(Texture2D));
Resource.images.Add(path,texture2D);
return texture2D;
}
return obj as Texture2D;
}
}
上篇:上一篇:Unity客户端架构-io
下篇:下一篇:Unity客户端架构-PanelManager