EditorUtils.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #if UNITY_2018_3_OR_NEWER
  2. using UnityEditor.Experimental.SceneManagement;
  3. #endif
  4. using UnityEditor;
  5. using UnityEditor.SceneManagement;
  6. using UnityEngine;
  7. namespace Coffee.UISoftMask
  8. {
  9. internal static class EditorUtils
  10. {
  11. internal static void MarkPrefabDirty()
  12. {
  13. #if UNITY_2018_3_OR_NEWER
  14. var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  15. if (prefabStage == null) return;
  16. EditorSceneManager.MarkSceneDirty(prefabStage.scene);
  17. #endif
  18. }
  19. /// <summary>
  20. /// Verify whether it can be converted to the specified component.
  21. /// </summary>
  22. internal static bool CanConvertTo<T>(Object context) where T : MonoBehaviour
  23. {
  24. return context && context.GetType() != typeof(T);
  25. }
  26. /// <summary>
  27. /// Convert to the specified component.
  28. /// </summary>
  29. internal static void ConvertTo<T>(Object context) where T : MonoBehaviour
  30. {
  31. var target = context as MonoBehaviour;
  32. var so = new SerializedObject(target);
  33. so.Update();
  34. var oldEnable = target.enabled;
  35. target.enabled = false;
  36. // Find MonoScript of the specified component.
  37. foreach (var script in Resources.FindObjectsOfTypeAll<MonoScript>())
  38. {
  39. if (script.GetClass() != typeof(T))
  40. continue;
  41. // Set 'm_Script' to convert.
  42. so.FindProperty("m_Script").objectReferenceValue = script;
  43. so.ApplyModifiedProperties();
  44. break;
  45. }
  46. (so.targetObject as MonoBehaviour).enabled = oldEnable;
  47. }
  48. }
  49. }