Version: 2022.3
언어: 한국어

PrefabUtility

class in UnityEditor

매뉴얼로 전환

설명

Utility class for any Prefab related operations.

// This script creates a new menu item Examples>Create Prefab in the main menu.
// Use it to create Prefab(s) from the selected GameObject(s).
// It is placed in the root Assets folder.
using System.IO;
using UnityEngine;
using UnityEditor;

public class Example { // Creates a new menu item 'Examples > Create Prefab' in the main menu. [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) { // Create folder Prefabs and set the path as within the Prefabs folder, // and name it as the GameObject's name with the .Prefab format if (!Directory.Exists("Assets/Prefabs")) AssetDatabase.CreateFolder("Assets", "Prefabs"); string localPath = "Assets/Prefabs/" + gameObject.name + ".prefab";

// Make sure the file name is unique, in case an existing Prefab has the same name. localPath = AssetDatabase.GenerateUniqueAssetPath(localPath);

// Create the new Prefab and log whether Prefab was saved successfully. bool prefabSuccess; PrefabUtility.SaveAsPrefabAssetAndConnect(gameObject, localPath, InteractionMode.UserAction, out prefabSuccess); if (prefabSuccess == true) Debug.Log("Prefab was saved successfully"); else Debug.Log("Prefab failed to save" + prefabSuccess); } }

// Disable the menu item if no selection is in place. [MenuItem("Examples/Create Prefab", true)] static bool ValidateCreatePrefab() { return Selection.activeGameObject != null && !EditorUtility.IsPersistent(Selection.activeGameObject); } }

정적 변수

prefabInstanceUpdatedUnity calls this method automatically when Prefab instances in the Scene have been updated.

정적 함수

ApplyAddedComponentApplies the added component to the Prefab Asset at the given asset path.
ApplyAddedGameObjectApplies the added GameObject to the Prefab Asset at the given asset path.
ApplyAddedGameObjectsApplies the added GameObjects to the Prefab Asset at the given asset path.
ApplyObjectOverrideApplies all overridden properties on a Prefab instance component or GameObject to the Prefab Asset at the given asset path.
ApplyPrefabInstanceApplies all overrides on a Prefab instance to its Prefab Asset.
ApplyPrefabInstancesApplies all overrides from a list of Prefab instances to their Prefab Assets.
ApplyPropertyOverrideApplies a single overridden property on a Prefab instance to the Prefab Asset at the given asset path.
ApplyRemovedComponentRemoves the component from the Prefab Asset which has the component on it.
ApplyRemovedGameObjectRemoves the GameObject from the source Prefab Asset.
ConvertToPrefabInstanceConvert the plain GameObject to a Prefab instance using the provided Prefab Asset root object.
ConvertToPrefabInstancesConvert an array of GameObjects to Prefab instances of the given Prefab Asset.
FindAllInstancesOfPrefabRetrieves the root GameObjects for all instances of the Prefab asset with root prefabRoot found in all currently loaded scenes. If prefabRoot is not a valid Prefab asset root GameObject, an ArgumentException is thrown.
GetAddedComponentsRetrieves a list of PrefabUtility.AddedComponent objects which contain information about added component overrides on the Prefab instance.
GetAddedGameObjectsRetrieves a list of PrefabUtility.AddedGameObject objects which contain information about added GameObjects on the Prefab instance.
GetCorrespondingObjectFromOriginalSourceRetrieves the object of origin for the given object.
GetCorrespondingObjectFromSourceRetrieves the corresponding asset object of source, or null if it can't be found.
GetCorrespondingObjectFromSourceAtPathRetrieves the corresponding object of the given object from a given Prefab Asset path.
GetIconForGameObjectRetrieves the icon for the given GameObject.
GetNearestPrefabInstanceRootRetrieves the GameObject that is the root of the nearest Prefab instance the object is part of.
GetObjectOverridesRetrieves a list of objects with information about object overrides on the Prefab instance.
GetOriginalSourceRootWhereGameObjectIsAddedUse this method to find the Prefab Asset root where a Prefab instance or Prefab Asset object was added originally.
GetOutermostPrefabInstanceRootRetrieves the GameObject that is the root of the outermost Prefab instance the object is part of.
GetPrefabAssetPathOfNearestInstanceRootRetrieves the asset path of the nearest Prefab instance root the specified object is part of.
GetPrefabAssetTypeRetrieves an enum value indicating the type of Prefab Asset, such as Regular Prefab, Model Prefab and Prefab Variant.
GetPrefabInstanceHandleRetrieves the PrefabInstance object for the outermost Prefab instance the provided object is part of.
GetPrefabInstanceStatusDetermines whether a Prefab instance is properly connected to its asset.
GetPropertyModificationsExtracts all modifications that are applied to the Prefab instance compared to the parent Prefab.
GetRemovedComponentsReturns a list of objects with information about removed component overrides on the Prefab instance.
GetRemovedGameObjectsReturns a list of objects with information about removed GameObject overrides on the Prefab instance.
HasManagedReferencesWithMissingTypesDetermines whether the object Prefab asset contains any MonoBehaviours with missing SerializeReference types.
HasPrefabInstanceAnyOverridesReturns true if the given Prefab instance has any overrides.
InstantiateAttachedAssetInstantiate an asset that is referenced by a Prefab and use it on the Prefab instance.
InstantiatePrefabInstantiates the given Prefab in a given Scene.
IsAddedComponentOverrideIs this component added to a Prefab instance as an override?
IsAddedGameObjectOverrideIs this GameObject added as a child to a Prefab instance as an override?
IsAnyPrefabInstanceRootIs the GameObject the root of any Prefab instance?
IsDefaultOverrideReturns true if the given modification is considered a default override.
IsOutermostPrefabInstanceRootIs the GameObject the root of a Prefab instance, excluding nested Prefabs?
IsPartOfAnyPrefabReturns true if the given object is part of any kind of Prefab.
IsPartOfImmutablePrefabIs this object part of a Prefab that cannot be edited?
IsPartOfModelPrefabReturns true if the given object is part of a Model Prefab Asset or Model Prefab instance.
IsPartOfNonAssetPrefabInstanceReturns true if the given object is part of a Prefab instance and not part of an asset.
IsPartOfPrefabAssetReturns true if the given object is part of a Prefab Asset.
IsPartOfPrefabInstanceReturns true if the given object is part of a Prefab instance.
IsPartOfPrefabThatCanBeAppliedToIs this object part of a Prefab that cannot be applied to?
IsPartOfRegularPrefabReturns true if the given object is part of a regular Prefab instance or Prefab Asset.
IsPartOfVariantPrefabReturns true if the given object is part of a Prefab Variant Asset or Prefab Variant instance.
IsPrefabAssetMissingReturns true if the given object is part of a Prefab instance but the source asset is missing.
LoadPrefabContentsLoads a Prefab Asset at a given path into an isolated Scene and returns the root GameObject of the Prefab.
LoadPrefabContentsIntoPreviewSceneLoads a Prefab Asset at a given path into a given preview Scene and returns the root GameObject of the Prefab.
MergePrefabInstanceForces a Prefab instance to merge with changes from the Prefab Asset.
RecordPrefabInstancePropertyModificationsCauses modifications made to the Prefab instance to be recorded.
ReplacePrefabAssetOfPrefabInstanceReplace the Prefab Asset for a Prefab instance that exists in a Scene or for a nested Prefab instance inside another Prefab.
ReplacePrefabAssetOfPrefabInstancesReplace the Prefab Asset for an array of Prefab instances that exists in Scenes or for nested Prefab instances inside another Prefab.
RevertAddedComponentRemoves this added component on a Prefab instance.
RevertAddedGameObjectRemoves this added GameObject from a Prefab instance.
RevertObjectOverrideReverts all overridden properties on a Prefab instance component or GameObject.
RevertPrefabInstanceReverts all overrides on a Prefab instance.
RevertPropertyOverrideRevert a single property override on a Prefab instance.
RevertRemovedComponentAdds this removed component back on the Prefab instance.
RevertRemovedGameObjectAdds this removed GameObject back on the Prefab instance.
SaveAsPrefabAssetUse this function to create a Prefab Asset at the given path from the given GameObject, including any childen in the Scene without modifying the input objects.
SaveAsPrefabAssetAndConnectUse this function to create a Prefab Asset at the given path from the given GameObject, including any children in the Scene and at the same time make the given GameObject into an instance of the new Prefab.
SavePrefabAssetUse this function to save the version of an existing Prefab Asset that exists in memory back to disk.
SetPropertyModificationsAssigns all modifications that are applied to the Prefab instance compared to the parent Prefab.
UnloadPrefabContentsReleases the content from a Prefab previously loaded with LoadPrefabContents from memory.
UnpackAllInstancesOfPrefabUnpacks all instances of a given Prefab Asset root GameObject in all open scenes so that all instances are replaced with the contents of the Prefab Asset while retaining all override values.
UnpackPrefabInstanceUnpacks a given Prefab instance so that it is replaced with the contents of the Prefab Asset while retaining all override values.
UnpackPrefabInstanceAndReturnNewOutermostRootsThis function will unpack the given Prefab instance using the behaviour specified by unpackMode.

Events

prefabInstanceUnpackedUnity calls this method automatically after a Prefab instance is unpacked.
prefabInstanceUnpackingUnity calls this method automatically before a Prefab instance is unpacked.

델리게이트

PrefabInstanceUpdatedDelegate for method that is called after Prefab instances in the Scene have been updated.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961