首页 > 解决方案 > 如何使用 editorwindow 脚本导出预制包?

问题描述

using UnityEditor;
using UnityEngine;

public class ExportObjects : EditorWindow
{
    //Creates a new menu (Examples) with a menu item (Create Prefab)
    [MenuItem("GameObject/Create Prefab", false, -1)]
    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/Prefabs to export/" + 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);
                    Export(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);
                Export(gameObject, localPath);
            }
        }
    }

    // Disable the menu item if no selection is in place
    [MenuItem("GameObject/Create Prefab", true, -1)]
    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.SaveAsPrefabAsset(obj, localPath);
        PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
    }

    static void Export(GameObject obj, string localPath)
    {
        AssetDatabase.ExportPackage(localPath, obj.name);
    }
}

第一个问题是在 Export 方法中。它正在到达那里并做线:

AssetDatabase.ExportPackage(localPath, obj.name); 

但它不会在 localPath 的硬盘上创建任何包文件。

第二个问题是我不确定是否做假,-1 是正确的(但它正在工作)。

三、如何换线:

PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);

ReplacePrefab 已过时,应该像之前的行一样替换为: SaveAsPrefabAsset 并且我尝试过但在这行中的参数与上面的不同:obj、prefab、ReplacePrefabOptions.ConnectToPrefab 所以我不知道该怎么做。

标签: c#unity3d

解决方案


我相信这个包没有被创建,因为你没有目录“Prefabs to export”。一种建议的方法是创建目录是

string defaultPath = Application.dataPath + "/Prefabs to export/";
if (!Directory.Exists(defaultPath))
{
     Directory.CreateDirectory(defaultPath);
}

如果它对您不存在,这将创建 driectory。它将到达 AssetDatabase.ExportPackage 但编辑器一定抛出了一些错误。请检查编辑器日志。

转到您在 MenuItem 中设置的问题的第二部分 -1 是关于您的按钮在菜单中显示的顺序。您可以根据需要设置为 0 或 1 甚至 10。我相信菜单中最顶部的按钮保持为 0,因此将其设置为 -1 将使您的按钮高于 Unity 中的其他按钮。

到目前为止,我不知道有任何替换方法,经过一番挖掘后,我发现根据Unity 的团队,它被标记为 obsolete without replacement 。因此,您可以同时使用此 Replace 预制件,直到问题得到解决。

我们正在研究 ReplacePrefab 的替代品。同时,您可以继续使用过时的 API。更换本应在我们发货时完成,但没有成功。我们的错误是旧方法在没有准备好替换的情况下被标记为过时。带来不便敬请谅解。


推荐阅读