技能编辑器需要继承EditorWinder进行编辑,所以我们需要再我们创建的项目中首先创建一个Editor编辑器文件夹,并将需要执行的编辑
实现的效果就是这样的 ,下一步我们将对立面进行分行选择编辑,这个时候我们就需要使用编辑器中OnGUI这个方法进行编辑器功能展示
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using Newtonsoft.Json;
using System.IO;
using NUnit.Framework;
public class SkillEditor : EditorWindow
{
static SkillData skillData;
static Texture texture;
AnimationClip anim;
GameObject effect;
Vector3 pos;
GameObject effectEnd;
static List<SkillData> list = new List<SkillData>();
[MenuItem("Tools/SkillEditor")]
public static void Init()
{
string json = File.ReadAllText(Application.streamingAssetsPath + "/skill");
list = JsonConvert.DeserializeObject<List<SkillData>>(json);
skillData = new SkillData();
texture = Resources.Load<Texture>("Icon/baihuwang_1b");
SkillEditor SkillEditor = GetWindow<SkillEditor>("SkillEditor");
SkillEditor.maxSize = new Vector2(600, 600);
SkillEditor.minSize = new Vector2(100, 100);
SkillEditor.Show();
}
private void OnGUI()
{
//开始纵向排序
GUILayout.BeginVertical();
//开始横向排序 ID
GUILayout.BeginHorizontal();
GUILayout.Label("技能ID");
GUILayout.Label(skillData.skillId.ToString());
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 名称
GUILayout.BeginHorizontal();
skillData.skillNice = EditorGUILayout.TextField("技能名称", skillData.skillNice);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 图片地址
GUILayout.BeginHorizontal();
if (GUILayout.Button(texture, GUILayout.Width(50), GUILayout.Height(50)))
{
string path = EditorUtility.OpenFilePanel("选择图片", Application.dataPath + "/Resources/Icon/", "png");
//Debug.Log(path);
string[] arr = path.Split('/');
skillData.skillIconPath = "Icon/" + arr[arr.Length - 1].Replace(".png", "");
texture = Resources.Load<Texture>(skillData.skillIconPath);
}
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 技能类型
GUILayout.BeginHorizontal();
skillData.skillType = (SkillTypeEnum)EditorGUILayout.EnumPopup("技能类型", skillData.skillType);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 施法时间
GUILayout.BeginHorizontal();
skillData.beginAnimTime = EditorGUILayout.FloatField("开始播放动画时间", skillData.beginAnimTime);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 选择动画
GUILayout.BeginHorizontal();
anim = (AnimationClip)EditorGUILayout.ObjectField("选择指定动画", anim, typeof(AnimationClip), false);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 开始创建效果
GUILayout.BeginHorizontal();
skillData.beginEffectTime = EditorGUILayout.FloatField("创建效果时间", skillData.beginEffectTime);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 创建效果
GUILayout.BeginHorizontal();
effect = (GameObject)EditorGUILayout.ObjectField("选择指定效果", effect, typeof(GameObject), false);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 效果位置
GUILayout.BeginHorizontal();
pos = EditorGUILayout.Vector3Field("创建效果位置", pos);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 效果持续时间
GUILayout.BeginHorizontal();
skillData.effectDuration = EditorGUILayout.FloatField("效果持续时间", skillData.effectDuration);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 效果移动类型
GUILayout.BeginHorizontal();
skillData.effectMoveType = (MoveTypeEnum)EditorGUILayout.EnumPopup("效果移动类型", skillData.effectMoveType);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 效果数量
GUILayout.BeginHorizontal();
skillData.effectNum = EditorGUILayout.IntField("效果数量", skillData.effectNum);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 效果间隔时间
GUILayout.BeginHorizontal();
skillData.effectCD = EditorGUILayout.FloatField("效果间隔时间", skillData.effectCD);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 效果结束
GUILayout.BeginHorizontal();
effectEnd = (GameObject)EditorGUILayout.ObjectField("选择效果结束", effectEnd, typeof(GameObject), false);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 施法距离
GUILayout.BeginHorizontal();
skillData.castingDistance = EditorGUILayout.FloatField("施法距离", skillData.castingDistance);
//结束横向排序
GUILayout.EndHorizontal();
//开始横向排序 技能间隔时间
GUILayout.BeginHorizontal();
skillData.skillCD = EditorGUILayout.FloatField("技能间隔时间", skillData.skillCD);
//结束横向排序
GUILayout.EndHorizontal();
if (GUILayout.Button("存储"))
{
skillData.beginEffectPos = new V3(pos.x, pos.y, pos.z);
string animPath = AssetDatabase.GetAssetPath(anim);
skillData.skillAnimPath = Getpath(animPath);
string effectPath = AssetDatabase.GetAssetPath(effect);
skillData.skillEffectPath = Getpath(effectPath);
string effectEndpath = AssetDatabase.GetAssetPath(effectEnd);
skillData.endEffectPath = Getpath(effectEndpath);
list.Add(skillData);
string json = JsonConvert.SerializeObject(list);
File.WriteAllText(Application.streamingAssetsPath + "/skill.json", json);
AssetDatabase.Refresh();
}
//结束纵向排序
GUILayout.EndVertical();
}
string Getpath(string path)
{
string path2 = "Assets/Player/";
string subPath = path.Substring(path2.Length);
string[] b = subPath.Split('.');
return b[0];
}
}
把技能编辑器上的数据存入Json需要的类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum SkillTypeEnum
{
指向技能,
范围技能,
自动技能,
被动技能
}
public enum MoveTypeEnum
{
直线,
弧线
}
public struct V3
{
public float x;
public float y;
public float z;
public V3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
public class SkillData
{
public int skillId; //id
public string skillNice; //名称
public string skillIconPath;//图标地址
public SkillTypeEnum skillType;//类型
public float beginAnimTime;//开始播放动画时间
public string skillAnimPath; //动画地址
public float beginEffectTime;//开始创建效果时间
public string skillEffectPath;//效果地址
public V3 beginEffectPos;//创建效果位置
public float effectDuration;//效果持续时间
public MoveTypeEnum effectMoveType;//效果移动类型
public int effectNum;//效果数量
public float effectCD;//效果间隔时间
public string endEffectPath;//结束效果地址
public float castingDistance;//施法距离
public float skillCD;//技能释放间隔时间
}