UIToggledObjects.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [AddComponentMenu("NGUI/Interaction/Toggled Objects")]
  5. public class UIToggledObjects : MonoBehaviour
  6. {
  7. private void Awake()
  8. {
  9. if (this.target != null)
  10. {
  11. if (this.activate.Count == 0 && this.deactivate.Count == 0)
  12. {
  13. if (this.inverse)
  14. {
  15. this.deactivate.Add(this.target);
  16. }
  17. else
  18. {
  19. this.activate.Add(this.target);
  20. }
  21. }
  22. else
  23. {
  24. this.target = null;
  25. }
  26. }
  27. UIToggle component = base.GetComponent<UIToggle>();
  28. EventDelegate.Add(component.onChange, new EventDelegate.Callback(this.Toggle));
  29. }
  30. public void Toggle()
  31. {
  32. bool value = UIToggle.current.value;
  33. if (base.enabled)
  34. {
  35. for (int i = 0; i < this.activate.Count; i++)
  36. {
  37. this.Set(this.activate[i], value);
  38. }
  39. for (int j = 0; j < this.deactivate.Count; j++)
  40. {
  41. this.Set(this.deactivate[j], !value);
  42. }
  43. }
  44. }
  45. private void Set(GameObject go, bool state)
  46. {
  47. if (go != null)
  48. {
  49. NGUITools.SetActive(go, state);
  50. }
  51. }
  52. public List<GameObject> activate;
  53. public List<GameObject> deactivate;
  54. [HideInInspector]
  55. [SerializeField]
  56. private GameObject target;
  57. [HideInInspector]
  58. [SerializeField]
  59. private bool inverse;
  60. }