We have to combine SkinnedMeshRenderer with NGUI UIPanels in Unity 4.3.x. In this case we have to handle the sorting layers in NGUI. Is the following code a correct way to do this?
I used the SortingLayerExposed-Script (
https://gist.github.com/nickgravelyn/7460288) to set the SortingLayer for the SkinnedMashRenderer. In NGUI I did the following patch:
In UIPanel.cs I added:
67 public int sortingOrder = 0;
68 public int sortingLayerID = 0;
In UIDrawCall GetDrawCall (int index, Material mat) I added after:
397 // Create the draw call
398 UIDrawCall drawCall = go.AddComponent<UIDrawCall>();
399 drawCall.baseMaterial = mat;
400 drawCall.renderQueue = UIDrawCall.list.size;
401 drawCall.panel = this;
the lines
402 drawCall.sortingLayerID = sortingLayerID;
403 drawCall.sortingOrder = sortingOrder;
In UIDrawCall.cs I added:
43 public int sortingLayerID
44 {
45 get
46 {
47 return mSortingLayerID;
48 }
49 set
50 {
51 if (mSortingLayerID != value)
52 {
53 mSortingLayerID = value;
54
55 if (mRen != null)
56 {
57 mRen.sortingLayerID = mSortingLayerID;
58 UnityEditor.EditorUtility.SetDirty(gameObject);
59 }
60 }
61 }
62 }
63 int mSortingLayerID;
64
65 public int sortingOrder
66 {
67 get
68 {
69 return mSortingOrder;
70 }
71 set
72 {
73 if (mSortingOrder != value)
74 {
75 mSortingOrder = value;
76
77 if (mRen != null)
78 {
79 mRen.sortingOrder = mSortingOrder;
80 UnityEditor.EditorUtility.SetDirty(gameObject);
81 }
82 }
83 }
84 }
85 int mSortingOrder;
In public void Set (BetterList<Vector3> verts, BetterList<Vector3> norms, BetterList<Vector4> tans, BetterList<Vector2> uvs, BetterList<Color32> cols) I added after:
363 if (mRen == null)
364 {
365 mRen = gameObject.GetComponent<MeshRenderer>();
366 }
the lines:
368 if (mRen != null)
369 {
370 mRen.sortingLayerID = sortingLayerID;
371 mRen.sortingOrder = sortingOrder;
372 }
And after:
374 if (mRen == null)
375 {
376 mRen = gameObject.AddComponent<MeshRenderer>();
the lines:
377 mRen.sortingLayerID = sortingLayerID;
378 mRen.sortingOrder = sortingOrder;
Finally I added in UIPanelInspector.cs after:
211 if (clipping != UIDrawCall.Clipping.None && !NGUIEditorTools.IsUniform(panel.transform.lossyScale))
212 {
213 EditorGUILayout.HelpBox("Clipped panels must have a uniform scale, or clipping won't work properly!", MessageType.Error);
214
215 if (GUILayout.Button("Auto-fix"))
216 {
217 NGUIEditorTools.FixUniform(panel.gameObject);
218 }
219 }
the lines:
221 int newSortingLayerId = EditorGUILayout.IntField("SOL ID", panel.sortingLayerID);
222 if (newSortingLayerId != panel.sortingLayerID)
223 {
224 panel.sortingLayerID = newSortingLayerId;
225 UnityEditor.EditorUtility.SetDirty(panel);
226 }
227
228 int newSortingLayerOrder = EditorGUILayout.IntField("SOL Order", panel.sortingOrder);
229 if (newSortingLayerOrder != panel.sortingOrder)
230 {
231 panel.sortingOrder = newSortingLayerOrder;
232 UnityEditor.EditorUtility.SetDirty(panel);
233 }