【unity编辑器拓展】unity spriteRender图层管理工具(支持Spine)
来源:互联网 发布:php input 获取date 编辑:程序博客网 时间:2024/06/16 05:19
使用unity做2d游戏的时候有个地方非常烦人而且经常出错,就是使用Sprite Renderer进行场景编辑的时候,图层老是重叠,或者有两个Sorting Order一样所以有时候A图层在前有时候B图层在前。
那么有没有一种方式能避免这些错误呢???于是想到做一个把所有Sprite Renderer(不局限于Sprite Render,同样对于Spine也支持)收集起来统一管理编辑的工具,下图是完成品截图:
功能如下:
1、单个显示/隐藏Sprite,确定当前编辑的是那个Sprite很有帮助
2、更加方便的更改Sorting Layer Id
3、更加方便的比对并编辑各个Order
4、直接选中当前编辑的Sprite
总结:
这样在编辑场景图层顺序时就一目了然知道有哪些相同图层或者顺序相同,也避免因为粗心导致的bug。
代码如下:
using UnityEngine;using System.Collections;using UnityEditor;using System.Collections.Generic;using System.Reflection;using System;public class LayerManagerEditor : EditorWindow {private static MethodInfo EditorGUILayoutSortingLayerField;Dictionary<string,List<Renderer>> renders=new Dictionary<string, List<Renderer>>();Dictionary<string,bool> selectDic=new Dictionary<string, bool>();SerializedObject spRenderSerializedObject;Vector2 scrollPos;Renderer[] spRenders;void OnEnable(){if(EditorGUILayoutSortingLayerField==null)EditorGUILayoutSortingLayerField = typeof(EditorGUILayout).GetMethod("SortingLayerField", BindingFlags.Static | BindingFlags.NonPublic, null, new Type[] { typeof(GUIContent), typeof(SerializedProperty), typeof(GUIStyle) }, null);spRenders = GameObject.FindObjectsOfType<Renderer> ();Resort ();}void OnGUI(){Refresh ();}void Resort(){renders.Clear ();foreach (Renderer spRender in spRenders) {if(!renders.ContainsKey(spRender.sortingLayerName)){renders[spRender.sortingLayerName]=new List<Renderer>();}renders[spRender.sortingLayerName].Add(spRender);}}void Refresh(){scrollPos = EditorGUILayout.BeginScrollView (scrollPos);EditorGUILayout.Space ();foreach (KeyValuePair<string,List<Renderer>> keyValue in renders) {string layerName=keyValue.Key;bool isSelected=false;if(!selectDic.ContainsKey(layerName)){selectDic[layerName]=true;}isSelected=selectDic[layerName];isSelected = EditorGUILayout.Foldout (isSelected, layerName);selectDic[layerName]=isSelected;if(isSelected){int length=keyValue.Value.Count;for (int i = 0; i < length; i++) {Renderer spRender=keyValue.Value[i];if(spRender==null) continue;EditorGUILayout.BeginHorizontal();bool isActive=spRender.gameObject.activeSelf;isActive=EditorGUILayout.Toggle(isActive,GUILayout.Width(10f));spRender.gameObject.SetActive(isActive);EditorGUILayout.LabelField(spRender.name,GUILayout.MaxWidth(200f));SerializedObject serializedObject = new SerializedObject (spRender);SerializedProperty serializedProperty=serializedObject.FindProperty("m_SortingLayerID");if(EditorGUILayoutSortingLayerField != null && serializedProperty != null) {EditorGUILayoutSortingLayerField.Invoke(null, new object[] { new GUIContent(""), serializedProperty, EditorStyles.popup } );} else {spRender.sortingLayerID = EditorGUILayout.IntField("Sorting Layer ID", spRender.sortingLayerID);}spRender.sortingOrder=EditorGUILayout.IntField(spRender.sortingOrder);if(GUILayout.Button("Select")){Selection.activeGameObject=spRender.gameObject;}EditorGUILayout.EndHorizontal();serializedObject.ApplyModifiedProperties();EditorUtility.SetDirty(spRender);}}}EditorGUILayout.Space ();EditorGUILayout.Space ();EditorGUILayout.Space ();EditorGUILayout.Space ();EditorGUILayout.BeginHorizontal ();EditorGUILayout.Space ();if (GUILayout.Button ("Resort",GUILayout.MaxWidth(100f))) {Resort();}EditorGUILayout.Space ();EditorGUILayout.EndHorizontal ();EditorGUILayout.EndScrollView ();}[MenuItem("Platform/Tool/LayerManager")]public static void Init() {LayerManagerEditor window = GetWindow<LayerManagerEditor>("LayerManager");window.Show();}}
1、将上面代码保存到项目Asset/Editor目录下(没有则创建)
2、在菜单栏找到Platform/Tool/LayerManager打开工具
0 0
- 【unity编辑器拓展】unity spriteRender图层管理工具(支持Spine)
- Unity SpriteRender 支持Tile平铺的Shader
- unity编辑器拓展
- Unity编辑器拓展<一>
- Unity编辑器拓展<二>
- unity拓展编辑器
- Unity SpriteRender 实现Fill效果
- Unity编辑器拓展之三:拓展Unity的Hierarchy面板
- 拓展编辑器功能--Unity自定义编辑器窗口
- 【unity拓展】使列表支持拖动排序(Reorderablelist)
- Unity中SpriteRender实现广告牌效果
- Unity SpriteRender 和Image的区别
- 【Unity&拓展编辑器&插件】开始插件,拓展编辑器教程
- Unity Spine 换图(通过外部图片)
- Spine+Unity(2D游戏)
- Spine导入Unity
- Unity 编辑器拓展 —— svn
- unity编辑器扩展篇-视图拓展基础
- Mac下使用tree命令。
- httpClient设置代理
- 常用的邮箱服务器(SMTP、POP3)域名、端口汇总
- 对比MIUI7和Flyme4.5 期待神秘的Flyme5
- 近期计划
- 【unity编辑器拓展】unity spriteRender图层管理工具(支持Spine)
- linux常用命令(10):cat命令
- 求左下角星星之和 树状数组或线段树 poj 2352 Stars
- POJ 1113 Wall(凸包)
- Spectral Clustering(学习Free Mind知识整理)
- loadrunner监控Windows之前需要做的准备工作
- Mybatis
- Windows windows7 关闭隧道适配器
- 可牛影像动感渐隐闪图教程