From 81330a6b68d307937c262368a42a3fa4e9ad4207 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 23 May 2024 10:10:23 +0800 Subject: + NavMeshTest --- Other/NavMeshTest/Assets/Scripts/Prop/Switch.cs | 63 +++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Other/NavMeshTest/Assets/Scripts/Prop/Switch.cs (limited to 'Other/NavMeshTest/Assets/Scripts/Prop/Switch.cs') diff --git a/Other/NavMeshTest/Assets/Scripts/Prop/Switch.cs b/Other/NavMeshTest/Assets/Scripts/Prop/Switch.cs new file mode 100644 index 0000000..1312616 --- /dev/null +++ b/Other/NavMeshTest/Assets/Scripts/Prop/Switch.cs @@ -0,0 +1,63 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public abstract class Switchable : MonoBehaviour +{ + public virtual void OnSwitch(bool on) + { } +} + + +public class Switch : MonoBehaviour +{ + public Switchable target; + + public Transform bar; + + public Vector2 rotationRange; + + private bool m_IsOn; + + private void Start() + { + m_IsOn = false; + OnPressed(); + } + + private void Update() + { + if(Input.GetMouseButtonDown(0)) + { + Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); + RaycastHit info; + if (Physics.Raycast(ray, out info)) + { + if(info.collider.gameObject == gameObject) + { + m_IsOn = !m_IsOn; + + OnPressed(); + + if (target != null) + { + target.OnSwitch(m_IsOn); + } + } + } + } + } + + void OnPressed() + { + if(m_IsOn) + { + bar.localRotation = Quaternion.Euler(0, 0, rotationRange.x); + } + else + { + bar.localRotation = Quaternion.Euler(0, 0, rotationRange.y); + } + } + +} \ No newline at end of file -- cgit v1.1-26-g67d0