添加扩展编辑器
将代码放在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!
批量生成
来源:CSDN
作者:Cuijiahao
链接:https://blog.csdn.net/cuijiahao/article/details/104082528