GameObjectActivator.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. namespace TheraBytes.BetterUi
  9. {
  10. #if UNITY_2018_3_OR_NEWER
  11. [ExecuteAlways]
  12. #else
  13. [ExecuteInEditMode]
  14. #endif
  15. [HelpURL("https://documentation.therabytes.de/better-ui/GameObjectActivator.html")]
  16. [AddComponentMenu("Better UI/Helpers/Game Object Activator", 30)]
  17. public class GameObjectActivator : UIBehaviour, IResolutionDependency
  18. {
  19. [Serializable]
  20. public class Settings : IScreenConfigConnection
  21. {
  22. public List<GameObject> ActiveObjects = new List<GameObject>();
  23. public List<GameObject> InactiveObjects = new List<GameObject>();
  24. [SerializeField]
  25. string screenConfigName;
  26. public string ScreenConfigName { get { return screenConfigName; } set { screenConfigName = value; } }
  27. }
  28. [Serializable]
  29. public class SettingsConfigCollection : SizeConfigCollection<Settings> { }
  30. public Settings CurrentSettings { get { return customSettings.GetCurrentItem(settingsFallback); } }
  31. [SerializeField]
  32. Settings settingsFallback = new Settings();
  33. [SerializeField]
  34. SettingsConfigCollection customSettings = new SettingsConfigCollection();
  35. protected override void OnEnable()
  36. {
  37. base.OnEnable();
  38. Apply();
  39. }
  40. public void OnResolutionChanged()
  41. {
  42. Apply();
  43. }
  44. public void Apply()
  45. {
  46. #if UNITY_EDITOR
  47. if (!(EditorPreview) && !(UnityEditor.EditorApplication.isPlaying))
  48. return;
  49. #endif
  50. foreach (GameObject go in CurrentSettings.ActiveObjects)
  51. {
  52. if (go != null)
  53. {
  54. go.SetActive(true);
  55. }
  56. }
  57. foreach (GameObject go in CurrentSettings.InactiveObjects)
  58. {
  59. if (go != null)
  60. {
  61. go.SetActive(false);
  62. }
  63. }
  64. }
  65. #if UNITY_EDITOR
  66. public bool EditorPreview { get; set; }
  67. protected override void OnValidate()
  68. {
  69. base.OnValidate();
  70. if (EditorPreview)
  71. {
  72. Apply();
  73. }
  74. }
  75. #endif
  76. }
  77. }