[英]I have an array in Unity. I want to assign an image based on the gameobject's tag
This is in Unity 2018.4.20f LTS.这是在 Unity 2018.4.20f LTS 中。 I have a lot of 3d gameobjects that I'm storing in an Array.我有很多 3d 游戏对象存储在一个数组中。 I have searched around but can't find a way to do it.我已经四处寻找,但找不到办法。 Right now I have a panel and a bunch of white box images as placeholders.现在我有一个面板和一堆白框图像作为占位符。 When I pick up an object a white box pops up on my canvas panel.当我拿起 object 时,我的 canvas 面板上会弹出一个白框。
What I would like to know is there a way to grab an image and display it in place of the white place holder image based on the gameobject's tag?我想知道有没有一种方法可以根据游戏对象的标签抓取图像并将其显示在白色占位符图像的位置? Like if it is tagged hammer and I have a hammer image display that image?就像它被标记为锤子并且我有一个锤子图像显示该图像?
If so could someone point me in the right direction on how I would accomplish this?如果是这样,有人可以指出我将如何实现这一目标的正确方向吗?
if you have a reference to the image in a c sharp script you can change the sprite being rendered with (sprite variable of newImageSprite)如果您在 c 尖锐脚本中有对图像的引用,您可以更改正在渲染的精灵(newImageSprite 的精灵变量)
imageOne.sprite = newImageSprite;
edit if you have an array of gamobjects如果您有一组游戏对象,请编辑
gameObjectOne.GetComponent<Image>().sprite = newImageSprite;
if (gameObjectOne.tag == "Hammer") {
imageGameObject.GetComponent<Image>().sprite = hammerSprite;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.