Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Rad-Coders · Jul 12, 2015 at 09:24 PM · c#guitextures

Replacing textures

Hi, so I am busy with my UI and I got stuck on this one problem.

I have 10 cars(just for testing) and there are 2 textures for each car. I have created a rectview to scroll thru the cars with the arrow keys. So I got it all working but, I need the current car that is selected to have a different texture(which is carSmallBox1 in the code). Can someone guide me to possible solution or an a idea that I can try. The link has the images if you want to test out the code(note, the textures with a 2 in front of the name like "2TestCube" is the textures for carSmallBox1[]), just set garageIsActive to true if you want to test it out. If you need me to explain anything, please ask.

Here is what it looks like. alt text

The code: using UnityEngine; using System.Collections;

 public class Garage : MonoBehaviour {
 
     /*Create Textures*/
     public Texture[] carSmallBox1 = new Texture[10];
     public Texture[] carSmallBox2 = new Texture[10];
     //private Texture carSmallBoxCurrent;
 
     /*Create Rect for gui elements*/
     private Rect[] carSmallBoxRect = new Rect[10];
 
     private bool garageIsActive;
     private int amountCars = 10;
 
     /*Create variables to determine the locations of the GUI elements*/
     [SerializeField] private float bottomBarCarsx; // This value will dtermine the legnth of the amount of cars in the x axis(returned as pixels)
     [SerializeField] private float bottomBarCarsy; // This value will dtermine the legnth of the amount of cars in the y axis(returned as pixels)
     [SerializeField] private float bottomBarCurrentx; //This value will determine which car is currently selected and to help with scrolling
     private float maxBottomBarCurrentx;
 
     private int currentCar = 0;
 
     /*Create the needed variables to determine the size of the scroll view rect*/
     private Vector2 scrollPos;
     private Rect scrollRectPos;
     private Rect scrollViewRect;
 
     /*Create the need variables for screen locations and screen resolutions*/
     private const float iconWidth = 70;
     private const float iconHeight = 100;
     private float screenWidth;
     private float screenHeight;
     private float nativeWidth = 1024;
     private float nativeHeight = 597;
     private float nativeWidthCentre = 512;
     private float nativeHeightCentre = 298.5f;
     private float rx;
     private float ry;
 
 
 
     void Start () 
     {
         screenWidth = Screen.width;
         screenHeight = Screen.height;
 
         bottomBarCarsx = nativeWidth/2;
         bottomBarCarsy = nativeHeight - 150;
         bottomBarCurrentx = 0;
 
         scrollRectPos = new Rect(0, nativeHeight - 150, nativeWidth, 150);
         scrollViewRect = new Rect(0, nativeHeight -150, nativeWidth +300,100);
 
         /*Determine the rx and ry(i dont know what they stand for) for the matrix*/
         rx = screenWidth/nativeWidth;
         ry = screenHeight/nativeHeight;
 
         for(int i = 0; i < amountCars; i++)
         {
             carSmallBoxRect[i] = new Rect(bottomBarCarsx, bottomBarCarsy, iconWidth,iconHeight);
             bottomBarCarsx += 100;
         }
 
         maxBottomBarCurrentx = (amountCars*200) - 200;
 
         scrollViewRect = new Rect(0, nativeHeight -150, bottomBarCarsx + 410,100);
     }
     
 
     void Update () 
     {
         garageIsActive = HomeScreen.test1Bool;
     }
 
     void OnGUI()
     {
         /*Create a new gui matrix to automatically scale all gui elemnts for any screen resolution*/
         GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, new Vector3 (rx, ry, 1));
 
         if(garageIsActive)
         {
         scrollPos = GUI.BeginScrollView( scrollRectPos, scrollPos, scrollViewRect);
 
             for(int i = 0; i < amountCars; i++)
             {
                 GUI.DrawTexture(carSmallBoxRect[i], carSmallBox2[i], ScaleMode.StretchToFill, true, 0);
             }
 
             GUI.ScrollTo(new Rect(0, nativeHeight - 150,1025 + bottomBarCurrentx, 150));
 
             if(Input.GetKeyDown(KeyCode.RightArrow))
             {
                     bottomBarCurrentx += 100;
                 if(bottomBarCurrentx > maxBottomBarCurrentx)
                     bottomBarCurrentx = maxBottomBarCurrentx;
                 currentCar ++;
             }
             if(Input.GetKeyDown(KeyCode.LeftArrow))
             {
                 bottomBarCurrentx -= 100;
                 if(bottomBarCurrentx < 0)
                     bottomBarCurrentx = 0;
                 currentCar++;
             }
         GUI.EndScrollView ();
 
         }
     }
 }
 

The link for the pictures: https://www.dropbox.com/s/1zypu1g1ofimiig/Assembly%20Bay%20Pics.zip?dl=0

sample.png (77.4 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

holding GUITextures in a array? and changing the size from script? 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Erroe when resizing GUITextures in C# file 1 Answer

What is the mathematical formula for GUI dragging ? 2 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