之前看别人做了为ngui panel添加sorting layer,我不会做,就把他的给大家分享给大家,这样ngui和unity2d就可以完全融合在一起了,层次关系可以任意设置。
ngui的版本为3.6.8,在其中添加代码:
文件:UIDrawCall.cs 109行左右- [csharp]
- /// <summary>
- /// Renderer's sorting order, to be used with Unity's 2D system.
- /// </summary>
- public int sortingOrder
- {
- get { return (mRenderer != null) ? mRenderer.sortingOrder : 0; }
- set { if (mRenderer != null && mRenderer.sortingOrder != value) mRenderer.sortingOrder = value; }
- }
- // added by Clark
- public string sortingLayerName
- {
- get { return (mRenderer != null) ? mRenderer.sortingLayerName : "default"; }
- set
- {
- if(mRenderer != null && mRenderer.sortingLayerName != value)
- {
- mRenderer.sortingLayerName = value;
- if UNITY_EDITOR
- UnityEditor.EditorUtility.SetDirty(gameObject);
- endif
- }
- }
- }
- // added end by Clark
- [csharp]
- /// <summary>
- /// Create a new draw call, reusing an old one if possible.
- /// </summary>
- static UIDrawCall Create (string name, UIPanel pan, Material mat, Texture tex, Shader shader)
- {
- UIDrawCall dc = Create(name);
- dc.gameObject.layer = pan.cachedGameObject.layer;
- dc.baseMaterial = mat;
- dc.mainTexture = tex;
- dc.shader = shader;
- dc.renderQueue = pan.startingRenderQueue;
- dc.sortingOrder = pan.sortingOrder;
- // added by Clark
- dc.sortingLayerName = pan.sortingLayerName;
- // added end by Clark
- dc.manager = pan;
- return dc;
- }
- [csharp]
- // Clipping rectangle
- [HideInInspector][SerializeField] UIDrawCall.Clipping mClipping = UIDrawCall.Clipping.None;
- [HideInInspector][SerializeField] Vector4 mClipRange = new Vector4(0f, 0f, 300f, 200f);
- [HideInInspector][SerializeField] Vector2 mClipSoftness = new Vector2(4f, 4f);
- [HideInInspector][SerializeField] int mDepth = 0;
- [HideInInspector][SerializeField] int mSortingOrder = 0;
- // added by Clark
- [HideInInspector][SerializeField] string mSortingLayerName;
- // added end by Clark
- [csharp]
- public int sortingOrder
- {
- get
- {
- return mSortingOrder;
- }
- set
- {
- if (mSortingOrder != value)
- {
- mSortingOrder = value;
- if UNITY_EDITOR
- NGUITools.SetDirty(this);
- endif
- UpdateDrawCalls();
- }
- }
- }
- // added by Clark
- public string sortingLayerName
- {
- get { return mSortingLayerName; }
- set
- {
- if(mSortingLayerName != value)
- {
- mSortingLayerName = value;
- if UNITY_EDITOR
- NGUITools.SetDirty(this);
- endif
- UpdateDrawCalls();
- }
- }
- }
- // added end by Clark
- [csharp]
- for (int i = 0; i < drawCalls.Count; ++i)
- {
- UIDrawCall dc = drawCalls[i];
- Transform t = dc.cachedTransform;
- t.position = pos;
- t.rotation = rot;
- t.localScale = scale;
- dc.renderQueue = (renderQueue == RenderQueue.Explicit) ? startingRenderQueue : startingRenderQueue + i;
- dc.alwaysOnScreen = alwaysOnScreen &&
- (mClipping == UIDrawCall.Clipping.None || mClipping == UIDrawCall.Clipping.ConstrainButDontClip);
- dc.sortingOrder = mSortingOrder;
- // added by Clark
- dc.sortingLayerName = mSortingLayerName;
- // added end by Clark
- }
- [csharp]
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- // added by Clark
- using System;
- using System.Reflection;
- using UnityEditorInternal;
- // added end by Clark
- [csharp]
- UIWidget.Pivot mDragPivot = UIWidget.Pivot.Center;
- GUIStyle mStyle0 = null;
- GUIStyle mStyle1 = null;
- // added by Clark
- string[] sortingLayerNames;
- // added end by Clark
- [csharp]
- protected override void OnEnable ()
- {
- base.OnEnable();
- mPanel = target as UIPanel;
- // added by Clark
- sortingLayerNames = GetSortingLayerNames();
- // added end by Clark
- }
- [csharp]
- GUI.changed = false;
- int so = EditorGUILayout.IntField("Sort Order", mPanel.sortingOrder, GUILayout.Width(120f));
- if (GUI.changed) mPanel.sortingOrder = so;
- // added by Clark
- int sortingLayerIndex = GetSortingLayerIndex(mPanel.sortingLayerName);
- int newSortingLayerIndex = EditorGUILayout.Popup("Sorting Layer", sortingLayerIndex, sortingLayerNames);
- if(sortingLayerIndex != newSortingLayerIndex)
- {
- mPanel.sortingLayerName = sortingLayerNames[newSortingLayerIndex];
- }
- // added end by Clark
- [csharp]
- // added by Clark
- string[] GetSortingLayerNames()
- {
- Type t = typeof(InternalEditorUtility);
- PropertyInfo prop = t.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
- return (string[])prop.GetValue(null, null);
- }
- int GetSortingLayerIndex(string layerName)
- {
- for(int i = 0; i < sortingLayerNames.Length; i++)
- {
- if(sortingLayerNames[i] == layerName)
- {
- return i;
- }
- }
- return 0;
- }
- // added end by Clark