20 where TNew : MonoBehaviour
23 private static Dictionary<MonoBehaviour, MonoBehaviour> _instanceMap =
null;
25 public static TNew Upgrade<TOld>(TOld oldComponent)
28 GameObject go = oldComponent.gameObject;
30 Debug.Log(
"Upgrading " + typeof(TNew).Name +
" on game object \"" + go.name +
"\" from Cesium for Unity " + oldComponent.VersionToBeUpgraded +
".");
32 TNew newComponent = go.AddComponent<TNew>();
36 oldComponent.Upgrade(go, newComponent);
38 if (_instanceMap ==
null)
40 _instanceMap =
new Dictionary<MonoBehaviour, MonoBehaviour>();
41 EditorApplication.update += UpdateScene;
44 _instanceMap.Add(oldComponent, newComponent);
48 Debug.LogError(
"Upgrading failed with an exception: " + e.ToString());
54 private static void UpdateScene()
58 for (
int i = 0; i < SceneManager.sceneCount; ++i)
60 Scene scene = SceneManager.GetSceneAt(i);
61 ReplaceReferencesInScene(scene);
64 foreach (var kvp
in _instanceMap)
66 MonoBehaviour oldComponent = kvp.Key;
67 MonoBehaviour newComponent = kvp.Value;
68 GameObject go = newComponent.gameObject;
71 EditorUtility.SetDirty(go);
75 Component[] components = go.GetComponents<Component>();
76 int indexOfOriginal =
Array.IndexOf(components, oldComponent);
77 int indexOfUpgraded =
Array.IndexOf(components, newComponent);
79 UnityEngine.Object.DestroyImmediate(oldComponent,
true);
81 if (indexOfOriginal >= 0 && indexOfUpgraded >= 0)
83 if (indexOfUpgraded > indexOfOriginal)
85 while (indexOfUpgraded > indexOfOriginal)
87 ComponentUtility.MoveComponentUp(newComponent);
95 EditorApplication.update -= UpdateScene;
98 private static void ReplaceReferencesInScene(Scene scene)
100 GameObject[] gameObjects = scene.GetRootGameObjects();
101 foreach (GameObject go
in gameObjects)
103 ReplaceReferencesInGameObject(go);
107 private static List<Component> _componentList =
new List<Component>();
109 private static void ReplaceReferencesInGameObject(GameObject go)
111 go.GetComponents<Component>(_componentList);
112 foreach (Component component
in _componentList)
115 if (component is MonoBehaviour m && _instanceMap.ContainsKey(m))
118 ReplaceReferencesInComponent(component);
121 Transform transform = go.transform;
122 for (
int i = 0; i < transform.childCount; ++i)
124 Transform childTransform = transform.GetChild(i);
125 ReplaceReferencesInGameObject(childTransform.gameObject);
129 private static void ReplaceReferencesInComponent(Component component)
131 SerializedObject so =
new SerializedObject(component);
132 SerializedProperty
property = so.GetIterator();
133 while (property.NextVisible(
true))
135 if (property.propertyType == SerializedPropertyType.ExposedReference)
137 UnityEngine.Object value =
property.exposedReferenceValue;
139 MonoBehaviour replacement;
140 if (value is MonoBehaviour m && _instanceMap.TryGetValue(m, out replacement))
142 Debug.Log(
"Updating property " + property.propertyPath +
" with upgraded component.");
143 property.exposedReferenceValue = replacement;
146 else if (property.propertyType == SerializedPropertyType.ObjectReference)
148 UnityEngine.Object value =
property.objectReferenceValue;
150 MonoBehaviour replacement;
151 if (value is MonoBehaviour m && _instanceMap.TryGetValue(m, out replacement))
153 Debug.Log(
"Updating property \"" + property.propertyPath +
"\" on component \"" + component.name +
"\" with upgraded component.");
154 property.objectReferenceValue = replacement;
158 so.ApplyModifiedProperties();