-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathMouseLock.cs
More file actions
39 lines (36 loc) · 963 Bytes
/
MouseLock.cs
File metadata and controls
39 lines (36 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using UnityEngine;
using UnityEngine.UI;
namespace Unity.NetCode.Samples.Common
{
public class MouseLock : MonoBehaviour
{
bool m_IsLocked;
Toggle m_Toggle;
void Start()
{
#if !UNITY_EDITOR && !UNITY_STANDALONE
gameObject.SetActive(false);
#endif
m_Toggle = GetComponent<Toggle>();
}
void OnDestroy()
{
if (m_IsLocked)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
void LateUpdate()
{
if (Input.GetKeyDown(KeyCode.Escape))
m_Toggle.isOn = !m_Toggle.isOn;
if (m_IsLocked != m_Toggle.isOn)
{
m_IsLocked = m_Toggle.isOn;
Cursor.lockState = m_IsLocked ? CursorLockMode.Locked : CursorLockMode.None;
Cursor.visible = !m_IsLocked;
}
}
}
}