10using System.Collections.Generic;
22 public static class InternalUtility {
25 public static Action OnAnySave;
28 private static List<InvokeStruct> _invokeList =
new List<InvokeStruct>();
30 static InternalUtility() {
34 public static bool IsPrefab(
Component component) {
35 return Utils.IsObjectPartOfPrefabAsset(component.gameObject);
44 public static void InvokeIfUserDestroyed(Action action) {
45 if (EditorApplication.isPlayingOrWillChangePlaymode ||
46 EditorApplication.isPlaying ||
47 EditorApplication.isPaused) {
51 _invokeList.Add(
new InvokeStruct(action));
59 public static T AddComponent<T>(GameObject obj) where T :
Component {
61 if (!Application.isPlaying) {
62 return Undo.AddComponent<T>(obj);
66 return obj.AddComponent<T>();
74 public static Component AddComponent(GameObject obj, Type type) {
76 if (!Application.isPlaying) {
77 return Undo.AddComponent(obj, type);
81 return obj.AddComponent(type);
93 public static void Destroy(
UnityEngine.Object obj) {
95 if (Application.isPlaying) {
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) {
113 }
catch (Exception e) {
114 Debug.LogException(e);
121 if (toDestroy.Count != 0) {
122 for (
int i = 0; i < toDestroy.Count; i++) {
123 var obj = toDestroy[i];
125 Undo.DestroyObjectImmediate(obj);
132 private static void dispatchOnAnySave() {
133 if (OnAnySave !=
null) {
138 private struct InvokeStruct {
140 public Action action;
142 public InvokeStruct(Action action) {
143 this.action = action;
144 scene = SceneManager.GetActiveScene();
148 public class SaveWatcher :
UnityEditor.AssetModificationProcessor {
149 static string[] OnWillSaveAssets(
string[] paths) {
150 EditorApplication.delayCall += dispatchOnAnySave;
UnityEngine.Component Component