unity游戏开发入门-Resources动态加载图片

问题:unity的Resources动态加载就不必多说了,这里出现的问题是当我把图片放入Resources文件夹后,使用Resources.Load(datapath)却并没有出现图片。
分析:由于你放在文件夹下的是图片,是Texture2D类型,但是实际用在u3d中的是sprite,所以需要转换类型。
方式:1.在代码中先行创建Texture2D变量,获取外部图片,利用已完成的该变量之后创建sprite
2.直接在unity中将图片转为sprite,然后直接调用(转换sprite方法:点击图片->在Inspector面板更改Texture Type)
更多方式可参考博客https://blog.csdn.net/lmyfx/article/details/78184436

   1.Texture2D img = Resources.Load("photo/" + inputText.text) as Texture2D;
   Sprite texSprite= Sprite.Create(img, new Rect(0, 0, img.width, img.height), new Vector2(0.5f, 0.5f));
   GameObject.FindGameObjectWithTag("Finish").transform.GetChild(0).gameObject.GetComponent<Image>().sprite =texSprite;
   
   2. GameObject.FindGameObjectWithTag("Finish").transform.GetChild(0).gameObject.GetComponent<Image>().sprite 
                = Resources.Load("photo/" + inputText.text, typeof(Sprite)) as Sprite;
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值