簡體   English   中英

如何設置Texture2D圖層順序

[英]How to set Texture2D layer order

我有一個計時器,作為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);
        }
    }

刪除此:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM