(转载)NGUI和Mesh和粒子和Spine层级解决方案

NGUI Mesh 粒子 Spine 层级解决方案

render order Vs render queue

ngui 层级排序原理


(转载)NGUI和Mesh和粒子和Spine层级解决方案_第1张图片

代码实现

本质就是把 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();

        }

        else {

            renderer = GetComponent();

        }

    }

    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 drawCalls = new BetterList();

[System.NonSerialized]

public BetterList renderQueueModifiers = new BetterList();

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 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 FindModifier(UIDrawCall dc)

{

    BetterList list = new BetterList();

    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

///

///

///

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;

        }

    }

}

你可能感兴趣的:((转载)NGUI和Mesh和粒子和Spine层级解决方案)