Tanoda
InternalUtility.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections.Generic;
11using UnityEngine;
12using UnityEngine.SceneManagement;
13#if UNITY_EDITOR
14using UnityEditor;
15#endif
16
17namespace Leap.Unity {
18
19#if UNITY_EDITOR
20 [InitializeOnLoad]
21#endif
22 public static class InternalUtility {
23
24#if UNITY_EDITOR
25 public static Action OnAnySave;
26
27 private static List<UnityEngine.Object> toDestroy = new List<UnityEngine.Object>();
28 private static List<InvokeStruct> _invokeList = new List<InvokeStruct>();
29
30 static InternalUtility() {
31 //EditorApplication.update += destroyLoop;
32 }
33
34 public static bool IsPrefab(Component component) {
35 return Utils.IsObjectPartOfPrefabAsset(component.gameObject);
36 }
37
44 public static void InvokeIfUserDestroyed(Action action) {
45 if (EditorApplication.isPlayingOrWillChangePlaymode ||
46 EditorApplication.isPlaying ||
47 EditorApplication.isPaused) {
48 return;
49 }
50
51 _invokeList.Add(new InvokeStruct(action));
52 }
53#endif
54
59 public static T AddComponent<T>(GameObject obj) where T : Component {
60#if UNITY_EDITOR
61 if (!Application.isPlaying) {
62 return Undo.AddComponent<T>(obj);
63 } else
64#endif
65 {
66 return obj.AddComponent<T>();
67 }
68 }
69
74 public static Component AddComponent(GameObject obj, Type type) {
75#if UNITY_EDITOR
76 if (!Application.isPlaying) {
77 return Undo.AddComponent(obj, type);
78 } else
79#endif
80 {
81 return obj.AddComponent(type);
82 }
83 }
84
93 public static void Destroy(UnityEngine.Object obj) {
94#if UNITY_EDITOR
95 if (Application.isPlaying) {
96 UnityEngine.Object.Destroy(obj);
97 } else {
98 toDestroy.Add(obj);
99 }
100#else
101 UnityEngine.Object.Destroy(obj);
102#endif
103 }
104
105#if UNITY_EDITOR
106 private static void destroyLoop() {
107 if (_invokeList.Count != 0) {
108 var scene = SceneManager.GetActiveScene();
109 foreach (var action in _invokeList) {
110 if (action.scene == scene) {
111 try {
112 action.action();
113 } catch (Exception e) {
114 Debug.LogException(e);
115 }
116 }
117 }
118 _invokeList.Clear();
119 }
120
121 if (toDestroy.Count != 0) {
122 for (int i = 0; i < toDestroy.Count; i++) {
123 var obj = toDestroy[i];
124 if (obj != null) {
125 Undo.DestroyObjectImmediate(obj);
126 }
127 }
128 toDestroy.Clear();
129 }
130 }
131
132 private static void dispatchOnAnySave() {
133 if (OnAnySave != null) {
134 OnAnySave();
135 }
136 }
137
138 private struct InvokeStruct {
139 public Scene scene;
140 public Action action;
141
142 public InvokeStruct(Action action) {
143 this.action = action;
144 scene = SceneManager.GetActiveScene();
145 }
146 }
147
148 public class SaveWatcher : UnityEditor.AssetModificationProcessor {
149 static string[] OnWillSaveAssets(string[] paths) {
150 EditorApplication.delayCall += dispatchOnAnySave;
151 return paths;
152 }
153 }
154#endif
155 }
156}
UnityEngine.Component Component
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19