public void RegisterCreatedObjects(GameObject[] gameObjects) { ExposeToEditor[] exposeToEditor = gameObjects.Select(o => o.GetComponent <ExposeToEditor>()).Where(o => o != null).OrderByDescending(o => o.transform.GetSiblingIndex()).ToArray(); Undo.BeginRecord(); if (exposeToEditor.Length == 0) { Debug.LogWarning("To register created object GameObject add ExposeToEditor script to it"); } else { Undo.RegisterCreatedObjects(exposeToEditor); } Selection.objects = gameObjects; Undo.EndRecord(); if (ObjectsRegistered != null) { ObjectsRegistered(gameObjects); } }
public void Delete() { GameObject[] selection = Selection.gameObjects; if (selection == null) { return; } Undo.BeginRecord(); for (int i = 0; i < selection.Length; ++i) { GameObject selectedObj = selection[i]; if (selectedObj != null) { Undo.BeginDestroyObject(selectedObj); } } Undo.RecordSelection(); Undo.EndRecord(); bool isEnabled = Undo.Enabled; Undo.Enabled = false; Selection.objects = null; Undo.Enabled = isEnabled; Undo.BeginRecord(); for (int i = 0; i < selection.Length; ++i) { GameObject selectedObj = selection[i]; if (selectedObj != null) { Undo.DestroyObject(selectedObj); } } Undo.RecordSelection(); Undo.EndRecord(); }
public void Delete(GameObject[] gameObjects) { if (gameObjects == null || gameObjects.Length == 0) { return; } if (!Undo.Enabled) { for (int i = 0; i < gameObjects.Length; ++i) { GameObject go = gameObjects[i]; if (go != null) { ExposeToEditor exposed = go.GetComponent <ExposeToEditor>(); if (exposed == null || exposed.CanDelete) { Destroy(go); } } } if (ObjectsDeleted != null) { ObjectsDeleted(gameObjects); } return; } ExposeToEditor[] exposeToEditor = gameObjects.Select(o => o.GetComponent <ExposeToEditor>()).Where(exposed => exposed != null && exposed.CanDelete).OrderByDescending(o => o.transform.GetSiblingIndex()).ToArray(); if (exposeToEditor.Length == 0) { return; } HashSet <GameObject> removeObjectsHs = new HashSet <GameObject>(exposeToEditor.Select(exposed => exposed.gameObject)); bool isRecording = Undo.IsRecording; if (!isRecording) { Undo.BeginRecord(); } if (Selection.objects != null) { List <UnityEngine.Object> selection = Selection.objects.ToList(); for (int i = selection.Count - 1; i >= 0; --i) { if (removeObjectsHs.Contains(selection[i])) { selection.RemoveAt(i); } } Selection.objects = selection.ToArray(); } Undo.DestroyObjects(exposeToEditor); if (!isRecording) { Undo.EndRecord(); } if (ObjectsDeleted != null) { ObjectsDeleted(gameObjects); } }
public void Duplicate(GameObject[] gameObjects) { if (gameObjects == null || gameObjects.Length == 0) { return; } if (!Undo.Enabled) { for (int i = 0; i < gameObjects.Length; ++i) { GameObject go = gameObjects[i]; if (go != null) { ExposeToEditor exposed = go.GetComponent <ExposeToEditor>(); if (exposed == null || exposed.CanDuplicate) { Instantiate(go, go.transform.position, go.transform.rotation); } } } if (ObjectsDuplicated != null) { ObjectsDuplicated(gameObjects); } return; } List <GameObject> duplicates = new List <GameObject>(); for (int i = 0; i < gameObjects.Length; ++i) { GameObject go = gameObjects[i]; if (go == null) { continue; } ExposeToEditor exposed = go.GetComponent <ExposeToEditor>(); if (exposed != null && !exposed.CanDuplicate) { continue; } GameObject duplicate = Instantiate(go, go.transform.position, go.transform.rotation); duplicate.SetActive(true); duplicate.SetActive(go.activeSelf); if (go.transform.parent != null) { duplicate.transform.SetParent(go.transform.parent, true); } duplicates.Add(duplicate); } if (duplicates.Count > 0) { ExposeToEditor[] exposeToEditor = duplicates.Select(o => o.GetComponent <ExposeToEditor>()).OrderByDescending(o => o.transform.GetSiblingIndex()).ToArray(); Undo.BeginRecord(); Undo.RegisterCreatedObjects(exposeToEditor); Selection.objects = duplicates.ToArray(); Undo.EndRecord(); } if (ObjectsDuplicated != null) { ObjectsDuplicated(gameObjects); } }