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 nTu4Ka · Feb 02, 2014 at 10:01 PM · button backround

GameObject to Texture

Hi,

I have a folder, containing several PNG images. I need them to be used as a background of a GUI.Box and they will be changed at runtime.

For example. I have 3 buttons. An image name is attached to every button. When pressing it, it should tell Box to use this image as BG.

Is there a way to load this images as textures using their names?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Feb 02, 2014 at 10:47 PM

Simply create public Texture variables and change which one is used based on your button. As an example. Attach it to an empty game object. Then drag and drop textures onto the 'tex1', 'tex2', and 'tex3' variables in the Inspector:

 #pragma strict
 
 var tex1 : Texture;
 var tex2 : Texture;
 var tex3 : Texture;
 
 private var currTex : Texture;
 
 function Start() {
     currTex = tex1;
 }
 
 function OnGUI() {
     GUI.DrawTexture(Rect(0,0, Screen.width, Screen.height), currTex);
     
     if (GUI.Button(Rect(0,0,100,50), "Use Texture 1")) {
         currTex = tex1;
     }
     
     if (GUI.Button(Rect(0,75,100,50), "Use Texture 2")) {
         currTex = tex2;
     }
     
     if (GUI.Button(Rect(0,150,100,50), "Use Texture 3")) {
         currTex = tex3;
     }
 }


Note if you really want to dynamically load the textures, you can place your images in the Assets/Resources directory and use the Resources.Load() function to load them.

Comment
Add comment · Show 1 · 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 nTu4Ka · Feb 06, 2014 at 10:38 PM 0
Share

I am happy to say that it was successfull.

BGTexture = (Texture)Resources.LoadAssetAtPath("Assets\\Resources\\FileName.PNG", typeof(Texture));

Worked perfect.

avatar image
0

Answer by nTu4Ka · Feb 03, 2014 at 10:22 AM

Note if you really want to dynamically load the textures

I think this is what I need. I've seen someone's opinion that using "Resources" directory is not a very good idea. Don't know why, though. But tried to avoid it.

Thanks for pointing in the right direction.

Comment
Add comment · 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

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

18 People are following this question.

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

Related Questions

GUIStyled button changes after being clicked 1 Answer

GUI Button + GUISkin = Bug, Help? 1 Answer

Chaning button Color (uGUI) 2 Answers

how to use the default skin in a custom stlye? 3 Answers

How do i pop up a canvas, when a click some key from keyboard? 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