选择对应的对象 填写位置 点击保存存入json 并生成对应对象
此处可用string替代枚举 可以根据string来实力加载Resources里对应的预制体
代码实现:
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public enum ObjType
{
方块, 球, 圆柱
}
public class Type02
{
public string name;
public ObjType type;
public float x;
public float y;
public float z;
}
public class day02 : EditorWindow
{
static Type02 my=new Type02();
[MenuItem("编辑器/对象生成器")]
private static void Init()
{
GetWindow<day02>("对象生成器").Show();
}
private void OnGUI()
{
GUILayout.Label("请选择对象:"); //GUILayout.Label窗口上的文字
my.type = (ObjType)EditorGUILayout.EnumPopup(my.type);
GUILayout.Space(10); //行之间的距离
GUILayout.BeginHorizontal();
GUILayout.Label("x:");
my.x=EditorGUILayout.FloatField(my.x);
GUILayout.Label("y:");
my.y=EditorGUILayout.FloatField(my.y);
GUILayout.Label("z:");
my.z=EditorGUILayout.FloatField(my.z);
GUILayout.EndHorizontal();
GUILayout.Space(10); //行之间的距离
if (GUILayout.Button("保存")) //保存按钮
{
//写入json
string json=JsonConvert.SerializeObject(my);
File.WriteAllText(Application.dataPath+"/Resources/Type.json",json);
AssetDatabase.Refresh();
//生成对应对象 这里可以不用枚举只使用string 用Resources.Load加载对应string名字即可
GameObject go=null;
if (my.type==ObjType.方块)
{
go = GameObject.CreatePrimitive(PrimitiveType.Cube);
}
if (my.type == ObjType.球)
{
go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
}
if (my.type == ObjType.圆柱)
{
go = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
}
go.transform.position=new Vector3(my.x,my.y,my.z);
}
}
}
付出才有回报,祝愿大家都越来越优秀!
小菜不易,感谢观看,学到了就留下个支持的赞吧!
关注我,了解更多Unity相关知识哦!