之前看别人做了为ngui panel添加sorting layer,我不会做,就把他的给大家分享给大家,这样ngui和unity2d就可以完全融合在一起了,层次关系可以任意设置。

ngui的版本为3.6.8,在其中添加代码:

文件:UIDrawCall.cs  109行左右
  1. [csharp] 
  2. /// <summary>
  3. /// Renderer's sorting order, to be used with Unity's 2D system.
  4. /// </summary>
  5. public int sortingOrder
  6. {
  7. get { return (mRenderer != null) ? mRenderer.sortingOrder : 0; }
  8. set { if (mRenderer != null && mRenderer.sortingOrder != value) mRenderer.sortingOrder = value; }
  9. }
  10. // added by Clark
  11. public string sortingLayerName
  12. {
  13. get { return (mRenderer != null) ? mRenderer.sortingLayerName : "default"; }
  14. set
  15. {
  16. if(mRenderer != null && mRenderer.sortingLayerName != value)
  17. {
  18. mRenderer.sortingLayerName = value;
  19. if UNITY_EDITOR
  20. UnityEditor.EditorUtility.SetDirty(gameObject);
  21. endif
  22. }
  23. }
  24. }
  25. // added end by Clark
712行左右
  1. [csharp] 
  2. /// <summary>
  3. /// Create a new draw call, reusing an old one if possible.
  4. /// </summary>
  5. static UIDrawCall Create (string name, UIPanel pan, Material mat, Texture tex, Shader shader)
  6. {
  7. UIDrawCall dc = Create(name);
  8. dc.gameObject.layer = pan.cachedGameObject.layer;
  9. dc.baseMaterial = mat;
  10. dc.mainTexture = tex;
  11. dc.shader = shader;
  12. dc.renderQueue = pan.startingRenderQueue;
  13. dc.sortingOrder = pan.sortingOrder;
  14. // added by Clark
  15. dc.sortingLayerName = pan.sortingLayerName;
  16. // added end by Clark
  17. dc.manager = pan;
  18. return dc;
  19. }
文件UIPanel.cs 130行左右
  1. [csharp]
  2. // Clipping rectangle
  3. [HideInInspector][SerializeField] UIDrawCall.Clipping mClipping = UIDrawCall.Clipping.None;
  4. [HideInInspector][SerializeField] Vector4 mClipRange = new Vector4(0f, 0f, 300f, 200f);
  5. [HideInInspector][SerializeField] Vector2 mClipSoftness = new Vector2(4f, 4f);
  6. [HideInInspector][SerializeField] int mDepth = 0;
  7. [HideInInspector][SerializeField] int mSortingOrder = 0;
  8. // added by Clark
  9. [HideInInspector][SerializeField] string mSortingLayerName;
  10. // added end by Clark
252行左右
  1. [csharp] 
  2. public int sortingOrder
  3. {
  4. get
  5. {
  6. return mSortingOrder;
  7. }
  8. set
  9. {
  10. if (mSortingOrder != value)
  11. {
  12. mSortingOrder = value;
  13. if UNITY_EDITOR
  14. NGUITools.SetDirty(this);
  15. endif
  16. UpdateDrawCalls();
  17. }
  18. }
  19. }
  20. // added by Clark
  21. public string sortingLayerName
  22. {
  23. get { return mSortingLayerName; }
  24. set
  25. {
  26. if(mSortingLayerName != value)
  27. {
  28. mSortingLayerName = value;
  29. if UNITY_EDITOR
  30. NGUITools.SetDirty(this);
  31. endif
  32. UpdateDrawCalls();
  33. }
  34. }
  35. }
  36. // added end by Clark
1432行左右,在函数UpdateDrawCalls里面
  1. [csharp]
  2. for (int i = 0; i < drawCalls.Count; ++i)
  3. {
  4. UIDrawCall dc = drawCalls[i];
  5. Transform t = dc.cachedTransform;
  6. t.position = pos;
  7. t.rotation = rot;
  8. t.localScale = scale;
  9. dc.renderQueue = (renderQueue == RenderQueue.Explicit) ? startingRenderQueue : startingRenderQueue + i;
  10. dc.alwaysOnScreen = alwaysOnScreen &&
  11. (mClipping == UIDrawCall.Clipping.None || mClipping == UIDrawCall.Clipping.ConstrainButDontClip);
  12. dc.sortingOrder = mSortingOrder;
  13. // added by Clark
  14. dc.sortingLayerName = mSortingLayerName;
  15. // added end by Clark
  16. }
文件UIPanelInspector.cs 9行
  1. [csharp] 
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. // added by Clark
  6. using System;
  7. using System.Reflection;
  8. using UnityEditorInternal;
  9. // added end by Clark
39行左右
  1. [csharp]
  2. UIWidget.Pivot mDragPivot = UIWidget.Pivot.Center;
  3. GUIStyle mStyle0 = null;
  4. GUIStyle mStyle1 = null;
  5. // added by Clark
  6. string[] sortingLayerNames;
  7. // added end by Clark
49行左右
  1. [csharp] 
  2. protected override void OnEnable ()
  3. {
  4. base.OnEnable();
  5. mPanel = target as UIPanel;
  6. // added by Clark
  7. sortingLayerNames = GetSortingLayerNames();
  8. // added end by Clark
  9. }
574行左右
  1. [csharp]
  2. GUI.changed = false;
  3. int so = EditorGUILayout.IntField("Sort Order", mPanel.sortingOrder, GUILayout.Width(120f));
  4. if (GUI.changed) mPanel.sortingOrder = so;
  5. // added by Clark
  6. int sortingLayerIndex = GetSortingLayerIndex(mPanel.sortingLayerName);
  7. int newSortingLayerIndex = EditorGUILayout.Popup("Sorting Layer", sortingLayerIndex, sortingLayerNames);
  8. if(sortingLayerIndex != newSortingLayerIndex)
  9. {
  10. mPanel.sortingLayerName = sortingLayerNames[newSortingLayerIndex];
  11. }
  12. // added end by Clark
785行左右,在文件最后
  1. [csharp] 
  2. // added by Clark
  3. string[] GetSortingLayerNames()
  4. {
  5. Type t = typeof(InternalEditorUtility);
  6. PropertyInfo prop = t.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
  7. return (string[])prop.GetValue(null, null);
  8. }
  9. int GetSortingLayerIndex(string layerName)
  10. {
  11. for(int i = 0; i < sortingLayerNames.Length; i++)
  12. {
  13. if(sortingLayerNames[i] == layerName)
  14. {
  15. return i;
  16. }
  17. }
  18. return 0;
  19. }
  20. // added end by Clark