有没有一种通过脚本将对象导出到程序包的方法?

时间:2018-10-18 20:34:35

标签: c# object unity3d

我知道您可以在“编辑器”菜单中进行操作:“资产”>“导出软件包” ... 但是,如果您有很多对象,将需要一些时间才能找到要导出为包的对象。在“导出”窗口中没有搜索选项。

但是在层次结构中,您可以搜索特定的对象,但不能通过搜索将它们打包。

我在层次结构中搜索了例如door_lo和结果:

Doors

但是如何复制或打包这些对象,以便在其他项目中轻松使用它们?我可以做一个预制件,然后从硬盘上复制它们。

2 个答案:

答案 0 :(得分:2)

这可能会对您有所帮助。

//path will be the path folder of this files
private void ExportFiles(string path)
{
   string [] prefabFiles = Directory.GetFiles (path, "*.*"); 
   //string [] prefabFiles = Directory.GetFiles (path, "*.prefab");
   string exportPackage = targetFolder + "/exportpackage.unitypackage";
   ExportPackageOptions exportFlags = ExportPackageOptions.Default|ExportPackageOptions.Interactive | ExportPackageOptions.Recurse;
   AssetDatabase.ExportPackage (prefabFiles, exportPackage, exportFlags);

}

答案 1 :(得分:1)

不知道这是否是一个好方法,但是它可以按我的意愿工作。 搜索对象以将搜索到的对象作为子对象来创建预制件。 现在,我可以将预制件复制到另一个项目中。

两个字段:一个用于搜索一个预制名称 两个按钮:一个用于搜索,一个用于生成预制件

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class GeneratePrefab : EditorWindow
{
    private string prefabName = "";
    private string objectsToSearch = "";
    private static GeneratePrefab editor;
    private static int width = 350;
    private static int height = 200;
    private static int x = 0;
    private static int y = 0;
    private Vector2 scroll;
    private int count = 0;
    private List<GameObject> foundObjects = new List<GameObject>();
    private bool usePrefabName = false;
    private bool searched = false;

    [MenuItem("Window/Generate Prefab")]
    static void ShowEditor()
    {
        editor = EditorWindow.GetWindow<GeneratePrefab>();

        CenterWindow();
    }

    private void OnGUI()
    {
        GUI.Label(new Rect(10, 10, 80, 20), "Prefab Name");

        if (usePrefabName)
        {
            GUI.enabled = true;
        }
        else
        {
            GUI.enabled = false;
        }
        prefabName = GUI.TextField(new Rect(10, 30, 150, 20), prefabName, 25);

        GUI.enabled = true;
        GUILayout.Space(55);
        usePrefabName = GUILayout.Toggle(usePrefabName, "Use prefab name");

        GUI.Label(new Rect(10, 90, 150, 20), "Search by name");
        objectsToSearch = GUI.TextField(new Rect(10, 110, 150, 20), objectsToSearch, 25);

        if (objectsToSearch != "")
        {
            GUI.enabled = true;
        }
        else
        {
            GUI.enabled = false;
        }
        GUILayout.Space(90);
        if (GUILayout.Button("Search"))
        {
            count = 0;
            foreach (GameObject gameObj in GameObject.FindObjectsOfType<GameObject>())
            {
                if (gameObj.name == objectsToSearch)
                {
                    count += 1;
                    foundObjects.Add(gameObj);
                }
            }

            if (foundObjects.Count > 0)
            {
                searched = true;
            }
            else
            {
                searched = false;
            }
        }

        GUI.enabled = true;
        if (count > 0)
            GUI.TextField(new Rect(10, 130, 60, 20), count.ToString(), 25);

        if (searched)
        {
            GUI.enabled = true;
        }
        else
        {
            GUI.enabled = false;
        }
        GUILayout.Space(0.1f);
        if (GUILayout.Button("Generate Prefab"))
        {
            GameObject go = new GameObject();
            for (int i = 0; i < foundObjects.Count; i++)
            {
                foundObjects[i].transform.parent = go.transform;
            }

            if (usePrefabName == true)
                go.name = prefabName;

            PrefabUtility.CreatePrefab("Assets/" + go.name + ".prefab", go);
            DestroyImmediate(go);

            objectsToSearch = "";
            searched = false;
            count = 0;
        }
    }

    private static void CenterWindow()
    {
        editor = EditorWindow.GetWindow<GeneratePrefab>();
        x = (Screen.currentResolution.width - width) / 2;
        y = (Screen.currentResolution.height - height) / 2;
        editor.position = new Rect(x, y, width, height);
        editor.maxSize = new Vector2(width, height);
        editor.minSize = editor.maxSize;
    }
}