• 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 4shm3x · May 08, 2013 at 10:36 PM · guitexture

What's the best way to create a gui slider that will change the image on screen?

Basically I am looking for a way to have a 2Dimage displayed on screen, and then use a gui slider that is below it to change that texture?

I.e the slider will have 5 different positions on it that it can be set to that will change it to a different texture.

What would be the best method, to use gui.drawtexture for the texture itself or to have the texture as a plane and make the slider change to a different plane?

What would be the best method for this or if I could be pointed in the right direction.

Thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aaronov · May 08, 2013 at 11:17 PM

You could do something like this:

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour 
     {
     private int textureIndex = 0;
     public Texture[] textures;
 
     void OnGUI() 
         {
         textureIndex =
                (int)GUI.HorizontalSlider(
                     new Rect(25, 70, 100, 30),
                     textureIndex,
                     0,
                     textures.Length-1);
 
         GUI.DrawTexture(
                new Rect(10, 10, 60, 60),
                textures[textureIndex],
                ScaleMode.ScaleToFit,
                true,
                10.0F);
         }
     }
Comment
Add comment · Show 7 · 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 Bunny83 · May 08, 2013 at 11:23 PM 0
Share

I'm almost happy with this answer except the magic number "5" in line 16 should be "textures.Length-1". Also you should simply make the textures array public so the user of the script can simply drag as many textures as he wants to the array in the inspector. In this case you don't have to create the array because that's done by the inspector / Unity.

avatar image Bunny83 · May 08, 2013 at 11:25 PM 0
Share

oh, and i guess you miss a cast to "int" in line 16

avatar image aaronov · May 08, 2013 at 11:27 PM 0
Share

Your suggestions are valid and I'll add them to the answer. I probably should have mentioned I hadn't tested this code :)

avatar image 4shm3x · May 08, 2013 at 11:42 PM 0
Share

Works great! I get a array index out of range error as I am able to move to a 6th position even though I only selected to have 5 textures. I removed the scalemode.scaletofit as it ws messing around with my textures

avatar image aaronov · May 08, 2013 at 11:54 PM 0
Share

The index out of range problem is fixed in the answer now. The array length required a 1 subtracted (textures.Length-1).

As for the squashed texture try set the 10.0F value to 0 ins$$anonymous$$d in the DrawTexture function so that the default aspect ratio of the texture is used.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

15 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

Related Questions

Multiple Cars not working 1 Answer

How To Make GUI Buttons Load/Quit 1 Answer

Display GUI texture on specific variable value? 0 Answers

Why doesn't my script work? 1 Answer

GUI Texture Positioning Help 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