可用于任何预制件相关操作的 Utility 类。
//Create a folder (right click in the Assets directory, click Create>New Folder) and name it “Editor” if one doesn’t exist already. //Place this script in that folder
//This script creates a new menu and a new menu item in the Editor window // Use the new menu item to create a prefab at the given path. If a prefab already exists it asks if you want to replace it //Click on a GameObject in your Hierarchy, then go to Examples>Create Prefab to see it in action.
using UnityEngine; using UnityEditor;
public class Example : EditorWindow { //Creates a new menu (Examples) with a menu item (Create Prefab) [MenuItem("Examples/Create Prefab")] static void CreatePrefab() { //Keep track of the currently selected GameObject(s) GameObject[] objectArray = Selection.gameObjects;
//Loop through every GameObject in the array above foreach (GameObject gameObject in objectArray) { //Set the path as within the Assets folder, and name it as the GameObject's name with the .prefab format string localPath = "Assets/" + gameObject.name + ".prefab";
//Check if the Prefab and/or name already exists at the path if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject))) { //Create dialog to ask if User is sure they want to overwrite existing prefab if (EditorUtility.DisplayDialog("Are you sure?", "The prefab already exists. Do you want to overwrite it?", "Yes", "No")) //If the user presses the yes button, create the Prefab { CreateNew(gameObject, localPath); } } //If the name doesn't exist, create the new Prefab else { Debug.Log(gameObject.name + " is not a prefab, will convert"); CreateNew(gameObject, localPath); } } }
// Disable the menu item if no selection is in place [MenuItem("Examples/Create Prefab", true)] static bool ValidateCreatePrefab() { return Selection.activeGameObject != null; }
static void CreateNew(GameObject obj, string localPath) { //Create a new prefab at the path given Object prefab = PrefabUtility.CreatePrefab(localPath, obj); PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab); } }
prefabInstanceUpdated | 在场景中的预制件实例完成更新后调用。 |
ConnectGameObjectToPrefab | 将源预制件连接到游戏对象。 |
CreateEmptyPrefab | 在给定路径创建一个空白预制件。 |
CreatePrefab | 从游戏对象层级视图创建预制件。 |
DisconnectPrefabInstance | 断开预制件实例与其父预制件的连接。 |
FindPrefabRoot | Helper 函数,可查找对象的预制件根(用于选择精度)。 |
FindRootGameObjectWithSameParentPrefab | 返回与 target 具有相同预制件父项的最顶层游戏对象。 |
FindValidUploadPrefabInstanceRoot | 如果根预制件实例是预制件的父项,则返回该预制件实例的根游戏对象。 |
GetPrefabObject | 检索已完成的预制件内包含的任何对象。 |
GetPrefabParent | 返回 source 的父资源对象,如果找不到,则返回 null。 |
GetPrefabType | 给定一个对象,返回其预制件类型(如果不是预制件,则返回 None)。 |
GetPropertyModifications | 提取应用于预制件实例的所有修改(与父预制件相比)。 |
InstantiateAttachedAsset | 将预制件所引用的资源实例化,并在预制件实例上使用该资源。 |
InstantiatePrefab | 将给定场景中的给定预制件实例化。 |
MergeAllPrefabInstances | 强制将此预制件的所有预制件实例再次合并。 |
ReconnectToLastPrefab | 将游戏对象连接到它上次连接到的预制件。 |
RecordPrefabInstancePropertyModifications | 指示记录对预制件实例所做的修改。 |
ReplacePrefab | 将 targetPrefab 替换为游戏对象层级视图 go 的副本。 |
ResetToPrefabState | 将组件或游戏对象的属性重置为父预制件状态。 |
RevertPrefabInstance | 重置预制件中所有对象的属性,包括添加到预制件实例的子游戏对象和组件。 |
SetPropertyModifications | 指定应用于预制件实例的所有修改(与父预制件相比)。 |
PrefabInstanceUpdated | 在场景中的预制件实例完成更新后所调用的方法的委托。 |