Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Quad Bore Studios · Sep 12, 2014 at 01:21 AM · c#javascriptgui

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;
     }
 }
 


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Sep 12, 2014 at 02:00 AM 0
Share

$$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 :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Quad Bore Studios · Sep 12, 2014 at 01:53 AM 0
Share

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.

avatar image Eric5h5 · Sep 12, 2014 at 01:58 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges