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 DVFrance · Sep 04, 2012 at 09:04 AM · guitexturebackground

Automatic GUI resizing

I everybody,

I've got a GUI texture used as background for some GUI elements. I made a textre in 1280*800 to fit with the biggest smartphones screens, but this texture doesn&'t be resized when I use a smaller screen.

How could I do that ?

Tks

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 DVFrance · Sep 04, 2012 at 09:35 AM 0
Share

Tooo, I hoped there is something automatic :( But tks anyway, I'll try to find a way !

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by SomeGuy22 · Sep 04, 2012 at 01:37 PM

This is a common problem with most games, and every single Unity Game I've seen that uses GUI elements FAIL to resize the buttons for multiple resolutions. What if (On a standalone build, the player does NOT want to play in full screen?)

What you're looking for is basically what I use for all my GUI code, with a few changes.

To make a re-sizable button, we look at this code here:

 if (GUI.Button(Rect(.1 * Screen.width, .8 * Screen.height, .2 * Screen.width, .1 * Screen.height), "My Button"))

What you're looking for is a texture that fills the whole screen, so we do a similar statement:

 GUI.DrawTexture(Rect(0 * Screen.width, 0 * Screen.height, 1 * Screen.width, 1 * Screen.height), myTexture, ScaleMode.ScaleToFit, false, 0);

Which can be simplified to:

 GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), myTexture, ScaleMode.ScaleToFit, false, 0);
Comment
Add comment · Show 6 · 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 DVFrance · Sep 04, 2012 at 04:32 PM 0
Share

Tks SomeGuy22,

I tried something like that :

var myTexture ; var GObject : GameObject;

 function Start () {
     GObject = GameObject.Find("gui_background");
     myTexture = GObject.guiTexture;
     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), myTexture, Scale$$anonymous$$ode.ScaleToFit, true, 1.6);
 }


And I've got that error message :

InvalidCastException: Cannot cast from source type to destination type.

As the noob as I am, I don't understand ...

avatar image SomeGuy22 · Sep 04, 2012 at 05:51 PM 0
Share

You need to define myTexture as a Texture2D:

var myTexture : Texture2D ; var GObject : GameObject;

avatar image DVFrance · Sep 05, 2012 at 07:53 AM 0
Share

I've already tried this and Unity tell me this :

BCE0022: Cannot convert 'UnityEngine.GUITexture' to 'UnityEngine.Texture2D'.

and if I defined myTexture in the start function like this: myTexture = GObject.GetComponent(Texture2D);

I've got a null reference execption: NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.DrawTexture (Rect position, UnityEngine.Texture image, Scale$$anonymous$$ode scale$$anonymous$$ode, Boolean alphaBlend, Single imageAspect)

I thought it was because I defined my texture as a GUI texture ins$$anonymous$$d of a texture in the inspector but it doesn't change anything...

avatar image SomeGuy22 · Sep 05, 2012 at 01:35 PM 0
Share

$$anonymous$$y bad, looking at the scripting reference:

http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTexture.html

It needs to be just Texture.

var myTexture : Texture;

I don't think you should have that code in Start (). Get rid of everything in the Start function and put the GUI.DrawTexture in OnGUI() ins$$anonymous$$d. Then just drag the texture into the variable in the inspector, because you might be confusing it by grabbing it from GObject.

EDIT: Actually it might be Texture2D... try both.

avatar image DVFrance · Sep 05, 2012 at 02:18 PM 0
Share

I'm such an as...le :)

I found the problem, I just put the line :

     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), nomTexture, Scale$$anonymous$$ode.StretchToFill, true, 0);

In the GUI function and not in the start one :

     var GObject : GameObject;
     var myTexture : GUITexture;
     var nomTexture : Texture;
 
 function Start () {
 
     GObject = GameObject.Find("gui_background");
     myTexture = this.GetComponent(GUITexture);
     nomTexture = myTexture.texture;
     
 }
 
 function OnGUI () {
     
     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), nomTexture, Scale$$anonymous$$ode.StretchToFill, true, 0);
 
 }

But it create an other problem, I can't ajust the depht, it seems to stay on forground !

Show more comments

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Overlaping GUITexture, help! 1 Answer

transparent background with GUI.DrawTexture 0 Answers

How to place/add a HD image/picture in background of my game? 0 Answers

background GUI texture 2 Answers

Changing Font, Background/Style of GUI Buttons 1 Answer


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