Unity技能编辑器(特效,音效)

技能编辑器是用于我们在创建角色后对角色进行编辑的编辑器,属于一种常见的编辑器类型,掌握技能编辑器有利于我们更好的对角色进行改造以及优化。

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

public class SkillEditorWindow : EditorWindow
{
    class PlayerEditor
    {
        public int characterIndex = 0;
        public int folderIndex = 0;
        public string characterName = string.Empty;
        public string folderName = string.Empty;
        public string characterFilter = string.Empty;
        public List charactList = new List();
        public Player playerEntity = null;
    }
    PlayerEditor player = new PlayerEditor();
    private List folderList = new List();
    public List characterList = new List();

    Dictionary> folderprefabs = new Dictionary>();

    string newSkillName = string.Empty;
    public string PlayerName = "";
    public string EnemyName = "";
    public int SelectIndex = 0;



    public int SelectEnemyIndex = 0;
    public int SelectEnemyPrefabIndex = 0;
    public List SilleEnemys = new List();
    public List EnemyPrefabs = new List();

    public string SkillName = null;
    public string Names = "";
    SkillWindow skillWindow;
   
    [MenuItem("Tools/Skill _%Q")]
    public static void Init() 
    {
        if (Application.isPlaying) 
        {
            var s = GetWindow("SkillEditor");
            if (s != null)
            {
                s.Show();
            }
        } 
    }
    public void OnEnable()
    {
        SerachFolder();
        GetFileFlder();
    }

    private void GetFileFlder()
    {
        folderList.Clear();
        folderList.Add("All");
        string[] folders = Directory.GetDirectories(GetPath());
        foreach (var item in folders)
        {
            folderList.Add(Path.GetFileName(item));
        }
    }

    private void SerachFolder()
    {
        string[] FolderbNameas = Directory.GetFiles(GetPath(), "*.prefab", SearchOption.AllDirectories);
        characterList.Clear();
        foreach (var item in FolderbNameas)
        {
            characterList.Add(Path.GetFileNameWithoutExtension(item));
        }
        characterList.Sort();
        characterList.Insert(0, "null");
        player.charactList.AddRange(characterList);
    }

    string GetPath()
    {
        return Application.dataPath + "/GameDate/Model";
    }
 
