[关闭]
@daaoling 2017-01-13T20:44:56.000000Z 字数 3167 阅读 3855

Unity NGUI

NGUI Mesh 粒子 Spine 层级解决方案

render order Vs render queue

ngui 层级排序原理

QQ20160828-0@2x.png-527kB

代码实现

本质就是把 mesh 的渲染序列加入到uipanel里面,粒子也是一样

RenderQueueModifier.cs

using UnityEngine;
using System.Collections;

public class RenderQueueModifier : MonoBehaviour {



    ///必须设置 或者 由代码设置
    public UIPanel m_panel;
    public UIWidget m_target;
    public bool isForSpine = true;



    public Renderer renderer;
    public SkeletonRenderer skeletonRender;
    void Awake() {
        if (isForSpine) { 
            skeletonRender = GetComponent<SkeletonRenderer>(); 
        }
        else {
            renderer = GetComponent<Renderer>();
        }
    }


    void OnEnable() {
        AddToPanel();
    }
    public void Set(UIPanel m_panel, UIWidget m_target, bool isForSpine) 
    {
        this.m_panel = m_panel;
        this.m_target = m_target;
        this.isForSpine = isForSpine;
        AddToPanel();
    }

    void AddToPanel() {
        if (m_panel != null) m_panel.renderQueueModifiers.Add(this);
    }

    void OnDisable() {

        m_panel.renderQueueModifiers.Remove(this);
    }





    int lasetQueue = int.MinValue;


    public void setQueue(int queue)
    {
        if (this.lasetQueue != queue) {
            this.lasetQueue = queue;

            if (isForSpine){
                skeletonRender.needControllerRenderQueue = true;
                skeletonRender.renderQueue = this.lasetQueue;
            }
            else {
                renderer.material.renderQueue = this.lasetQueue;
            }
        }
    }
}

UIPanel.cs

[System.NonSerialized]
public BetterList<UIDrawCall> drawCalls = new BetterList<UIDrawCall>();

[System.NonSerialized]
public BetterList<RenderQueueModifier> renderQueueModifiers = new BetterList<RenderQueueModifier>();

void UpdateDrawCalls (){


        .....


        int addRq = -1;

        for (int i = 0; i < drawCalls.size; ++i)
        {
            addRq += 1;

            UIDrawCall dc = drawCalls.buffer[i];

            Transform t = dc.cachedTransform;
            t.position = pos;
            t.rotation = rot;
            t.localScale = scale;



            dc.renderQueue = (renderQueue == RenderQueue.Explicit) ? startingRenderQueue : startingRenderQueue + addRq;
            dc.alwaysOnScreen = alwaysOnScreen &&
                (mClipping == UIDrawCall.Clipping.None || mClipping == UIDrawCall.Clipping.ConstrainButDontClip);
#if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
            dc.sortingOrder = mSortingOrder;
#endif



            BetterList<RenderQueueModifier> queueModifiers = FindModifier(dc);
            if (queueModifiers.size > 0) {
                for (int j = 0; j < this.renderQueueModifiers.size; j++) {
                    addRq += 1;

                    RenderQueueModifier rm = queueModifiers.buffer[j];
                    rm.setQueue(startingRenderQueue + addRq);
                }
            }
        }

}

public BetterList<RenderQueueModifier> FindModifier(UIDrawCall dc) 
{
    BetterList<RenderQueueModifier> list = new BetterList<RenderQueueModifier>();

    for (int i = 0; i < this.renderQueueModifiers.size; ++i)
    {
        RenderQueueModifier rm = this.renderQueueModifiers.buffer[i];
        if (rm.m_target != null && rm.m_target.drawCall != null && rm.m_target.drawCall == dc) {
            list.Add(rm);
        }
    }
    return list;
}

SkeletonRenderer.cs

/// <summary>
/// 
/// </summary>
public bool controllerMaterialByOut = false;
public Material[] repMaterials; /// material for replace

///是否需要和NGUI统一层
public bool needControllerRenderQueue = false;
public int  renderQueue = 3000;

public virtual void LateUpdate () {

    ...

    ///如果要替换材质
    if (this.controllerMaterialByOut)
    {
        for (int i = 0; i < meshRenderer.sharedMaterials.Length; i++)
        {
            var texture = meshRenderer.sharedMaterials[i].GetTexture("_MainTex");
            var alpha_texture = meshRenderer.sharedMaterials[i].GetTexture("_AlphaTex");
            this.repMaterials[i].SetTexture("_MainTex", texture);
            this.repMaterials[i].SetTexture("_AlphaTex", alpha_texture);
        }

        meshRenderer.sharedMaterials = this.repMaterials;
    }

    ///如果在 NGUI 中要动态改层级
    if (this.needControllerRenderQueue) {
        for (int i = 0; i < this.meshRenderer.sharedMaterials.Length; i++)
        {
            this.meshRenderer.sharedMaterials[i].renderQueue = renderQueue;
        }
    }
}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注