MobileControlRig.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. {
  11. // this script enables or disables the child objects of a control rig
  12. // depending on whether the USE_MOBILE_INPUT define is declared.
  13. // This define is set or unset by a menu item that is included with
  14. // the Cross Platform Input package.
  15. #if !UNITY_EDITOR
  16. void OnEnable()
  17. {
  18. CheckEnableControlRig();
  19. }
  20. #endif
  21. private void Start()
  22. {
  23. #if UNITY_EDITOR
  24. if (Application.isPlaying) //if in the editor, need to check if we are playing, as start is also called just after exiting play
  25. #endif
  26. {
  27. UnityEngine.EventSystems.EventSystem system = GameObject.FindObjectOfType<UnityEngine.EventSystems.EventSystem>();
  28. if (system == null)
  29. {//the scene have no event system, spawn one
  30. GameObject o = new GameObject("EventSystem");
  31. o.AddComponent<UnityEngine.EventSystems.EventSystem>();
  32. o.AddComponent<UnityEngine.EventSystems.StandaloneInputModule>();
  33. }
  34. }
  35. }
  36. #if UNITY_EDITOR
  37. private void OnEnable()
  38. {
  39. EditorApplication.update += Update;
  40. EditorUserBuildSettings.activeBuildTargetChanged += Update;
  41. }
  42. private void OnDisable()
  43. {
  44. EditorApplication.update -= Update;
  45. EditorUserBuildSettings.activeBuildTargetChanged -= Update;
  46. }
  47. private void Update()
  48. {
  49. CheckEnableControlRig();
  50. }
  51. #endif
  52. private void CheckEnableControlRig()
  53. {
  54. #if MOBILE_INPUT
  55. EnableControlRig(true);
  56. #else
  57. EnableControlRig(false);
  58. #endif
  59. }
  60. private void EnableControlRig(bool enabled)
  61. {
  62. foreach (Transform t in transform)
  63. {
  64. t.gameObject.SetActive(enabled);
  65. }
  66. }
  67. }
  68. }