如何设置Texture2D图层顺序

时间:2015-06-04 09:06:43

标签: c# unity3d

我有一个计时器,我作为在Rect上面渲染的Texture2D绘制到屏幕上。问题是,我无法弄清楚如何让屏幕上的其他元素出现在其上方或下方。目前,它完全取决于所有内容,但我需要在其上显示菜单。

希望这不会遗漏任何相关信息。我不得不删除大量涉及计时器填充和场景其他元素的代码。

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

public class Timer : MonoBehaviour {

    public float timerWidth; // X coordinate for all timers' placement.
    public float timerHeight; // Y coordinate for timer's placement.
    Rect timeBarRect; // The bar coordinates.
    Rect blankBarRect; // The blank bar coordinates.
    Texture2D timeTexture; // Color of bar when it is filling.
    Texture2D blankBarTexture; // Bar that has yet to be filled.

    void Start (){

        timerHeight = 1.528f;
        timerWidth = 4.54f;

        cam = (GameObject.FindWithTag("MainCamera").GetComponent<Camera>()) as Camera;

        blankBarRect = new Rect(0, 0, Screen.width / 3, Screen.height / 50);
        timeBarRect = new Rect(0, 0, Screen.width / 3, Screen.height / 50);         

        timeTexture = new Texture2D (1, 1);
        timeTexture.SetPixel(0,0, Color.green);
        timeTexture.Apply();

        blankBarTexture = new Texture2D (1, 1);
        blankBarTexture.SetPixel(0,0, Color.black);
        blankBarTexture.Apply();
    }

    void Update ()
    {

        timeBarRect.x = cam.pixelWidth / timerWidth;
        timeBarRect.y = cam.pixelHeight / timerHeight;

        blankBarRect.x = cam.pixelWidth / timerWidth;
        blankBarRect.y = cam.pixelHeight / timerHeight;
    }

    void OnGUI(){ 

        blankBarRect.width = (Screen.width * .1f); 
        GUI.DrawTexture (blankBarRect, blankBarTexture);

        timeBarRect.width = ((Screen.width * .1f)); 
        GUI.DrawTexture (timeBarRect, timeTexture);
        }
    }

1 个答案:

答案 0 :(得分:1)

删除此:

void OnGUI(){ 
    blankBarRect.width = (Screen.width * .1f); 
    GUI.DrawTexture (blankBarRect, blankBarTexture);

    timeBarRect.width = ((Screen.width * .1f)); 
    GUI.DrawTexture (timeBarRect, timeTexture);
}

在Canvas

下的场景层次结构中创建一个RawImage GameObject

http://docs.unity3d.com/Manual/script-RawImage.html

然后您可以使用FindWithTag()GetComponent<RawImage>()来获取组件并使用您自己的纹理设置其Texture属性

http://docs.unity3d.com/ScriptReference/UI.RawImage.html