[英]Checking if all objects of an array is active at the same time Unity C#
[英]OnMouseEnter function prints all the logs at the same time (Unity and C#)
我目前正在 Unity 中开发游戏,但遇到以下问题:
我有一个游戏对象(一个面板),在这个面板上我有多个 TextMeshProUGUI 显示“保存”、“加载”、“选项”和“退出”。 我想这样做,当玩家将鼠标悬停在这些对象之一上时,字体颜色会发生变化或发光会上升。 但是,我无法掌握如何真正实现它。 每当我开始游戏时,甚至在我将鼠标悬停在对象上之前,控制台都会打印所有日志。 当我之后这样做时,不再打印日志。
到目前为止,我有以下代码:
public class OptionsHoverSkript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
OnMouseEnter();
OnMouseExit();
}
// Update is called once per frame
void Update()
{
}
private void OnMouseEnter()
{
// Get the game object.
GameObject[] initialColorText = GameObject.FindGameObjectsWithTag("HoverText");
foreach (var texts in initialColorText)
{
// Get the TextMeschProUGUI component from the object.
TextMeshProUGUI newColorText = texts.GetComponent<TextMeshProUGUI>();
string anotherText = newColorText.text;
if (anotherText == "Save")
{
Debug.Log("Log1111111111111");
}
else if (anotherText == "Load")
{
Debug.Log("Log222222222");
}
}
// Make it glow.
// newColorText.fontSharedMaterial.SetColor(ShaderUtilities.ID_GlowColor, new Color32(215, 127, 60, 255));
}
private void OnMouseExit()
{
Debug.Log("ARGH / ANGRY ARNOLD VOICE!!");
}
这里有两个问题。 首先是评论中提到的那个。 您不会像评论中的其他人所说的那样手动调用OnMouseEnter()
。
但是既然你说在我们可以假设你使用的是新的输入系统而不是旧的输入管理器快速入门指南之前它没有被调用
将来您应该提供更多详细信息,以便人们为您提供答案。 了解 Unity 版本和使用的包很重要。
如果我的假设是正确的,按照以下方式更改您的代码应该会产生所需的结果。
public class OptionsHoverSkript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public void OnPointerEnter(PointerEventData eventData)
{
// Get the game object.
GameObject[] initialColorText = GameObject.FindGameObjectsWithTag("HoverText");
foreach (var texts in initialColorText)
{
// Get the TextMeschProUGUI component from the object.
TextMeshProUGUI newColorText = texts.GetComponent<TextMeshProUGUI>();
string anotherText = newColorText.text;
if (anotherText == "Save")
{
Debug.Log("Log1111111111111");
}
else if (anotherText == "Load")
{
Debug.Log("Log222222222");
}
}
// Make it glow.
// newColorText.fontSharedMaterial.SetColor(ShaderUtilities.ID_GlowColor, new Color32(215, 127, 60, 255));
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("ARGH / ANGRY ARNOLD VOICE!!");
}
可以在这个非常详细的答案中找到更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.