Forked from SiarheiPilat/ReplaceWithPrefab.cs
Last active
September 30, 2022 03:04
-
-
Save nrichards/172dd5551487e31c8ab7453f8fe73c0b to your computer and use it in GitHub Desktop.
Unity 2022 script, ReplaceWithPrefab: To edit a scene's Prefab model and data, avoiding editing the original Prefab
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 UnityEngine; | |
using UnityEditor; | |
/// Video: https://youtu.be/1FhVMFMG97Q | |
/// Taken from: https://gist.github.com/SiarheiPilat/05463e64d4662860c6a799bb23d9aec8 | |
/// Forked from: https://gist.github.com/unity3dcollege/c1efea3f87d3775bee3e010e9c6d7648 | |
/// Author: Siarhei Pilat (Suasor AB) | |
/// License: MIT | |
public class ReplaceWithPrefab : EditorWindow | |
{ | |
[SerializeField] private GameObject prefab; | |
[MenuItem("Tools/Replace With Prefab")] | |
static void CreateReplaceWithPrefab() | |
{ | |
GetWindow<ReplaceWithPrefab>(); | |
} | |
private void OnGUI() | |
{ | |
prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false); | |
if (GUILayout.Button("Replace")) | |
{ | |
var selection = Selection.gameObjects; | |
for (var i = selection.Length - 1; i >= 0; --i) | |
{ | |
var selected = selection[i]; | |
var prefabType = PrefabUtility.GetPrefabAssetType(prefab); | |
GameObject newObject; | |
if (prefabType != PrefabAssetType.NotAPrefab) | |
{ | |
newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab); | |
} | |
else | |
{ | |
newObject = Instantiate(prefab); | |
newObject.name = prefab.name; | |
} | |
if (newObject == null) | |
{ | |
Debug.LogError("Error instantiating prefab"); | |
break; | |
} | |
Undo.RegisterCreatedObjectUndo(newObject, "Replace With Prefabs"); | |
newObject.transform.parent = selected.transform.parent; | |
newObject.transform.localPosition = selected.transform.localPosition; | |
newObject.transform.localRotation = selected.transform.localRotation; | |
newObject.transform.localScale = selected.transform.localScale; | |
newObject.transform.SetSiblingIndex(selected.transform.GetSiblingIndex()); | |
Undo.DestroyObjectImmediate(selected); | |
} | |
} | |
GUI.enabled = false; | |
EditorGUILayout.LabelField("Selection count: " + Selection.objects.Length); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful to add your own variant of a Prefab to an existing instantiated set of Prefabs in a Scene.
Here, I took a Corgi Engine sample Scene, and made a "Platform*...Jumper.prefab", converting regular platforms into auto-jumpig platforms, without modifying the original prefab. I duplicated and renamed as new, the original two prefabs which are used to build the level. Then I added a Corgi Engine Jumper component to the new Prefabs. Then I used THIS script to replace the prefabs in the scene, and VOILA I had a whole level that was a jumping level.