加入板子部分非通用,仅为当前项目需求,更多通用功能之后再慢慢加。
代码放在editor文件夹下,预制体放在resource下,路径按需修改,常用预制体可将名字加入枚举中,点击场景内物体点击添加即可作为子物体加入
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
public class ImportPrefabManager : EditorWindow
{
public static ImportPrefabManager importPrefabManager;
public List<string> pName = null;//新增的需要加入场景的预制体名
private string text = null;//输入的预制体名字
private string text1 = null;//输入板子的数量
private AddPlace addPlace;//加入的位置
private int tempListLength = 0;//记录列表长度,以便刷新显示
public CountNumber[] CountNumber;//记录加入的类型和数量
private bool Init = false;//是否初始化完成
/// <summary>
///添加到场景的哪个部分
/// </summary>
public enum AddPlace
{
lefttop,
leftcenter,
leftbottom,
rigthtop,
rigthcenter,
rigthbottom,
top,
center,
bottom,
}
/// <summary>
/// 常用的场景内含有的预制体名字
/// </summary>
public enum PrbName {
Baffle,
BronDoor,
Gear,
Ground1,
House,
OFF,
Pedal,
Player,
Player002,
Rock,
Sliders,
Stick,
tanhuang,
Transfer
}
/// <summary>
/// 初始化常用预制体的名字
/// </summary>
void InitPrbName() {
if (pName.Count<1) {
PrbName prb = new PrbName();
string[] temp = System.Enum.GetNames(prb.GetType());
foreach (string s in temp) {
pName.Add(s);
}
}
}
/// <summary>
/// 创建窗口
/// </summary>
[MenuItem("Tool/ImportManager _&z")]
static void AddPrefabToScene()
{
ImportPrefabManager window = EditorWindow.GetWindow<ImportPrefabManager>();
window.pName = new List<string>();
window.CountNumber = new CountNumber[100];
}
/// <summary>
/// 绘制窗口
/// </summary>
private void OnGUI()
{
//常用预制体初始化
InitPrbName();
GUILayout.Label("输入加载的预制体名");
text = GUILayout.TextField(text);
tempListLength = pName.Count;
if (GUILayout.Button("添加"))
{
pName.Add(text);
}
//加入的中间板子
GUILayout.Label("输入想加入的中间板子的数量");
text1 = GUILayout.TextField(text1);
if (GUILayout.Button("添加"))
{
AddGround(int.Parse(text1));
}
//类型计数初始化
if (Init == false) {
CountNumber = InitCount(pName);
Init = true;
}
GUILayout.Label("选择添加的预制体");
foreach (var name in pName)
{
GUILayout.BeginHorizontal();
GUILayout.Label(name);
if (GUILayout.Button("添加"))
{
AddPrefab(name);
}
GUILayout.EndHorizontal();
}
tempListLength = pName.Count;
}
/// <summary>
/// 获取选择的物体
/// </summary>
Transform[] CatchThePos() {
GameObject[] objects = Selection.gameObjects;
Transform[] transforms = new Transform[objects.Length];
for (int i = 0; i < objects.Length; i++) {
transforms[i] = objects[i].transform;
}
return transforms;
}
/// <summary>
/// 加入预制体到选择的地方
/// </summary>
/// <param name="pName">预制体名字</param>
void AddPrefab(string pName)
{
Transform[] transforms = CatchThePos();
int tempInt = 0;
for (int i = 0;i<CountNumber.Length;i++) {
Debug.Log(i+CountNumber[i].type + CountNumber[i].count);
if (CountNumber[i].type == pName)
{
CountNumber[i].count++;
tempInt = i;
}
Debug.Log(CountNumber[i].type + CountNumber[i].count);
}
foreach (Transform t in transforms) {
Object theObject = PrefabUtility.InstantiatePrefab(Resources.Load("Prefabs/"+pName),t); //加载原预制体
// Object theObject = Instantiate(Resources.Load(pName),t.position,t.rotation);
theObject.name = pName + CountNumber[tempInt].count;
}
}
/// <summary>
/// 初始化计数器
/// </summary>
/// <param name="l">预制体列表</param>
/// <returns></returns>
CountNumber[] InitCount(List<string> l) {
CountNumber[] Count = new CountNumber[l.Count];
string[] temp = l.ToArray();
for (int i = 0;i<l.Count;i++) {
CountNumber tempCount = new CountNumber();
Count[i] = tempCount;
Count[i].type = temp[i];
Debug.Log("Count["+i+"].type " + Count[i].type);
}
return Count;
}
/// <summary>
/// 设置加入的地面的长度,并添加碰撞体
/// </summary>
/// <param name="count">中间要加几块板</param>
void AddGround(int count) {
int temp = -1;
GameObject Ground = new GameObject();
Ground.transform.position = Vector3.zero;
Ground.name = "Road";
GameObject left = (GameObject)PrefabUtility.InstantiatePrefab(Resources.Load("Prefabs/Road_left"), Ground.transform);
GameObject right = (GameObject)PrefabUtility.InstantiatePrefab(Resources.Load("Prefabs/Road_right"), Ground.transform);
BoxCollider box = Ground.AddComponent<BoxCollider>();
box.size = new Vector3(0.6389f, 0.36f, 1f);
box.center = new Vector3(-0.0895f, 0, 0);
if (count == 0)
{
left.transform.position = new Vector3(-0.265f, 0, 0);
right.transform.position = new Vector3(0.1752f, 0, 0);
}
else {
left.transform.position = new Vector3(-0.2650f - (count * 0.357f), 0, 0);
right.transform.position = new Vector3(0.1752f + (count * 0.357f), 0, 0);
for (int i = 1; i <= count; i++) {
GameObject center = (GameObject)PrefabUtility.InstantiatePrefab(Resources.Load("Prefabs/Road_center"), Ground.transform);
center.name = "Road_center" + i;
center.transform.position = new Vector3(0.357f*((i+temp)-count+1),0,0);
temp++;
}
box.size = new Vector3(0.8619f + (count * 0.63f), 0.36f, 1f);
}
}
}