using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Editor;
using Unity.Plastic.Newtonsoft.Json;
using UnityEditor;
using UnityEngine;
public class SkillEditor : EditorWindow
{
static SkillData skill;//技能数据
//动画名称集合
static List<string> anims = new List<string>();
//技能列表
static List<SkillData> list;
//技能图标
static Texture texture;
//打开技能编辑器
[MenuItem("Tools/SkillEditor")]
public static void Init()
{
//创建空数据技能
skill = new SkillData();
//加载json技能列表
TextAsset text = Resources.Load<TextAsset>("skill");
if (text==null)
{
list = new List<SkillData>();
}
else
{
list = JsonConvert.DeserializeObject<List<SkillData>>(text.text);
}
//设置技能id为己有技能的数量
skill.id = list.Count;
//创建编辑器窗口
var win = GetWindow<SkillEditor>();
//打开窗口
win.Show();
//加载全部动画地址
string[] path = System.IO.Directory.GetFiles(Application.dataPath + "/Resources/Anim", "*.anim");
foreach (var clip in path)
{
anims.Add(clip.Split('\\')[1].Split('.')[0]);
}
//设置默认图片
texture = Resources.LoadAll<Texture>("icon")[0];
}
int animid;
private void OnGUI()
{
//添加纵向布局
GUILayout.BeginVertical();
//显示文字
GUILayout.Label("技能id:"+skill.id);
//添加横向布局
GUILayout.BeginHorizontal();
//显示文字
GUILayout.Label("技能名称");
//添加文字输入框
skill.nick = GUILayout.TextField(skill.nick, GUILayout.Width(200));
//结束横向布局
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("技能描述");
skill.des = GUILayout.TextField(skill.des, GUILayout.Width(200));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
//添加图片按钮 并设置宽高
if (GUILayout.Button(texture,GUILayout.Width(50),GUILayout.Height(50)))
{
//打开指定文件夹选择图片 并返回地址
string path = EditorUtility.OpenFilePanel("选择图片", Application.dataPath + "Resources/icon/", "png");
string[] a = path.Split('/');
string[] b = a[a.Length - 1].Split('.');
Debug.Log(b[0]);
texture = Resources.Load<Texture>("icon/" + b[0]);
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("技能CD");
//添加float输入框
skill.CD = EditorGUILayout.FloatField(skill.CD,GUILayout.Width(200));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("动作");
//添加下拉栏
animid = EditorGUILayout.Popup(animid, anims.ToArray());
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("前摇");
skill.delay=EditorGUILayout.FloatField(skill.delay,GUILayout.Width(200));
GUILayout.EndHorizontal();
//添加文字按钮
if (GUILayout.Button("保存"))
{
//赋值动画路径
skill.animpath = "Anim/" + anims[animid];
Debug.Log(skill);
//技能列表写出json
list.Add(skill);
string json = JsonConvert.SerializeObject(list);
File.WriteAllText(Application.dataPath+"/Resources/skill.json",json);
AssetDatabase.Refresh();
}
//结束纵向布局
GUILayout.EndVertical();
}
}
技能SkillData数据
using Unity.IO.LowLevel.Unsafe;
namespace Editor
{
public enum Shape
{
fang,
yuan
}
public enum Track
{
直线,
弧线,
}
public class SkillData
{
//技能id
public int id;
//技能名称
public string nick;
//技能描述
public string des;
//技能图标 路径
public string iconpath;
//技能CD
public float CD;
//技能动画
public string animpath;
//技能个数
public int num;
//发射间隔 0同时发射
public float interval;
//前摇 延迟创建时间
public float delay;
//技能施法距离 0为近战
public float distance;
//施法时间
public float casttimes;
//轨迹
public Track track;
//弹道效果 路径
public string effectpath;
//攻击范围
public float area;
//攻击范围形状
public Shape shape;
//持续时间
public float duration;
//范围效果
public string areaeffactpath;
//Buffid数组
public int[] buffs;
}
}
SkillMgr 在剪辑器保存了json文件之后,在解析json文件,把数据在Unity上展示,这个脚本挂在一个空物体上
```using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Newtonsoft.Json;
namespace Editor
{
public class SkillMgr : MonoBehaviour
{
public static Dictionary skillDic = new Dictionary();
public List str1=new List();
private void Start()
{
ReadJson();
skillDic.Add(0,str1[0]);
}
void ReadJson()
{
string str = File.ReadAllText(Application.dataPath + "/Resources/skill.json");
str1= JsonConvert.DeserializeObject>(str);
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/17f5df006a0a438192e4bd91f6c6caf6.png)
在UI界面上创建一个Button,挂上这个脚本,触发发送消息
同时 在Untiy上放置一个动画人物,挂上这个脚本
```csharp
```csharp
using System;
using System.Dynamic;
using UnityEngine;
namespace Editor
{
public class AnimMgr : MonoBehaviour
{
RuntimeAnimatorController rac;
private void Start()
{
//加载动画控制器
rac = Resources.Load("Anim/Anim");
//设置默认动画控制器
GetComponent().runtimeAnimatorController = rac;
//设置默认动画
SetDefault();
//监听动画
MessageCenter.Ins.Add(1001,Skill);
}
//技能动画
public void Skill(object obj)
{
object[] arr = obj as object[];
int skillid = (int) arr[0];
//获取技能对应动画地址
string path = SkillMgr.skillDic[skillid].animpath;
//设置动画
SetAnim(path);
}
public void SetAnim(string path)
{
//根据路径加载动画
AnimationClip clip = Resources.Load(path);
//重写控制器
AnimatorOverrideController aoc = new AnimatorOverrideController();
//设置为默认控制器
aoc.runtimeAnimatorController = rac;
//把默认动画改为加载的动画
aoc["Default"] = clip;
//清空
GetComponent().runtimeAnimatorController = null;
//赋值
GetComponent().runtimeAnimatorController = aoc;
//播放指定动画
GetComponent().Play("Default");
//获取动画时长
float time = clip.length;
//判断正在播放的是不是默认动画
Debug.Log(GetComponent().GetCurrentAnimatorClipInfo(0)[0].clip.name);
if (GetComponent().GetCurrentAnimatorClipInfo(0)[0].clip.name!="Hug01")
{
//在动画播放结束后执行默认动画
Invoke("SetDefault",time);
}
}
public void SetDefault()
{
SetAnim("Anim/Hug01");
}
}
}