How to make cursor dissapear after inactivity in Unity

后端 未结 2 1127
鱼传尺愫
鱼传尺愫 2021-01-26 05:13

My goal is simple, like a lot of applications, I want to make my cursor invisible after a certain time of inactivity of my user.

My solution doesn\'t s

2条回答
  •  逝去的感伤
    2021-01-26 05:51

    This should be the good way to achieve the task, using coroutine, without any memory issue:

    private Coroutine co_HideCursor;
    
    void Update()
    {
        if (Input.GetAxis("Mouse X") == 0 && (Input.GetAxis("Mouse Y") == 0))
        {
            if (co_HideCursor == null)
            {
                co_HideCursor = StartCoroutine(HideCursor());
            }
        }
        else
        {
            if (co_HideCursor != null)
            {
                StopCoroutine(co_HideCursor);
                co_HideCursor = null;
                Cursor.visible = true;
            }
        }
    }
    
    private IEnumerator HideCursor()
    {
        yield return new WaitForSeconds(3);
        Cursor.visible = false;
    }
    

提交回复
热议问题