http://blog.csdn.net/xtxy/article/details/38332801
接着上一篇文章的问题,看到了老外做的一个补丁,为ngui panel添加sorting layer,我也照着做了一个,这样ngui和unity2d就可以完全融合在一起了,层次关系可以任意设置。
ngui的版本为3.6.8,在其中添加代码:
文件:UIDrawCall.cs 109行左右
/// <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
712行左右
/// <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;
}
文件UIPanel.cs 130行左右
// 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
252行左右
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
1432行左右,在函数UpdateDrawCalls里面
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
}
文件UIPanelInspector.cs 9行
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
// added by Clark
using System;
using System.Reflection;
using UnityEditorInternal;
// added end by Clark
39行左右
UIWidget.Pivot mDragPivot = UIWidget.Pivot.Center;
GUIStyle mStyle0 = null;
GUIStyle mStyle1 = null;
// added by Clark
string[] sortingLayerNames;
// added end by Clark
49行左右
protected override void OnEnable ()
{
base.OnEnable();
mPanel = target as UIPanel;
// added by Clark
sortingLayerNames = GetSortingLayerNames();
// added end by Clark
}
574行左右
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
785行左右,在文件最后
// 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
}
这样,在UIPanel里面就多一个选项sorting layer:
完整的修改好的文件我放到我的资源里面了。