SCToggleGroup3D.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. namespace SC.XR.Unity
  7. {
  8. public class SCToggleGroup3D : MonoBehaviour
  9. {
  10. [SerializeField] private bool m_AllowSwitchOff = false;
  11. public bool allowSwitchOff { get { return m_AllowSwitchOff; } set { m_AllowSwitchOff = value; } }
  12. private List<SCToggleBase> m_Toggles = new List<SCToggleBase>();
  13. protected SCToggleGroup3D()
  14. { }
  15. private void ValidateToggleIsInGroup(SCToggleBase toggle)
  16. {
  17. if (toggle == null || !m_Toggles.Contains(toggle))
  18. throw new ArgumentException(string.Format("Toggle {0} is not part of ToggleGroup {1}", new object[] { toggle, this }));
  19. }
  20. public void NotifyToggleOn(SCToggleBase toggle)
  21. {
  22. ValidateToggleIsInGroup(toggle);
  23. // disable all toggles in the group
  24. for (var i = 0; i < m_Toggles.Count; i++)
  25. {
  26. if (m_Toggles[i] == toggle)
  27. continue;
  28. m_Toggles[i].isOn = false;
  29. }
  30. }
  31. public void UnregisterToggle(SCToggleBase toggle)
  32. {
  33. if (m_Toggles.Contains(toggle))
  34. m_Toggles.Remove(toggle);
  35. }
  36. public void RegisterToggle(SCToggleBase toggle)
  37. {
  38. if (!m_Toggles.Contains(toggle))
  39. m_Toggles.Add(toggle);
  40. }
  41. public bool AnyTogglesOn()
  42. {
  43. return m_Toggles.Find(x => x.isOn) != null;
  44. }
  45. public IEnumerable<SCToggleBase> ActiveToggles()
  46. {
  47. return m_Toggles.Where(x => x.isOn);
  48. }
  49. public void SetAllTogglesOff()
  50. {
  51. bool oldAllowSwitchOff = m_AllowSwitchOff;
  52. m_AllowSwitchOff = true;
  53. for (var i = 0; i < m_Toggles.Count; i++)
  54. m_Toggles[i].isOn = false;
  55. m_AllowSwitchOff = oldAllowSwitchOff;
  56. }
  57. }
  58. }