编辑器(例如技能)

 

 效果

代码:

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Newtonsoft.Json;

/// <summary>
/// 数据
/// </summary>
public class Data
{
public string id;
public float num;
public string name;
public string path;

public Data(string id, float num, string name, string path)
{
this.id = id;
this.num = num;
this.name = name;
this.path = path;
}
}

/// <summary>
/// 技能编辑器
/// </summary>
public class SkillEditor : EditorWindow
{
//保存数据的集合
public List<Data> datas = new List<Data>();
//图片
public Texture texture;
public string text;
public float num;
public int index;
string pathIcon;
public string[] arr = { "张三", "李四", "王二", "麻子" };

[MenuItem("工具/1,技能编辑器")]
public static void Init()
{
//创建一个窗口
var win = GetWindow<SkillEditor>();
win.Show();//展示窗口
}
private void OnGUI()
{
//开始水平创建
GUILayout.BeginHorizontal();
//描述
GUILayout.Label("技能名称:");
//输入框 string 类型 宽度限制
text = GUILayout.TextField(text, GUILayout.Width(200));
//结束水平创建
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label("技能攻击力:");
// 编辑器GUI布局 float类型
num = EditorGUILayout.FloatField(num, GUILayout.Width(200));
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label("角色选择:");
// 下拉框 对应下标 ,数组,输入框宽度
index = EditorGUILayout.Popup(index, arr, GUILayout.Width(200));
GUILayout.EndHorizontal();

GUILayout.BeginVertical();
GUILayout.Label("技能图片:");
// 按钮 纹理 宽 高
if (GUILayout.Button(texture, GUILayout.Width(50), GUILayout.Height(50)))
{
//选取图片 编辑器程序 打开文件面板 面板左上角名称 Assets目录下 路径 类型
string path = EditorUtility.OpenFilePanel("选择图片", Application.dataPath + "/Resources/Skill/", "png");
string[] a = path.Split('/');
string[] b = a[a.Length - 1].Split('.');
pathIcon = "Skill/" + b[0];
texture = Resources.Load<Texture>(pathIcon);

}
GUILayout.EndVertical();

GUILayout.BeginHorizontal();
if (GUILayout.Button("保存"))
{
datas.Add(new Data(text, num, arr[index], pathIcon));
// json转换 .序列化对象 数组
string json = JsonConvert.SerializeObject(datas.ToArray());
//文件.写入全部文本 路径 字符串
File.WriteAllText(Application.dataPath + "/Resources/skillData.json", json);
//资源数据库.刷新
AssetDatabase.Refresh();
}

GUILayout.EndHorizontal();


}


}

posted @ 2023-03-22 14:16  天分丶张三  阅读(39)  评论(0编辑  收藏  举报