Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Gui textures and mouse scroll

Discussion in 'Scripting' started by Zou, Oct 23, 2007.

  1. Zou

    Zou

    Joined:
    Oct 19, 2007
    Posts:
    22
    Hi,

    What I would like to do is scroll through a set of GUI textures with the mouse wheel. To be more specific, let’s say I have:

    Texture1 (T1)
    Texture2 (T2)
    Texture3 (T3)
    Texture4 (T4)
    Texture5 (T5)

    As I scroll through them they would appear on GUI. Also the purpose of this isn’t an indication of switching weapons from the player, but more like viewing the environment differently. So these textures will cover most of the view/HUD of the player.

    In a game situation
    When the game starts, T3 will be current texture on GUI, so when the player scrolls up, T2 will appear and scrolling up again will bring about T1. Once the player reaches T1, I don’t want T5 to appear if the player keeps scrolling up. And I want the same thing to happen if the player is scrolling down; textures will stop at T5 and not scroll to T1. In essence the player has to scroll up and down to go through T1 to T5.

    I really have no idea on how to even start writing the scripts for this, so any help would be appreciated. I have set of test textures to use and I will be using the FPS controller. Also, I have set an element from the input manager to allow mouse wheel control.

    If this explanation isn’t clear enough or if knowing what exactly I will be doing with this will you guys have better understanding then I will explain more.

    Thanks

    I don’t know if this is correct or not but I decided to put the textures into an array using

    var textures : Textures[];

    and assign the array elements through the Inspector view. Now I am trying to get T3 to show up on GUI when playtesting.

    Again I don’t know if this is the correct approach or not.
     
  2. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    To change the GUI Texture on any GUI component is very easy.

    There are two ways to approach it:
    First - create 5 GUIStyles each with its own Normal Background texture and just change the style based on a variable.

    Second - create one custom style (since you probably dont want to redefine defaults at runtime) and 5 textures - then simply swap the Normal.Background texture based on a variable.

    Whatever the case, GUIStyles are your friend :)

    This info - check the part on background images - should help:
    http://unity3d.com/support/documentation/ScriptReference/GUIStyle.html

    Cheers
    Shaun
     
  3. Zou

    Zou

    Joined:
    Oct 19, 2007
    Posts:
    22
    I'm sorry but I don't understand your approach. I'm not saying you're wrong, I guess I need a longer explanation and possibly some scripts(of course I'm not expecting the entire script but maybe a smaller script or sample script from another source that I could work off from). I took a look at the link but I just got more confused with the vague explanations.
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    No worries - the GUI system takes a while to get used to.

    I wrote the following off the top of my head - so it may not compile, but the concept is there.
    The basic idea is to setup a bunch of textures that you assign manually, then create a new GUIStyle at runtime. You then create a control and override its default GUIStyle with yours and then just change the normal.background Texture 2D.
    I hope C# is OK - I've sworn off JS.
    good luck. shaun.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class testScript : MonoBehaviour
    5. {
    6.     public Texture2D tex01 = null; //assign these textures in the inspector
    7.     public Texture2D tex02 = null;
    8.     public Texture2D tex03 = null;
    9.     public Texture2D tex04 = null;
    10.     public Texture2D tex05 = null;
    11.     public Texture2D tex06 = null;
    12.     private GUIStyle gsTextureSwapper;
    13.  
    14.     void Start()
    15.     {
    16.          gsTextureSwapper = new GUIStyle();
    17.          gsTextureSwapper.normal.background = tex01; //set the default texture
    18.     }
    19.     void OnGui()
    20.     {
    21.         //Draw the button, but set its style to our custom one          
    22.          if(GUI.Button(new Rect(), "hello world", gsTextureSwapper))
    23.         {
    24.              gsTextureSwapper.normal.background = tex02; //If we click, change to Texture 02;
    25.         }
    26.     }
    27. }
    28.  
     
  5. Zou

    Zou

    Joined:
    Oct 19, 2007
    Posts:
    22
    I am using javascript, so I am trying to convert the above code into Javascript. This is what I have so far:

    Code (csharp):
    1. //I only have three textures but will most likely have more
    2. var Calm :Texture; // I want this as my default texture when the game starts.
    3. var Anger : Texture;
    4. var SupCA : Texture;
    5. private var GUIStyle;
    6.  
    7. function Start ()
    8. {
    9. }
    10.  
    11. function OnGui()
    12. {
    13. }
    I'm not sure how to rewrite the C# code within the 'void start' and 'void OnGui' to Javascript. Also I can't find a script for mouse wheel/scroll input and how I could apply it to my game. Any help would be appreciated.

    Thanks
     
  6. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Untested:

    Code (csharp):
    1. var tex01 : Texture2D;
    2. var tex02 : Texture2D;
    3. var tex03 : Texture2D;
    4. var tex04 : Texture2D;
    5. var tex05 : Texture2D;
    6. var tex06 : Texture2D;
    7. private var gsTextureSwapper : GUIStyle;
    8.  
    9. function Start() {
    10.     gsTextureSwapper = new GUIStyle();
    11.     gsTextureSwapper.normal.background = tex01;
    12.     }
    13.    
    14. function OnGui() {
    15.     if(GUI.Button(new Rect(), "hello world", gsTextureSwapper)) {
    16.         gsTextureSwapper.normal.background = tex02;
    17.         }
    18. }
     
  7. Zou

    Zou

    Joined:
    Oct 19, 2007
    Posts:
    22
    Code (csharp):
    1.  
    2. var Calm : Texture;
    3. var Anger: Texture;
    4. var SupCA : Texture;
    5. private var gsTextureSwapper : GUIStyle;
    6.  
    7. function Start ()
    8. {
    9.     gsTextureSwapper = new GUIStyle();
    10.     gsTextureSwapper.normal.background = Calm ;
    11. }
    12.  
    13. function OnGui()
    14. {   if(GUI.Button(new Rect(), "hello world" ,gsTextureSwapper))
    15.     {
    16.         gsTextureSwapper.normal.background = Anger;
    17.     }
    18. }
    I use the above script but it gives me an error for:
    Code (csharp):
    1. private var gsTextureSwapper : GUIStyle;
    that said "The name 'GUIStyle' does not denote a valid type." I am not sure how to fix this.

    Another question, is this correct for using the mouse scroll wheel:
    Code (csharp):
    1.  
    2. function OnGui()
    3. {
    4.     var delta = Input.GetAxis ("EyeO");
    5.     if (Input.GetButton("EyeO"))
    6.     {
    7.           //Script here would be my next question below
    8.     }
    9. }
    Also, how can I write script so that it will detect which direction the the mouse wheel will be scrolling. Eventually I want the player to have to scroll up if they are on the last texture and scroll if they're on the top.
     
  8. Zou

    Zou

    Joined:
    Oct 19, 2007
    Posts:
    22
    I am also using Unity 1.6.2 not Unity 2.0 and probably will not be upgrading. I think the script isn't working because of this. I keep getting errors about 'GUIStyle' and 'GUI' being unknown identifiers. I also tried to do the GUI Basics from the user manual on the Unity website but I get the same error of the 'GUI' being a unknown identifier.

    Hopefully someone can clear this up for me.
     
  9. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    The new GUI commands are part of 2.0, so they won't work until you've gotten the upgrade.
     
  10. Zou

    Zou

    Joined:
    Oct 19, 2007
    Posts:
    22
    Now that this clear, any suggestions on my original post would be appreciated. I have Unity 1.6.2

    Thanks
     
  11. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    If at all possible, you really should consider upgrading.
    Unity 2.0 is miles better and it's comparatively cheap for the Indie version vs. features.
     
  12. Zou

    Zou

    Joined:
    Oct 19, 2007
    Posts:
    22
    I working on my school's computer so I don't think they will be upgrading