PrefabUtility

class in UnityEditor

切换到手册

描述

可用于任何预制件相关操作的 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断开预制件实例与其父预制件的连接。
FindPrefabRootHelper 函数,可查找对象的预制件根(用于选择精度)。
FindRootGameObjectWithSameParentPrefab返回与 target 具有相同预制件父项的最顶层游戏对象。
FindValidUploadPrefabInstanceRoot如果根预制件实例是预制件的父项,则返回该预制件实例的根游戏对象。
GetPrefabObject检索已完成的预制件内包含的任何对象。
GetPrefabParent返回 source 的父资源对象,如果找不到,则返回 null。
GetPrefabType给定一个对象,返回其预制件类型(如果不是预制件,则返回 None)。
GetPropertyModifications提取应用于预制件实例的所有修改(与父预制件相比)。
InstantiateAttachedAsset将预制件所引用的资源实例化,并在预制件实例上使用该资源。
InstantiatePrefab将给定场景中的给定预制件实例化。
MergeAllPrefabInstances强制将此预制件的所有预制件实例再次合并。
ReconnectToLastPrefab将游戏对象连接到它上次连接到的预制件。
RecordPrefabInstancePropertyModifications指示记录对预制件实例所做的修改。
ReplacePrefab将 targetPrefab 替换为游戏对象层级视图 go 的副本。
ResetToPrefabState将组件或游戏对象的属性重置为父预制件状态。
RevertPrefabInstance重置预制件中所有对象的属性,包括添加到预制件实例的子游戏对象和组件。
SetPropertyModifications指定应用于预制件实例的所有修改(与父预制件相比)。

委托

PrefabInstanceUpdated在场景中的预制件实例完成更新后所调用的方法的委托。
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961