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 sonic220 · Mar 19, 2011 at 10:19 PM · imageguitexture

image in the button

hi, I'm following the book unity beginners guide by examples, and I'm having problems showing an image in a button, this is the code:

if(GUILayout.Button(Resources.Load(card.img), GUILayout.Width(cardDIM))) {
        Debug.Log(card.img);
        }

there's no error in the game, the card.img is a string that tell me the name "empty", the same name in the project panel of an image 100 x 100.

I don't know why this don't work, i've followed the book carefully

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 Sintua · Oct 14, 2011 at 11:20 PM 0
Share

I'm having the same problem. To clarify:

Card c = aGrid[i][j]; if (GUILayout.Button(Resources.Load(c.cardImage), GUILayout.Width(cardWidth))) { Debug.Log(c.cardImage); }

cardImage is a string that is the resources name.

Even typing in "robot" (The name of the picture in Resources folder) directly does not work, since Resources.Load seems to be returning an Object ins$$anonymous$$d of a Texture.

5 Replies

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

Answer by clunk47 · Dec 04, 2013 at 05:18 AM

I'm going to give a fresh example. Create a folder called Resources in your Assets folder (project pane). For this example, just use a .png file and name it "Image", place it directly into your Resources folder. Now we'll create GUIContent and use it for the GUI Button. I've attached an example project .zip in case you have some issues with the following script:

Download Example Project

 #pragma strict
 
 var image : Texture2D;
 var content : GUIContent;
 
 function Start()
 {
     image = Resources.Load("Image");
     content.image = image;
 }
 
 function OnGUI()
 {
     GUI.Button(Rect(0, 0, 128, 128), content);
 }


This works to some extent. If you simply want to use your texture as the button itself and not use built in background:

 #pragma strict
 
 var image : Texture2D;
 
 function Start()
 {
     image = Resources.Load("Image") as Texture2D;
 }
 
 function OnGUI()
 {
     GUI.skin.button.normal.background = image;
     GUI.skin.button.hover.background = image;
     GUI.skin.button.active.background = image;
     GUI.Button(Rect(0, 0, 128, 128), "");
 }

You could of course use a GUIStyle to do this in the inspector, you could also use different images for each button state.


guibutton image example.zip (152.2 kB)
Comment
Add comment · Show 4 · 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 boc · May 10, 2014 at 11:44 AM 0
Share

Thanks!! This was exactly that I was looking for: hiding the fancy border from the GUI Texture Button to show only the texture.

avatar image clunk47 · May 11, 2014 at 07:01 PM 0
Share

^Glad I was able to help out :D

avatar image VinciValar · Jul 29, 2015 at 12:35 PM 0
Share

Can someone help change these codes into C# script?

avatar image clunk47 · Jul 29, 2015 at 12:46 PM 1
Share

http://www.m2h.nl/files/js_to_c.php

avatar image
0

Answer by efge · Mar 19, 2011 at 10:54 PM

This function needs the name of the asset (or the asset path) as a string:

Resources.Load("card.img")


Edit:

var img : Texture;

if(GUILayout.Button(img, ...))

Comment
Add comment · Show 3 · 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 efge · Mar 19, 2011 at 10:55 PM 0
Share

If this does not work try it without the file extension.

avatar image sonic220 · Mar 19, 2011 at 11:27 PM 0
Share

card is an object that I've created and img is the variable string,

card.img is set as "image", the name of the image.

I've tryed with Resources.Load("image"), but this doesn't work either

avatar image efge · Mar 20, 2011 at 11:51 AM 0
Share

edited my answer (if I get it right)

avatar image
0

Answer by Uzquiano · Mar 19, 2011 at 11:19 PM

Hi,

You should just write in "" the name of the file you placed in the Resources folder inside your project, take away the extension. See the example:

if(GUILayout.Button(Resources.Load("card"));
Comment
Add comment · Show 3 · 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 sonic220 · Mar 19, 2011 at 11:27 PM 0
Share

card is an object that I've created and img is the variable string, card.img is set as "image", the name of the image. I've tryed with Resources.Load("image"), but this doesn't work either

avatar image Uzquiano · Mar 19, 2011 at 11:32 PM 0
Share

Sorry, I didn't get your point. So, are you trying to show a string as an image?

Resources.Load() is used for loading asset which are placed in the Resources folder... I am totally lost, I don't get what you are trying, sorry

avatar image Uzquiano · Mar 19, 2011 at 11:35 PM 0
Share

Hei, have you seen this in the reference? http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html

The Description says: Loads an asset stored at path in a Resources folder.

avatar image
0

Answer by $$anonymous$$ · Nov 16, 2012 at 11:54 PM

You need to explicitly state that the resource you are loading is a texture. Looks like Resource.Load(); returns a texture, but GUILayout.Button(); is not sure whether it is a texture, a string or something else.

if ( GUILayout.Button( (Texture)Resources.Load("robot"), GUILayout.Width(cardW) ) )

This worked for me. For C#. Not sure if casting is done the same way in JavaScript.

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 FrazZahid · Jun 19, 2014 at 12:18 PM 0
Share

This works for me.. i am new so cant vote it up because dont have 15+ reputation :(

avatar image
0

Answer by Parithi · Dec 04, 2013 at 05:02 AM

Just create a folder named "Resources" in your Assets folder -> Assets/Resources and place your robot.jpg inside the Resources folder. And Bam! Your code works...

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 akhawatrah · Mar 03, 2014 at 08:41 PM 0
Share

A very late comment: This actually works. Thanks.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

problem showing image in GUITexture 1 Answer

GUI dependent on an int variable 1 Answer

Pop up image triggered by box collider? 1 Answer

GuiTexture Blurred after Build for website 0 Answers

Reduce Draw call for Multiple GUI Textures with same Texture 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