LegacyEditorHelper2.cs 1.4 KB

1234567891011121314151617181920212223242526272829
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Linq;
  4. namespace Pathfinding.Legacy {
  5. public static class LegacyEditorHelper {
  6. public static void UpgradeDialog (Object[] targets, System.Type upgradeType) {
  7. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  8. var gui = EditorGUIUtility.IconContent("console.warnicon");
  9. gui.text = "You are using the compatibility version of this component. It is recommended that you upgrade to the newer version. This may change the component's behavior.";
  10. EditorGUILayout.LabelField(GUIContent.none, gui, EditorStyles.wordWrappedMiniLabel);
  11. if (GUILayout.Button("Upgrade")) {
  12. Undo.RecordObjects(targets.Select(s => (s as Component).gameObject).ToArray(), "Upgrade from Legacy Component");
  13. foreach (var tg in targets) {
  14. var comp = tg as Component;
  15. var components = comp.gameObject.GetComponents<Component>();
  16. int index = System.Array.IndexOf(components, comp);
  17. var newRVO = Undo.AddComponent(comp.gameObject, upgradeType);
  18. foreach (var field in newRVO.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public)) {
  19. field.SetValue(newRVO, field.GetValue(comp));
  20. }
  21. Undo.DestroyObjectImmediate(comp);
  22. for (int i = components.Length - 1; i > index; i--) UnityEditorInternal.ComponentUtility.MoveComponentUp(newRVO);
  23. }
  24. }
  25. EditorGUILayout.EndVertical();
  26. }
  27. }
  28. }