You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.4 KiB

  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. {
  13. private enum BuildTargetGroup
  14. {
  15. Standalone,
  16. Mobile
  17. }
  18. [SerializeField] private BuildTargetGroup m_BuildTargetGroup;
  19. [SerializeField] private GameObject[] m_Content = new GameObject[0];
  20. [SerializeField] private MonoBehaviour[] m_MonoBehaviours = new MonoBehaviour[0];
  21. [SerializeField] private bool m_ChildrenOfThisObject;
  22. #if !UNITY_EDITOR
  23. void OnEnable()
  24. {
  25. CheckEnableContent();
  26. }
  27. #endif
  28. #if UNITY_EDITOR
  29. private void OnEnable()
  30. {
  31. EditorUserBuildSettings.activeBuildTargetChanged += Update;
  32. EditorApplication.update += Update;
  33. }
  34. private void OnDisable()
  35. {
  36. EditorUserBuildSettings.activeBuildTargetChanged -= Update;
  37. EditorApplication.update -= Update;
  38. }
  39. private void Update()
  40. {
  41. CheckEnableContent();
  42. }
  43. #endif
  44. private void CheckEnableContent()
  45. {
  46. #if (UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8 || UNITY_BLACKBERRY )
  47. if (m_BuildTargetGroup == BuildTargetGroup.Mobile)
  48. {
  49. EnableContent(true);
  50. } else {
  51. EnableContent(false);
  52. }
  53. #endif
  54. #if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8 || UNITY_BLACKBERRY )
  55. if (m_BuildTargetGroup == BuildTargetGroup.Mobile)
  56. {
  57. EnableContent(false);
  58. }
  59. else
  60. {
  61. EnableContent(true);
  62. }
  63. #endif
  64. }
  65. private void EnableContent(bool enabled)
  66. {
  67. if (m_Content.Length > 0)
  68. {
  69. foreach (var g in m_Content)
  70. {
  71. if (g != null)
  72. {
  73. g.SetActive(enabled);
  74. }
  75. }
  76. }
  77. if (m_ChildrenOfThisObject)
  78. {
  79. foreach (Transform t in transform)
  80. {
  81. t.gameObject.SetActive(enabled);
  82. }
  83. }
  84. if (m_MonoBehaviours.Length > 0)
  85. {
  86. foreach (var monoBehaviour in m_MonoBehaviours)
  87. {
  88. monoBehaviour.enabled = enabled;
  89. }
  90. }
  91. }
  92. }
  93. }