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.

Prefab Utility Shed

CreatePrefabEditor.cs
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);
}
}
Prefabutility
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Mar 25th, 2018

Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time. Learn Unity for 2D Game Development - TIA. Prefabs are an editor only feature. That's why the PrefabUtility is an editor class. Prefabs are just serialized standalone assets. At runtime you can only use assets which you have created in the editor.

Never
Not a member of Pastebin yet?Sign Up, it unlocks many cool features!
  1. usingUnityEditor;
  2. {
  3. staticvoid RevertPrefabNames()
  4. RevertAllNames(Selection.GetFiltered(typeof(GameObject), SelectionMode.OnlyUserModifiable));
  5. publicstaticvoid RemoveNameModification(UnityEngine.Object aObj)
  6. var mods =new List<PropertyModification>(PrefabUtility.GetPropertyModifications(aObj));
  7. {
  8. mods.RemoveAt(i);
  9. PrefabUtility.SetPropertyModifications(aObj, mods.ToArray());
  10. publicstaticvoid RevertAllNames(Object[] aObjects)
  11. var items =new List<Object>();
  12. {
  13. if(prefab !=null)
  14. Undo.RecordObject(item, 'Revert perfab name');
  15. items.Add(item);
  16. }
  17. foreach(var item in items)
  18. RemoveNameModification(item);
  19. }

Prefab Utility Buildings For Sale

Prefabutility

Unity Prefab Variant

RAW Paste Data

Prefabutility.saveasprefabasset