123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using System;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using UnityEngine.Serialization;
- namespace SC.XR.Unity
- {
- public abstract class SCToggleBase : MonoBehaviour, IPointerClickHandler
- {
- [Serializable]
- public class ToggleEvent : UnityEvent<bool>
- {
- }
- [SerializeField]
- private SCToggleGroup3D m_Group;
- public SCToggleGroup3D group
- {
- get { return m_Group; }
- set
- {
- m_Group = value;
- #if UNITY_EDITOR
- if (Application.isPlaying)
- #endif
- {
- SetToggleGroup(m_Group, true);
- PlayEffect();
- }
- }
- }
- public ToggleEvent onValueChanged = new ToggleEvent();
- [FormerlySerializedAs("m_IsActive")]
- [Tooltip("Is the toggle currently on or off?")]
- [SerializeField]
- protected bool m_IsOn;
- protected SCToggleBase()
- { }
- protected virtual void OnEnable()
- {
- SetToggleGroup(m_Group, false);
- PlayEffect();
- }
- protected void OnDisable()
- {
- SetToggleGroup(null, false);
- }
- #if UNITY_EDITOR
- protected void OnValidate()
- {
- PlayEffect();
- }
- #endif // if UNITY_EDITOR
- private void SetToggleGroup(SCToggleGroup3D newGroup, bool setMemberValue)
- {
- SCToggleGroup3D oldGroup = m_Group;
-
-
- if (m_Group != null)
- m_Group.UnregisterToggle(this);
-
-
- if (setMemberValue)
- m_Group = newGroup;
-
- if (newGroup != null)
- newGroup.RegisterToggle(this);
-
-
- if (newGroup != null && newGroup != oldGroup && isOn)
- newGroup.NotifyToggleOn(this);
- }
- public bool isOn
- {
- get { return m_IsOn; }
- set
- {
- Set(value);
- }
- }
- void Set(bool value)
- {
- Set(value, true);
- }
- void Set(bool value, bool sendCallback)
- {
- if (m_IsOn == value)
- return;
-
- m_IsOn = value;
- if (m_Group != null)
- {
- if (m_IsOn || (!m_Group.AnyTogglesOn() && !m_Group.allowSwitchOff))
- {
- m_IsOn = true;
- m_Group.NotifyToggleOn(this);
- }
- }
-
-
-
-
- PlayEffect();
- if (sendCallback)
- {
- UISystemProfilerApi.AddMarker("Toggle.value", this);
- onValueChanged.Invoke(m_IsOn);
- }
- }
- protected abstract void PlayEffect();
- protected void Start()
- {
- PlayEffect();
- }
- private void InternalToggle()
- {
- isOn = !isOn;
- }
-
-
-
- public virtual void OnPointerClick(PointerEventData eventData)
- {
- InternalToggle();
- }
- }
- }
|