温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

[Unity] 文件夹图像资源的读取

发布时间:2020-06-28 10:19:15 来源:网络 阅读:359 作者:蓬莱仙羽 栏目:开发技术

[Unity] 文件夹图像资源的读取

注意文件以及文件夹必须寄宿在Resources目录下,才能顺利调用Resources.Load()和Resources.loadAll()这两个函数得到所需要的图像文件。

 

 

public class GUITest : MonoBehaviour {

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }


    private Texture2D texSingle;
    private Texture2D[] texAll;

    void OnGUI()
    {

        if (GUI.Button(new Rect(0,10,100,50),"加载一张贴图"))
        {
            if (texSingle==null)
            {
                texSingle = Resources.Load("single/0") as Texture2D;   //这里不需要加后缀
            }
           
        }
        if (GUI.Button(new Rect(0,130,100,50),"加载一组贴图"))
        {
            if (texAll==null)
            {                
                var textures = Resources.LoadAll("textures");
                int countAll=textures.Length;
                texAll=new Texture2D[countAll];
                for (int i = 0; i < countAll; i++)
                {
                    texAll[i] = textures[i] as Texture2D;
                }

            }
        }

        //绘制贴图
        if (texSingle!=null)
        {
            GUI.DrawTexture(new Rect(110,10,80,80),texSingle,ScaleMode.ScaleToFit,true,0);
        }
        if (texAll!=null)
        {
            int countOfAll = texAll.Length;
            for (int i = 0; i < countOfAll; i++)
            {
                GUI.DrawTexture(new Rect(110+i*80,130,80,80),texAll[i],ScaleMode.ScaleToFit,true,0);
            }
        }
    }
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

un ty
AI