【Unity】对象生成编辑器(练习小功能)

选择对应的对象 填写位置 点击保存存入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相关知识哦!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不食卷心菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值