Unity技能编辑器小进阶优化

  • Post author:
  • Post category:其他


对昨天的技能编辑器进行了简单的优化,加上了攻击等特效,实现粒子特效等,加载在json表中

在这里插入图片描述

在这里插入图片描述

写入携程,可以让他继承单例

using System;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;

namespace Editor
{
    public class ScheduleOnce:Singleton<ScheduleOnce>
    {
        int id = 0;
        Dictionary<int, Coroutine> dic = new Dictionary<int, Coroutine>();
        public void AddSchedule(MonoBehaviour self, float time, Action<object> action,params object[] arr)
        {
            int name = id++;
            Coroutine coroutine = self.StartCoroutine(DelayFun(self, name, time, action,arr));
            dic.Add(name,coroutine);
        }
        //携程函数
        IEnumerator DelayFun(MonoBehaviour self, int name, float time, Action<object> action,params object[] arr)
        {
            yield return new WaitForSeconds(time);
            action(arr);
            self.StopCoroutine(dic[name]);
        }
        public void AddSchedule(MonoBehaviour self, float time, Action action)
        {
            int name = id++;
            Coroutine coroutine = self.StartCoroutine(DelayFun(self, name, time, action));
            dic.Add(name,coroutine);
        }
        //携程函数
        IEnumerator DelayFun(MonoBehaviour self, int name, float time, Action action)
        {
            yield return new WaitForSeconds(time);
            action();
            self.StopCoroutine(dic[name]);
        }
    }
}

然后写出特效的脚本

using System;
using UnityEngine;
using DG.Tweening;

namespace Editor
{
    public class EffectMgr : MonoBehaviour
    {
        private void Start()
        {
            //监听效果
            MessageCenter.Ins.Add(1001,Skill);
        }
        SkillData skill;
        public GameObject begin;
        public GameObject[] end;
        //技能效果
        public void Skill(object obj)
        {
            object[] arr = obj as object[];
            int skillid = (int) arr[0];
            //获取技能
            skill = SkillMgr.skillDic[skillid];
            ScheduleOnce.Ins.AddSchedule(this,skill.delay,OneSkill);
        }

        public void OneSkill()
        {
            //执行次数
            for (int i = 0; i < skill.num; i++)
            {
                Invoke("TwoSkill",i*skill.interval);
                ScheduleOnce.Ins.AddSchedule(this,i*skill.interval,TwoSkill,i);
            }
        }

        public void TwoSkill(object obj)
        {
            object[] arr = obj as object[];
            int i = (int)arr[0];
            //是否换弹道
            if (skill.distance!=0)
            {
                GameObject effect = Instantiate(Resources.Load<GameObject>(skill.effectpath));
                Vector3 beginpos = begin.transform.position + begin.transform.forward;
                effect.transform.position =beginpos;
                Vector3 endpos;
                //没目标向最远距离空放
                if (end.Length==0)
                {
                    endpos = begin.transform.position + begin.transform.forward * skill.distance;
                }
                else if (end.Length>i)
                {
                    endpos = end[i % end.Length].transform.position;
                }
                else
                {
                    endpos = end[i].transform.position;
                }
                Destroy(effect,skill.casttimes);
                switch (skill.track)
                {
                    case Track.直线:
                        //线性差值移动
                        effect.transform.DOMove(endpos, skill.casttimes);
                        break;
                    case Track.弧线:
                        //贝赛尔曲线移动
                        Vector3 pos = Vector3.Lerp(beginpos,endpos, 0.5f) + Vector3.up * 2;
                        Vector3[] posarr = new Vector3[] {beginpos,pos,endpos};
                        DOTween.To((t) =>
                        {
                            effect.transform.position = BasselCurve(posarr, t);
                        }, 0, 1, skill.casttimes);
                        break;
                    default:
                        break;
                }
            }
            ScheduleOnce.Ins.AddSchedule(this,skill.casttimes,ThreeSkill,i);
        }
        public void ThreeSkill(object obj)
        {
            object[] arr = obj as object[];
            int i = (int) arr[0];
            GameObject areaeffect = Instantiate(Resources.Load<GameObject>(skill.areaeffactpath));
            if (skill.distance==0)
            {
                areaeffect.transform.position = begin.transform.position;
            }
            else
            {
                areaeffect.transform.position = end[i].transform.position;
            }
            Destroy(areaeffect,skill.duration);
        }
        //贝赛尔曲线
        public Vector3 BasselCurve(Vector3[] pos, float t)
        {
            Vector3[] arr = new Vector3[pos.Length - 1];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = pos[i] * (1 - t) + pos[i + 1] * t;
                Debug.DrawLine(pos[i],pos[i+1],Color.red);
            }

            if (arr.Length==1)
            {
                return arr[0];
            }
            else
            {
                return BasselCurve(arr, t);
            }
        }
    }
}
之后略微修改一下编辑器就可以了
![在这里插入图片描述](https://img-blog.csdnimg.cn/d834db4d0f824952991d2e56bcde8aad.png)



版权声明:本文为DecadeWizard原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。