简体   繁体   English

OnMouseEnter function 同时打印所有日志(Unity和C#)

[英]OnMouseEnter function prints all the logs at the same time (Unity and C#)

I am currently working on a game in Unity and I have the following problem:我目前正在 Unity 中开发游戏,但遇到以下问题:

I have a gameobject (a Panel) and on this panel I have multiple TextMeshProUGUI displaying " Save ", " Load ", " Options " and " Quit ".我有一个游戏对象(一个面板),在这个面板上我有多个 TextMeshProUGUI 显示“保存”、“加载”、“选项”和“退出”。 I want to make it so, that when the player hovers with the mouse over one of these objects, the fontcolor changes or the glow goes up.我想这样做,当玩家将鼠标悬停在这些对象之一上时,字体颜色会发生变化或发光会上升。 However, I am unable to get a hold of how to actually make it happen.但是,我无法掌握如何真正实现它。 Whenever I start the game the console prints all the logs even bevore I have hovered over the objects.每当我开始游戏时,甚至在我将鼠标悬停在对象上之前,控制台都会打印所有日志。 And when I do it afterwards, the logs are not printed anymore.当我之后这样做时,不再打印日志。

So far I have the following code:到目前为止,我有以下代码:

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!!");
    }

There are two problems here.这里有两个问题。 First the one mentioned in the comments.首先是评论中提到的那个。 You do not manually call OnMouseEnter() like others in the comments have said.您不会像评论中的其他人所说的那样手动调用OnMouseEnter()

But since you said it was not being called before we can assume you are using the new Input System not the older Input Manager Quick start guide但是既然你说在我们可以假设你使用的是新的输入系统而不是旧的输入管理器快速入门指南之前它没有被调用

In the future you should provide more details for people to provide you with an answers.将来您应该提供更多详细信息,以便人们为您提供答案。 The Unity Version and used packages are important to know.了解 Unity 版本和使用的包很重要。

If my assumption is correct changing your code in the following way should produce the desired result.如果我的假设是正确的,按照以下方式更改您的代码应该会产生所需的结果。

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!!");
    }

More detail can be found in this very detailed answer .可以在这个非常详细的答案中找到更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM