MobileControlRig.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. #endif
  5. using UnityEngine;
  6. namespace UnityStandardAssets.CrossPlatformInput
  7. {
  8. [ExecuteInEditMode]
  9. public class MobileControlRig : MonoBehaviour
  10. #if UNITY_EDITOR
  11. , UnityEditor.Build.IActiveBuildTargetChanged
  12. #endif
  13. {
  14. // this script enables or disables the child objects of a control rig
  15. // depending on whether the USE_MOBILE_INPUT define is declared.
  16. // This define is set or unset by a menu item that is included with
  17. // the Cross Platform Input package.
  18. #if !UNITY_EDITOR
  19. void OnEnable()
  20. {
  21. CheckEnableControlRig();
  22. }
  23. #else
  24. public int callbackOrder
  25. {
  26. get
  27. {
  28. return 1;
  29. }
  30. }
  31. #endif
  32. private void Start()
  33. {
  34. #if UNITY_EDITOR
  35. if (Application.isPlaying) //if in the editor, need to check if we are playing, as start is also called just after exiting play
  36. #endif
  37. {
  38. UnityEngine.EventSystems.EventSystem system = GameObject.FindObjectOfType<UnityEngine.EventSystems.EventSystem>();
  39. if (system == null)
  40. {//the scene have no event system, spawn one
  41. GameObject o = new GameObject("EventSystem");
  42. o.AddComponent<UnityEngine.EventSystems.EventSystem>();
  43. o.AddComponent<UnityEngine.EventSystems.StandaloneInputModule>();
  44. }
  45. }
  46. }
  47. #if UNITY_EDITOR
  48. private void OnEnable()
  49. {
  50. EditorApplication.update += Update;
  51. }
  52. private void OnDisable()
  53. {
  54. EditorApplication.update -= Update;
  55. }
  56. private void Update()
  57. {
  58. CheckEnableControlRig();
  59. }
  60. #endif
  61. private void CheckEnableControlRig()
  62. {
  63. #if MOBILE_INPUT
  64. EnableControlRig(true);
  65. #else
  66. EnableControlRig(false);
  67. #endif
  68. }
  69. private void EnableControlRig(bool enabled)
  70. {
  71. foreach (Transform t in transform)
  72. {
  73. t.gameObject.SetActive(enabled);
  74. }
  75. }
  76. #if UNITY_EDITOR
  77. public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget)
  78. {
  79. CheckEnableControlRig();
  80. }
  81. #endif
  82. }
  83. }