简体   繁体   English

我在 Unity 中有一个数组。 我想根据游戏对象的标签分配图像

[英]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.

相关问题 我想在Unity中制作简单的2D游戏。 我是否全面学习c#? 我没有编程经验 - I want to make simple 2D games in Unity. Do I learn c# in its entirety? I have no programming experience 如何为 Unity 中由 OnTriggerEnter 触发的游戏对象分配新的 transform.position? - How do I assign a new transform.position for a GameObject that's triggered by an OnTriggerEnter in Unity? 使用统一 c# 我想打开或关闭一个包含所有组件的游戏对象 - Using unity c# i want to toggle on or off a GameObject with all of it's components inside 我的 MoveTowards 方法是在 Unity 中传送。 我只希望它慢慢移动。 我该怎么办? - My MoveTowards method is teleporting in Unity. I only want it to move slowly. What do I do? gameobject object 变量未分配,我如何在脚本中统一分配? - gameobject object variable is not assigned, how do i assign this in script in unity? 如何根据运动方向旋转带有 RigidBody 的游戏对象? 团结 | 3D | C# - How do I rotate a GameObject with a RigidBody based on it's movement direction? Unity | 3D | C# 我需要使用 c# 将一个游戏对象旋转到 Unity 中的另一个游戏对象,但我希望旋转仅在 z - I need to rotate a gameObject towards another gameobject in Unity with c#, but i want the rotation to be only in z 我已经遵循了关于为统一的fps游戏制作控件的教程。 控制工作但如果我离开控件然后我继续向左移动 - I have followed a tutorial on making controls for a fps game on unity. the controls work but if I leave the controls then I keep moving to the left 如何指定GameObject的x和y位置? - How do I assign a GameObject's x and y position? C#-Unity使用此代码崩溃-根据GameObject标签创建Transform数组 - C# - Unity crashes with this code - creating Transform array based on GameObject tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM