PlatformSpecificContent.cs 2.7 KB

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