unity 利用扩展编辑器添加摆放好的游戏物体

孤街醉人 提交于 2020-02-01 01:16:36

添加扩展编辑器

将代码放在Editor文件夹下,这个文件夹下的所有脚本在场景运行前会先被预编译,而且不会被打包进去。

 

编辑器脚本

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEditor;

 

public class SpawnManager : EditorWindow {

 

    [MenuItem("Tools/Click Me!")] //创建菜单Tools/Click Me!

    static void PatternSystem()

    {

        GameObject spawnManager = GameObject.Find("PatternManager");

        if(spawnManager != null)

        {

            var patternManager = spawnManager.GetComponent<PatternManager>();

            if (Selection.gameObjects.Length == 1)//如果当前鼠标只点击了1个游戏物体

            {

                var item = Selection.gameObjects[0].transform.Find("Item");//获取当前点击的游戏物体的item子节点

                if (item != null)

                {

                    //创建一套道路方案

                    Pattern pattern = new Pattern();

                    foreach (var child in item)

                    {

                        Transform childTrans = child as Transform;

                        if(childTrans != null)

                        {

                            //找到它对应的预制体

                            var prefab = UnityEditor.PrefabUtility.GetPrefabParent(childTrans.gameObject);//返回游戏物体对应的预制体

                            if (prefab != null)

                            {

                                //设置道路上游戏物体的信息

                                PatterItem patternItem = new PatterItem

                                {

                                    pos = childTrans.localPosition,

                                    prefabName = prefab.name

                                };

                                pattern.PatterItems.Add(patternItem);//将游戏物体添加到方案里

                            }

                        }

                    }

                    patternManager.Patterns.Add(pattern);//将道路方案添加到方案列表里

                }

            }

        }

    }

}

 

 

点击Pattern_1,点击Click Me!

批量生成

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!