【unity编辑器拓展】unity spriteRender图层管理工具(支持Spine)

使用unity做2d游戏的时候有个地方非常烦人而且经常出错,就是使用Sprite Renderer进行场景编辑的时候,图层老是重叠,或者有两个Sorting Order一样所以有时候A图层在前有时候B图层在前。


那么有没有一种方式能避免这些错误呢???于是想到做一个把所有Sprite Renderer(不局限于Sprite Render,同样对于Spine也支持)收集起来统一管理编辑的工具,下图是完成品截图:


【unity编辑器拓展】unity spriteRender图层管理工具(支持Spine)_第1张图片


功能如下:

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> renders=new Dictionary>();
	Dictionary selectDic=new Dictionary();
	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 ();
		Resort ();
	}

	void OnGUI(){
		Refresh ();
	}

	void Resort(){
		renders.Clear ();
		foreach (Renderer spRender in spRenders) {
			if(!renders.ContainsKey(spRender.sortingLayerName)){
				renders[spRender.sortingLayerName]=new List();
			}
			renders[spRender.sortingLayerName].Add(spRender);
		}
	}

	void Refresh(){
		scrollPos = EditorGUILayout.BeginScrollView (scrollPos);
		EditorGUILayout.Space ();
		foreach (KeyValuePair> 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("LayerManager");
		window.Show();
	}
}


使用方法:
1、将上面代码保存到项目Asset/Editor目录下(没有则创建)

2、在菜单栏找到Platform/Tool/LayerManager打开工具

你可能感兴趣的:(unity3d,Unity拓展,游戏开发踩坑记录)