1 using System;
2 #
if UNITY_EDITOR
3 using
UnityEditor;
4 #endif

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


this script enables or disables the child objects of a control rig

depending on whether the USE_MOBILE_INPUT define is declared.

This define is set or unset by a menu item that is included with

the Cross Platform Input package.

if (Application.isPlaying) if in the editor, need to check if we are playing, as start is also called just after exiting play

{the scene have no event system, spawn one



Gõ tìm kiếm nhanh...