- Home /
How to draw texture in C# without using (new Rect)
I have been using UnityScript for a basic loading screen and it has worked fine. I am getting better at C# now however and I am working on converting all of my scripts over. I have hit a snag though, and I think I know what is causing the problem but I'm not sure how to fix it. When it goes to the screen everything draws, it just shows a still image for the loading bar that previously worked in UnityScript. I THINK it is due to the fact that while parsing it throws errors if you don't use (new Rect (vars, blah, blah)), however in UnityScript it is fine. I have pasted the UnityScript (working) one and the C# (not working) down below. I hope I was clear enough, thanks in advance.
UnityScript
#pragma strict
private var progress : float = 0;
private var height = Screen.height / 2 + 100;
private var heightLogo = Screen.height / 2 - 250;
private var isLoading : boolean = true;
var progressBarSize : Vector2 = new Vector2 (400, 40);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;
var companyLogo : Texture2D;
var gameLogo : Texture2D;
var splashText : Texture2D;
var backgroundImage : Texture2D;
function Awake ()
{
Application.targetFrameRate = 60;
}
function Start ()
{
var menuSelect = Random.Range(1, 3);
yield WaitForSeconds (15);
isLoading = false;
yield WaitForSeconds (1);
Application.LoadLevel (menuSelect);
}
function OnGUI()
{
GUI.DrawTexture (Rect (0, 0, Screen.width, Screen.height), backgroundImage);
if (isLoading == true)
{
GUI.DrawTexture (Rect (Screen.width / 2 - 200, height, progressBarSize.x, progressBarSize.y), progressBarEmpty);
GUI.BeginGroup (new Rect (Screen.width / 2 - 200, height, progressBarSize.x * Mathf.Clamp01(progress), progressBarSize.y));
GUI.DrawTexture (new Rect (0, 0, progressBarSize.x, progressBarSize.y), progressBarFull);
GUI.EndGroup();
}
GUI.DrawTexture (Rect (Screen.width / 2 - 450, heightLogo, 256, 256), companyLogo);
GUI.DrawTexture (Rect (Screen.width / 2 + 200, heightLogo, 256, 256), gameLogo);
GUI.DrawTexture (Rect (Screen.width / 2 - 100, Screen.height /2 - 100, 200, 80), splashText);
}
function Update()
{
progress = Time.time * 0.07;
}
C#
using UnityEngine;
using System.Collections;
public class LoadScreen : MonoBehaviour
{
private float progress = 0;
private int height = Screen.height / 2 + 100;
private int heightLogo = Screen.height / 2 - 250;
private bool isLoading = true;
public Vector2 progressBarSize = new Vector2 (400, 40);
public Texture2D progressBarEmpty;
public Texture2D progressBarFull;
public Texture2D companyLogo;
public Texture2D gameLogo;
public Texture2D splashText;
public Texture2D backgroundImage;
void Awake ()
{
Application.targetFrameRate = 60;
}
void Start ()
{
StartCoroutine (WaitCoroutine ());
}
void OnGUI ()
{
GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), backgroundImage);
if (isLoading == true)
{
GUI.DrawTexture (new Rect (Screen.width / 2 - 200, height, progressBarSize.x, progressBarSize.y), progressBarEmpty); // HERE IS WHAT I THINK THE ISSUE IS
GUI.BeginGroup (new Rect (Screen.width / 2 - 200, height, progressBarSize.x * Mathf.Clamp01(progress), progressBarSize.y));
GUI.DrawTexture (new Rect (0, 0, progressBarSize.x, progressBarSize.y), progressBarFull);
GUI.EndGroup();
}
GUI.DrawTexture (new Rect (Screen.width / 2 - 450, heightLogo, 256, 256), companyLogo);
GUI.DrawTexture (new Rect (Screen.width / 2 + 200, heightLogo, 256, 256), gameLogo);
GUI.DrawTexture (new Rect (Screen.width / 2 - 100, Screen.height /2 - 100, 200, 80), splashText);
}
void Update ()
{
}
IEnumerator WaitCoroutine()
{
int menuSelect = Random.Range(1, 3);
yield return new WaitForSeconds (15);
isLoading = false;
yield return new WaitForSeconds (1);
Application.LoadLevel (menuSelect);
yield break;
}
}
$$anonymous$$Aybe i missed something, but what is your actual problem / what doesn't work? Any compile errors? Any runtime errors? What behaviour do you get which you didn't expect?
As Eric said in C# you have to use the "new" keyword, so could you please edit your question and actually ask one that can be answered, thanks :)
Answer by Eric5h5 · Sep 12, 2014 at 01:41 AM
You don't do it without using "new Rect". There's no issue with doing that (it's allocated on the stack, not the heap), and Unityscript does the same thing...regardless of whether you explicitly write it out "new" or not, the code behaves as if it's always there anyway. Whenever you create a new instance of anything, well, it's always a new instance, so the "new" bit is superfluous.
Ok, but UnityScript doesn't give an error without using new, C# requires me to use new, that's why I said that. But the issue still remains, thanks for the clarification though.
Like I said, it's not an issue. I explained that Unityscript implicitly always uses "new" whether you write it out or not. Regardless of whether you write "Rect(1,2,3,4)" or "new Rect(1,2,3,4)", the actual code is always "new Rect(1,2,3,4)".
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How To make a Ui button Chance colours when pressed 1 Answer
Multiple Cars not working 1 Answer
RTS rectangle selection system 3 Answers
GUI Label Width & Height 0 Answers