Reputation: 545
I have a timer that I draw to the screen as a Texture2D rendered atop a Rect. The problem is that I can't figure out how to let other elements on the screen appear over or under it. Currently, it's drawn on top of absolutely everything but I need the menu to appear over it.
Hopefully this is not missing any relevant info. I had to delete a ton of code that involves the timer filling and other elements of the scene.
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);
}
}
Upvotes: 1
Views: 194
Reputation: 2683
delete this:
void OnGUI(){
blankBarRect.width = (Screen.width * .1f);
GUI.DrawTexture (blankBarRect, blankBarTexture);
timeBarRect.width = ((Screen.width * .1f));
GUI.DrawTexture (timeBarRect, timeTexture);
}
and make a RawImage GameObject in your scene hierarchy under Canvas
http://docs.unity3d.com/Manual/script-RawImage.html
you can then use FindWithTag()
and GetComponent<RawImage>()
to get the component and set its Texture property with your own texture
http://docs.unity3d.com/ScriptReference/UI.RawImage.html
Upvotes: 1