unity 特效制作快捷辅助工具
通过选中节点,鼠标中间点击,弹出配置好的快捷粒子,快速创建模块化粒子
上码
(注意effectFastList 集合里面的数据需要自己填充)
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEditor;
using UnityEngine;
public class EffectHelpQuick
{
[InitializeOnLoadMethod]//这个特性作用:C#编译完成后首先执行该方法
static void Install()
{
EditorApplication.hierarchyWindowItemOnGUI += onhier;
}
static PopupExample popupExample;
static void ShowHierSelectMenu(Rect pos)
{
if (popupExample == null)
{
popupExample = new PopupExample(effectFastList.Count);
}
PopupWindow.Show(pos, popupExample);
return;
}
static void onhier(int guid, Rect selectionRect)
{
if (Selection.activeInstanceID == guid)
{
var evt = Event.current;
//if (evt.type == EventType.ContextClick)//用这个触发可以替换掉右键菜单
if (evt.type == EventType.MouseDown && evt.button == 2)//鼠标中键触发
{
var mousePos = evt.mousePosition;
if (selectionRect.Contains(mousePos))
{
ShowHierSelectMenu(selectionRect);
evt.Use();
}
}
}
}
//弹出PopupWindow
public class PopupExample : PopupWindowContent
{
int width = 200;
int height = 300;
public PopupExample(int _height)
{
height = _height;
}
public override Vector2 GetWindowSize()
{
return new Vector2(width, height);
}
public override void OnGUI(Rect rect)
{
if (effectFastList != null)
{
int heightArea = 0;
for (int i = 0; i < effectFastList.Count; i++)
{
GUILayout.BeginArea(new Rect(0, heightArea, width, height), effectFastList[i].name, GUI.skin.window);
heightArea += effectFastList[i].list.Count * 17+26;
foreach (dataInfo infolist in effectFastList[i].list)
{
if (GUILayout.Button(infolist.name, EditorStyles.toolbarButton))
{
UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(infolist.path, typeof(UnityEngine.Object));
if (obj != null && obj is GameObject)
{
GameObject instGo = GameObject.Instantiate((obj as GameObject));
instGo.name = instGo.name.Replace("(Clone)", "");
GameObject cutGo = Selection.activeGameObject;
if (cutGo != null)
{
instGo.transform.parent = cutGo.transform;
}
Selection.activeGameObject = instGo;
}
if (popupExample != null)
{
popupExample.editorWindow.Close();
}
}
}
//width += 100;
GUILayout.EndArea();
}
}
}
public override void OnOpen()
{
//Debug.Log("Popup opened: " + this);
}
public override void OnClose()
{
//Debug.Log("Popup closed: " + this);
}
}
public class dataInfo
{
public string name { get; set; } //名字
public string path { get; set; } //prefab 路径
public bool show { get; set; }
public List<dataInfo> list { get; set; }
}
static List<dataInfo> effectFastList = new List<dataInfo>(); //这里的数据自己填
}