Unity客户端架构-Resource

发表于2018-09-05
评论0 1.9k浏览
继续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客户端架构系列教程

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引