public static Object CreateEmptyPrefab (string path);

描述

在给定路径创建一个空白预制件。

如果路径中已经存在预制件,它将被删除并替换为一个空白预制件。 返回对预制件的引用。

// Creates a prefab at the given path.
// If a prefab already exists it asks if you want to replace it

using UnityEngine; using UnityEditor;

public class CreateEmptyExample : EditorWindow { [MenuItem("Examples/Create Empty Prefab")] static void CreatePrefab() { GameObject[] objs = Selection.gameObjects;

foreach (GameObject go in objs) { string localPath = "Assets/" + go.name + ".prefab"; if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject))) { if (EditorUtility.DisplayDialog("Are you sure?", "The prefab already exists. Do you want to overwrite it?", "Yes", "No")) { CreateNew(go, localPath); } } else { Debug.Log(go.name + " is not a prefab, will convert"); CreateNew(go, localPath); } } }

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

static void CreateNew(GameObject obj, string localPath) { Object prefab = PrefabUtility.CreateEmptyPrefab(localPath); PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961