PlatformSpecificContent.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using UnityEngine;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. #pragma warning disable 618, 649
  7. namespace UnityStandardAssets.Utility
  8. {
  9. #if UNITY_EDITOR
  10. [ExecuteInEditMode]
  11. #endif
  12. public class PlatformSpecificContent : MonoBehaviour
  13. #if UNITY_EDITOR
  14. , UnityEditor.Build.IActiveBuildTargetChanged
  15. #endif
  16. {
  17. private enum BuildTargetGroup
  18. {
  19. Standalone,
  20. Mobile
  21. }
  22. [SerializeField]
  23. private BuildTargetGroup m_BuildTargetGroup;
  24. [SerializeField]
  25. private GameObject[] m_Content = new GameObject[0];
  26. [SerializeField]
  27. private MonoBehaviour[] m_MonoBehaviours = new MonoBehaviour[0];
  28. [SerializeField]
  29. private bool m_ChildrenOfThisObject;
  30. #if !UNITY_EDITOR
  31. void OnEnable()
  32. {
  33. CheckEnableContent();
  34. }
  35. #else
  36. public int callbackOrder
  37. {
  38. get
  39. {
  40. return 1;
  41. }
  42. }
  43. #endif
  44. #if UNITY_EDITOR
  45. private void OnEnable()
  46. {
  47. EditorApplication.update += Update;
  48. }
  49. private void OnDisable()
  50. {
  51. EditorApplication.update -= Update;
  52. }
  53. public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget)
  54. {
  55. CheckEnableContent();
  56. }
  57. private void Update()
  58. {
  59. CheckEnableContent();
  60. }
  61. #endif
  62. private void CheckEnableContent()
  63. {
  64. #if (UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8 || UNITY_TIZEN)
  65. if (m_BuildTargetGroup == BuildTargetGroup.Mobile)
  66. {
  67. EnableContent(true);
  68. } else {
  69. EnableContent(false);
  70. }
  71. #endif
  72. #if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8 || UNITY_TIZEN)
  73. if (m_BuildTargetGroup == BuildTargetGroup.Mobile)
  74. {
  75. EnableContent(false);
  76. }
  77. else
  78. {
  79. EnableContent(true);
  80. }
  81. #endif
  82. }
  83. private void EnableContent(bool enabled)
  84. {
  85. if (m_Content.Length > 0)
  86. {
  87. foreach (var g in m_Content)
  88. {
  89. if (g != null)
  90. {
  91. g.SetActive(enabled);
  92. }
  93. }
  94. }
  95. if (m_ChildrenOfThisObject)
  96. {
  97. foreach (Transform t in transform)
  98. {
  99. t.gameObject.SetActive(enabled);
  100. }
  101. }
  102. if (m_MonoBehaviours.Length > 0)
  103. {
  104. foreach (var monoBehaviour in m_MonoBehaviours)
  105. {
  106. monoBehaviour.enabled = enabled;
  107. }
  108. }
  109. }
  110. }
  111. }