    private void OnGUI()
    {
        GUILayout.Button("ChangeCameraParam");
        GUILayout.Label("Player");
        GUILayout.BeginHorizontal();
        PlayerName = GUILayout.TextField(PlayerName, GUILayout.Width(100), GUILayout.Height(20));
        int folderIndex = EditorGUILayout.Popup(player.folderIndex, folderList.ToArray());
        if (folderIndex != player.folderIndex)
        {
            player.folderIndex = folderIndex;
            player.characterIndex = -1;
            string folderName = folderList[player.folderIndex];
            List lists;
            if (folderName.Equals("all"))
            {
                lists = characterList;
            }
            else
            {
                if (!folderprefabs.TryGetValue(folderName, out lists))
                {
                    lists = new List();
                    string[] files = Directory.GetFiles(GetPath() + "/" + folderName, "*.prefab", SearchOption.AllDirectories);
                    foreach (var item in files)
                    {
                        lists.Add(Path.GetFileNameWithoutExtension(item));
                    }
                    folderprefabs[folderName] = lists;
                }
            }
            player.charactList.Clear();
            player.charactList.AddRange(lists);
        }

        SelectIndex = EditorGUILayout.Popup(player.characterIndex,player.charactList.ToArray(), GUILayout.Width(120), GUILayout.Height(20));
        if (SelectIndex!=player.characterIndex) 
        {
            player.characterIndex = SelectIndex;
            if (player.characterName!=player.charactList[player.characterIndex])
            {
                player.characterName = player.charactList[player.characterIndex];
                if (!string.IsNullOrEmpty(player.characterName)) 
                {
                    if (player.playerEntity!=null) 
                    {
                        player.playerEntity.Destroy();
                    }
                    player.playerEntity = Player.Init(player.characterName);
                }
            }
        }
     
    
        GUILayout.EndHorizontal();
        GUILayout.Space(5);

        GUILayout.Label("Enemy");
        SilleEnemys.Add("all");
        SilleEnemys.Add("Enemy");
        EnemyPrefabs.Add("null");
        EnemyPrefabs.Add("Teddy");
        GUILayout.BeginHorizontal();
        EnemyName = GUILayout.TextField(EnemyName, GUILayout.Width(100), GUILayout.Height(20));
        SelectEnemyIndex = EditorGUILayout.Popup(SelectEnemyIndex, SilleEnemys.ToArray(), GUILayout.Width(120), GUILayout.Height(20));
        SelectEnemyPrefabIndex = EditorGUILayout.Popup(SelectEnemyPrefabIndex, EnemyPrefabs.ToArray(), GUILayout.Width(120), GUILayout.Height(20));
        GUILayout.EndHorizontal();
        GUILayout.Space(3);

        GUILayout.Label("New");
        GUILayout.BeginHorizontal();
        SkillName= GUILayout.TextField(SkillName, GUILayout.Width(200), GUILayout.Height(20));
        if (GUILayout.Button("New")) 
        {
            if (!string.IsNullOrEmpty(SkillName) &&player.playerEntity!=null) 
            {
                List skills = player.playerEntity.AddNewSkills(SkillName);
                SkillName = "";
            }

        }
        GUILayout.EndHorizontal();
        GUILayout.Space(3);
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Sa ve", GUILayout.Width(110), GUILayout.Height(20)))
        {
            player.playerEntity.SaveSkill();
        }

        if (GUILayout.Button("Save As", GUILayout.Width(110), GUILayout.Height(20)))
        {

        }
        if (GUILayout.Button("Delete", GUILayout.Width(110), GUILayout.Height(20)))
        {

        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("ActFilter");
        Names= GUILayout.TextField(Names, GUILayout.Width(100), GUILayout.Height(20));
        GUILayout.EndHorizontal();
        if (player.playerEntity!=null) 
        {
            Pos = GUILayout.BeginScrollView(Pos, false, true);
            foreach (var item in player.playerEntity.skillList)
            {
                GUILayout.BeginHorizontal();
                if (GUILayout.Button(item.Key))
                {
                    Names = item.Key;
                    List skillComponet = player.playerEntity.GetSkill(item.Key);
                    foreach (var ite in skillComponet)
                    {
                        ite.Init();
                    }
                    OpenSkillWindow(item.Key, skillComponet);
                }
                GUILayoutOption[] options = new GUILayoutOption[]
                {
                     GUILayout.Width(60),
                   GUILayout.Height(20)
                };
                if (GUILayout.Button("删除技能",options))
                {
                    player.playerEntity.RemoveSkill(item.Key);
                    break;
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();
        }
    }
    Vector2 Pos = new Vector2(0, 0);

    private void OpenSkillWindow(string skillName, List skillCompoent)
    {
        if (skillCompoent != null)
        {
            if (skillWindow == null)
            {
                skillWindow = GetWindow("");
            }
            skillWindow.titleContent = new GUIContent(skillName);
            skillWindow.SetiniySkill(skillCompoent, player.playerEntity);
            skillWindow.Show();
            skillWindow.Repaint();
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;

public class SkillWindow : EditorWindow
{
    string[] skills =new string[] { "低", "中", "高" };
    string[] comos = new string[] { "null","动画", "特效","音效" };
    List skillComPonents=new List();
    public List FolderList = new List();
    public int SelectIndex = 0;
    public int Folderindex = 0;
    public float speed = 1;
    public float speed1 = 0;
    public bool isok = true;
    public bool isOpen = true;
    public string str = "AnimationQueue";
    Player _player;
    public AnimationClip anim;

    public Skill_Anim Anim;
    public void SetiniySkill(List skillCompoent,Player player)
    {
        _player = player;
        speed = 1;
        skillComPonents = skillCompoent;
        _player.currSkillComponts = skillComPonents;
    }
    private void OnGUI()
    {
        FolderList.Add(null);
        GUILayout.BeginHorizontal();

        if (GUILayout.Button("播放", GUILayout.Width(130), GUILayout.Height(20)))
        {
            foreach (var item in skillComPonents)
            {
                item.Play();
            }
        }
        if (GUILayout.Button("停止", GUILayout.Width(130), GUILayout.Height(20)))
        {
            foreach (var item in skillComPonents)
            {
                item.Stop();
            }
        }
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();

        _player.times = GUILayout.TextField(_player.times);

        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();

        _player.quanzhong = GUILayout.TextField(_player.quanzhong);

        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        GUILayout.Label("特效等级");
        SelectIndex = EditorGUILayout.Popup(SelectIndex, skills, GUILayout.Width(120), GUILayout.Height(20));
        GUILayout.EndHorizontal();    

        GUILayout.BeginHorizontal();
        GUILayout.Label("播放速度");
        float Speed=EditorGUILayout.Slider(speed, 0, 3);
        if (Speed!= speed)
        {
            speed = Speed;
            Time.timeScale = speed;
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();

        GUILayout.Label("动画时间轴");
        GUILayout.Label(speed1.ToString(), GUILayout.Width(50));
        float value = GUILayout.HorizontalSlider((float)Math.Round(speed1, 2), 0, 3);
        if (Mathf.Abs(value - speed1) > 0.001)
        {
            speed1 = value;
        }
        //speed1 = EditorGUILayout.Slider(speed1, 0, 5);
        //if (Mathf.Abs()) 
        //{

        //}
        GUILayout.EndHorizontal();
        GUILayout.Space(3);
        GUILayout.BeginHorizontal();
        Folderindex = EditorGUILayout.Popup(Folderindex, comos, GUILayout.Width(120), GUILayout.Height(20));
        if (GUILayout.Button("添加", GUILayout.Width(100), GUILayout.Height(20)))
        {
            switch (Folderindex)
            {
                case 1:
                    skillComPonents.Add(new Skill_Anim(_player));
                    break;
                case 2:
                    skillComPonents.Add(new Skill_Effects(_player));
                    break;
                case 3:
                    skillComPonents.Add(new Skill_Audio(_player));
                    break;
                default:
                    break;
            }
        }
        GUILayout.EndHorizontal();

        isok = EditorGUILayout.Toggle("全部展开", isok);
        if (isok) 
        {
            isOpen=EditorGUILayout.Foldout(isOpen, str);
            Pos = GUILayout.BeginScrollView(Pos, false, true);
            foreach (var item in skillComPonents)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label(item.name);
                if (GUILayout.Button("删除")) 
                {
                    skillComPonents.Remove(item);
                    break;
                }
                GUILayout.EndHorizontal();
                if (item is Skill_Anim) 
                {
                    ShowSkill_Anim(item as Skill_Anim);
                }
                if (item is Skill_Effects)
                {
                    ShowSkill_Effect(item as Skill_Effects);
                }
                if (item is Skill_Audio)
                {
                    ShowSkill_Audio(item as Skill_Audio);
                }
            }
            GUILayout.EndScrollView();
        }
    }

    Vector2 Pos = new Vector2(0, 0);
    void ShowSkill_Anim(Skill_Anim _Anim) 
    {
        _Anim.trigger = EditorGUILayout.TextField(_Anim.trigger);
        AnimationClip clip = EditorGUILayout.ObjectField(_Anim.clip, typeof(AnimationClip), false) as AnimationClip;
        if (_Anim.clip!=clip) 
        {
            _Anim.SetAnimClip(clip);
        }
    }
    void ShowSkill_Effect(Skill_Effects _Effect)
    {
        _Effect.trigger = EditorGUILayout.TextField(_Effect.trigger);
        GameObject game = EditorGUILayout.ObjectField(_Effect.game, typeof(GameObject), false) as GameObject;
        if (_Effect.game != game)
        {
            _Effect.SetGameClip(game);
        }
    }
    void ShowSkill_Audio(Skill_Audio _Audio)
    {
        _Audio.trigger = EditorGUILayout.TextField(_Audio.trigger);
        AudioClip clip = EditorGUILayout.ObjectField(_Audio.clip, typeof(AudioClip), false) as AudioClip;
        if (_Audio.clip != clip)
        {
            _Audio.SetAudioClip(clip);
        }
    }
}

上面这段代码是用于对技能进行更精确的编辑时所使用的。

下面展示一下具体的效果:

Unity技能编辑器(特效,音效)_第1张图片

Unity技能编辑器(特效,音效)_第2张图片技能编辑器大致效果就是在创建角色时进行编辑操作。

下面附上完整项目,

 Skill_Editor.zip-Unity3D文档类资源-CSDN下载

你可能感兴趣的:(unity,编辑器,游戏引擎)