unity3d 第十一天

来源:互联网 发布:姓名签名设计软件 编辑:程序博客网 时间:2024/05/23 19:32

昨天的改好了 但还是有问题


Texture_text.cs

using UnityEngine;using System.Collections;public class Texture_test : MonoBehaviour {Texture2D texSingle;Object[] texAll;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}void OnGUI(){//Resources.Load 返回的是object    Resources.LoadAll() 返回的是object[];if (GUI.Button (new Rect (0, 10, 100, 50), "加载一张贴图")) {if(texSingle == null){//texSingle = (Resources.Load("3"))as Texture2D;texSingle = (Texture2D)Resources.Load("3");}}if (GUI.Button (new Rect (0, 130, 100, 50), "加载一组贴图")) {if(texAll == null){//加载所有贴图texAll = Resources.LoadAll("Textures");//texAll = Resources.LoadAll("Textures", typeof(Texture2D));}}if (texSingle != null){//绘制一张贴图GUI.DrawTexture(new Rect(110,10,120,120),texSingle,ScaleMode.StretchToFill,true,0);}if (texAll != null) {for (int i =0; i<texAll.Length; i++) {GUI.DrawTexture (new Rect (110 + i * 1, 130, 120, 120), (Texture2D)texAll[i], ScaleMode.StretchToFill, true, 0);}int j = texAll.Length;Debug.Log (j);} else {GUI.Label(new Rect(140,130,100,50),"asd");}}}

加载所有贴图出问题了 texAll.length 始终为 0.

问题解决了  Resources.LoadAll("path")   path 为Resources文件夹下 的目录或者图片名 才能正常载入 

改了之后只显示最后一张是因为 GUI.DrawTexture(new Rect(110+i * 1,130,120,120) 在测试时120 改为了1 ,所以重叠了。


完整代码

using UnityEngine;using System.Collections;public class Texture_test : MonoBehaviour {Texture2D texSingle;Object[] texAll;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}void OnGUI(){//Resources.Load 返回的是object    Resources.LoadAll() 返回的是object[];if (GUI.Button (new Rect (0, 10, 100, 50), "加载一张贴图")) {if(texSingle == null){//texSingle = (Resources.Load("3"))as Texture2D;texSingle = (Texture2D)Resources.Load("3");}}if (GUI.Button (new Rect (0, 130, 100, 50), "加载一组贴图")) {if(texAll == null){//加载所有贴图texAll = Resources.LoadAll("texture");//texAll = Resources.LoadAll("Textures", typeof(Texture2D));}}if (texSingle != null){//绘制一张贴图GUI.DrawTexture(new Rect(110,10,120,120),texSingle,ScaleMode.StretchToFill,true,0);}if (texAll != null) {for (int i =0; i<texAll.Length; i++) {GUI.DrawTexture (new Rect (110 + i * 120, 130, 120, 120), (Texture2D)texAll[i], ScaleMode.StretchToFill, true, 0);}int j = texAll.Length;Debug.Log (j);} else {GUI.Label(new Rect(140,130,100,50),"asd");}}}


0 0
原创粉丝点击