Prefabutility



Unity API Example in C# - PrefabUtility.CreateEmptyPrefab & PrefabUtility.ReplacePrefab. You will need to put this in Assets/Editor (make it if it doesn't already exist) as this is a Editor script.
CreatePrefabEditor.cs

Unity Get Prefab

PrefabUtility.SaveAsPrefabAsset was introduced in 2018.3.0a6 Resolution Note: The creation of the prefab out of the model in postprocessor requires a little bit more code: You need to instantiate your model first, then save as prefab asset and destroy the instance. Adds a prefab data cleaner window under Window/Prefab data cleaner. Place the scripts in a folder named Editor in your project's Assets folder.; Open the window by selecting Window/Prefab data cleaner.; What it does.

usingUnityEngine;
usingUnityEditor;
publicclassCreatePrefabEditor
{
/* Example from Unity API Documentation for:
* PrefabUtility.CreateEmptyPrefab (Looks like a duplicate example) &
* PrefabUtiltiy.ReplacePrefab
* Converted from UnityScript to C#
*
* However, if you make a prefab from the project folder, there are a few errors
* generated from the CreateNew() function.
*
* Creates a prefab from the selected GameObjects.
* If the prefab already exists it asks if you want to replace it
*
*/
[MenuItem('Examples/Create Prefab From Selected')]
privatestaticvoidCreatePrefab()
{
GameObject[] objs=Selection.gameObjects;
/* For each gameobject in selected array,
* Creates an empty prefab then replaces it
*/
foreach(GameObjectgoinobjs)
{
stringlocalPath='Assets/'+go.name+'.prefab';
if (AssetDatabase.LoadAssetAtPath<GameObject>(localPath))
{
if (EditorUtility.DisplayDialog('Are you sure?',
'The prefab already exists. Do you want to overwrite it?',
'Yes', 'No'))
{
CreateNew(go, localPath);
}
}
else
{
CreateNew(go, localPath);
}
}
}
[MenuItem('Examples/Create Prefab From Selected', true)]
boolValidateCreatePrefab()
{
returnSelection.activeGameObject!=null;
}
// Create Empty Prefab and then Replace
staticvoidCreateNew(GameObjectobj, stringlocalPath)
{
Objectprefab=PrefabUtility.CreateEmptyPrefab(localPath);
PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
}
}
Prefabinstanceupdated

Prefab Utility Buildings Fiberglass

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment