Last active
September 24, 2021 18:41
-
-
Save isuzu-shiranui/9700b1ca51d3bbb6abf87a074998b5d5 to your computer and use it in GitHub Desktop.
Undo可能にするやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
internal static class UndoableExtension | |
{ | |
public static T GetOrAddComponentWithUndo<T>(this GameObject gameObject) where T : Component | |
{ | |
if (!gameObject.TryGetComponent<T>(out var component)) | |
component = Undo.AddComponent<T>(gameObject); | |
else | |
Undo.RecordObject(component, $"{nameof(component)} Component Added."); | |
return component; | |
} | |
public static T GetOrAddComponentWithUndo<T>(this Transform transform) where T : Component | |
{ | |
if (!transform.TryGetComponent<T>(out var component)) | |
component = Undo.AddComponent<T>(transform.gameObject); | |
else | |
Undo.RecordObject(component, $"{nameof(component)} Component Added."); | |
return component; | |
} | |
public static T AddComponentWithUndo<T>(this GameObject gameObject) where T : Component | |
{ | |
return Undo.AddComponent<T>(gameObject); | |
} | |
public static T AddComponentWithUndo<T>(this Transform transform) where T : Component | |
{ | |
return Undo.AddComponent<T>(transform.gameObject); | |
} | |
public static GameObject CreateNewGameObjectWithUndo(string name, Transform parent = null) | |
{ | |
var gameObject = new GameObject(name); | |
Undo.RegisterCreatedObjectUndo(gameObject, name); | |
if(parent != null) gameObject.transform.SetParent(parent); | |
return gameObject; | |
} | |
public static void SetDirtyIfPartOfPrefabInstance(this Component component) | |
{ | |
if (PrefabUtility.IsPartOfPrefabInstance(component)) | |
EditorUtility.SetDirty(component); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment