首页 > 其他 > 详细

Unity3D里Resource方法的二次封装

时间:2014-02-21 21:46:25      阅读:599      评论:0      收藏:0      [点我收藏+]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 *
 *          改编后的unity3D Resources 类,使用起来更加方便,省略了在代码中拆箱装箱的操作
 *         
 *         
 *
 * */
 
using UnityEngine;
 
public class ResourcesEx
{
    private string path;
 
    public ResourcesEx(string path)
    {
        if (path.Length == 0)
        {
            this.path = string.Empty;
        }
        else
        {
            this.path = path;
        }
    }
 
    public T[] LoadAll<T>() where T : Object
    {
        Object[] obj = Resources.LoadAll(this.path, typeof(T));
 
        T[] TLoaded = new T[obj.Length];
        obj.CopyTo(TLoaded, 0);
 
        return TLoaded;
    }
 
    public T[] LoadAll<T>(string dirname) where T : Object
    {
        Object[] obj = Resources.LoadAll(this.path + "/" + dirname, typeof(T));
 
        T[] TLoaded = new T[obj.Length];
        obj.CopyTo(TLoaded, 0);
 
        return TLoaded;
    }
 
    public Texture GetTexture(string textureName)
    {
        Object obj = Resources.Load(this.path + "/" + textureName, typeof(Texture));
        if (obj)
        {
            return obj as Texture;
        }
        else
        {
            Debug.LogError("["+ this.path + textureName + "] Non Exist");
            return new Texture();
        }
    }
 
    public Texture2D GetTexture2D(string textureName)
    {
        Object obj = Resources.Load(this.path + "/" + textureName, typeof(Texture2D));
        if (obj)
        {
            return obj as Texture2D;
        }
        else
        {
            Debug.LogError("["+ this.path + textureName + "] Non Exist");
            return new Texture2D(1, 1);
        }
    }
 
    public T GetObject<T>(string objectName) where T : Object
    {
        Object obj = Resources.Load(this.path + "/" + objectName, typeof(T));
        if (obj)
        {
            return obj as T;
        }
        else
        {
            Debug.LogError("["+ this.path + objectName + "] Non Exist");
            return new Object() as T;
        }
    }
}

  

Unity3D里Resource方法的二次封装

原文:http://www.cnblogs.com/vital/p/3559093.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!