Search Unity

Switch marquee direction?

Discussion in 'Scripting' started by jay73, Aug 23, 2014.

  1. jay73

    jay73

    Joined:
    Jul 31, 2014
    Posts:
    22
    I'm using this code on my project, but i just can't seem to understand how to change the code that it would scroll text from right-to-left instead of left-to-right. How do i do that? Can someone help me, please?

    Also, if someone could also tell me how can i change the size of the font and the color on text attached to this code would be really nice too. Yes, i'm new to coding :)
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Just flip the rects x pos. Like this:

    (I've added a bool so you can have both left or right)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Marquee : MonoBehaviour {
    5.     public string message     = "Where we're going, we don't need roads.";
    6.     public float scrollSpeed = 50;
    7.  
    8.     Rect messageRect;
    9.  
    10.     public bool left; //if true it'll scroll left, else right
    11.  
    12.     void OnGUI () {
    13.         if (messageRect.width == 0) {
    14.             Vector2 dimensions = GUI.skin.label.CalcSize(new GUIContent(message));
    15.          
    16.             // Start the message past the left side of the screen
    17.             messageRect.x      = -dimensions.x;
    18.             messageRect.width  =  dimensions.x;
    19.             messageRect.height =  dimensions.y;
    20.         }
    21.      
    22.         messageRect.x += Time.deltaTime * scrollSpeed;
    23.  
    24.         if (messageRect.x > Screen.width)
    25.             messageRect.x = -messageRect.width;
    26.  
    27.         if(!left)
    28.             GUI.Label(messageRect, message);
    29.         else
    30.             GUI.Label(new Rect(-messageRect.x, messageRect.y, messageRect.width, messageRect.height), message);
    31.     }
    32. }
    33.  
     
  3. jay73

    jay73

    Joined:
    Jul 31, 2014
    Posts:
    22
    That was fast! :) Thanks a lot! It start rolling at the middle of the screen and not in the right, but i think i'll figure that out. Important thing is that it now goes right-to-left, like i wanted :) Thanks again for your very quick reply, sir!
     
  4. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    No worries :)
     
  5. jay73

    jay73

    Joined:
    Jul 31, 2014
    Posts:
    22
    I figured out the font size and color issue. I added GUIstyle to the code and i'm set :)

    Code (CSharp):
    1. public GUIstyle myStyle;
    2.  
    3.  
    4.  
    5.  
    6. if(!left)
    7.  
    8. GUI.Label(messageRect, message, myStyle);
    9.  
    10. else
    11.  
    12. GUI.Label(new Rect(-messageRect.x, messageRect.y, messageRect.width, messageRect.height), message, myStyle);
    Now i have figure out how to move the text at the bottom on the screen and starting of the text scrolling to the right edge of the screen...
     
  6. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    just set the message rect y to something lower.
     
  7. jay73

    jay73

    Joined:
    Jul 31, 2014
    Posts:
    22
    Got it. Now text is at the bottom of the screen, but X is giving me a hard time. If i add x+something, it does what i want and text starts at the right edge of the screen, but when it gets at the left side of the screen, it cuts back to the right side before all of the text has scrolled off to the left. I don't know if you understand what i'm trying to say but anyway...
     
  8. jay73

    jay73

    Joined:
    Jul 31, 2014
    Posts:
    22
    Dammit! It stopped working when I'm trying to put a whole lot bigger/longer text on string... What's up with that?
     
  9. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Not sure. I'm gonna need more information than that :) what's your updated script? and how big is your string?
     
  10. jay73

    jay73

    Joined:
    Jul 31, 2014
    Posts:
    22
    My bad. It's still working, but I had -messageRect.x + 460 at line 51 (for the line to start coming from the right side of the screen) but when I changed the length of the text, it went so far to the right that it took a lot of time to appear on the screen and all the code progress (text scrolling) went all to hell with longer string.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MainMenu : MonoBehaviour {
    5.  
    6.     public string message = "WELCOME TO THE UNITY VERSION OF THE TEKHAN ARCADE GAME BOMBJACK    CODED BY XXXX XXXXXXXX    THANKS TO RORY GREEN FOR THE GRAPHICS    AND MARKD COOKSEY FOR THE MUSIC     ALSO TO GAVIN HARVEY AND CLARE POMEROY FOR ALL THEY HELP";
    7.     public float scrollSpeed = 45;
    8.     public bool left;
    9.     public GUIStyle myStyle;
    10.    
    11.     Rect messageRect;
    12.  
    13.     //GUIText StartGame;
    14.     // Use this for initialization
    15.     void Start () {
    16.  
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if (Input.GetKey ("space")) {
    23.             Application.LoadLevel("Level_1");
    24.         }
    25.  
    26.         if (Input.GetKey("escape"))
    27.             Application.Quit();
    28.     }
    29.  
    30.     void OnGUI ()
    31.     {
    32.         // Set up message's rect if we haven't already.
    33.         if (messageRect.width == 0) {
    34.             var dimensions = GUI.skin.label.CalcSize(new GUIContent(message));
    35.            
    36.             // Start message past the left side of the screen.
    37.             messageRect.x = -dimensions.x;
    38.             messageRect.width = dimensions.x;
    39.             messageRect.height = dimensions.y;
    40.         }
    41.        
    42.         messageRect.x += Time.deltaTime * scrollSpeed;
    43.        
    44.         // If message has moved past the right side, move it back to the left.
    45.         if (messageRect.x > Screen.width)
    46.             messageRect.x = -messageRect.width;
    47.        
    48.         if(!left)
    49.             GUI.Label(messageRect, message, myStyle);
    50.         else
    51.             GUI.Label(new Rect(-messageRect.x, messageRect.y+540, messageRect.width, messageRect.height), message, myStyle);
    52.     }
    53.  
    54. }
    55.  
     
  11. jay73

    jay73

    Joined:
    Jul 31, 2014
    Posts:
    22
    I've got it! Still don't quite understand the script, but it's now doing for me what I want. Scrolls my text starting from right side of the screen to the left side and when all the text gets to the left (disappears to the left) it starts over from the right side. Does the job now. I'm happy. Lines 45 and 51 were the key to get it working for me.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MainMenu : MonoBehaviour {
    5.  
    6.     public string message = "WELCOME TO THE UNITY VERSION OF THE TEKHAN ARCADE GAME BOMBJACK    CODED BY XXXXXXXXXXXXX    THANKS TO RORY GREEN FOR THE GRAPHICS    AND MARK COOKSEY FOR THE MUSIC     ALSO TO GAVIN HARVEY AND CLARE POMEROY FOR ALL THEY HELP";
    7.     public float scrollSpeed = 45;
    8.     public bool left;
    9.     public GUIStyle myStyle;
    10.  
    11.     Rect messageRect;
    12.  
    13.     //GUIText StartGame;
    14.     // Use this for initialization
    15.     void Start () {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if (Input.GetKey ("space")) {
    23.             Application.LoadLevel("Level_1");
    24.         }
    25.  
    26.         if (Input.GetKey("escape"))
    27.             Application.Quit();
    28.     }
    29.  
    30.     void OnGUI ()
    31.     {
    32.         // Set up message's rect if we haven't already.
    33.         if (messageRect.width == 0) {
    34.             var dimensions = GUI.skin.label.CalcSize(new GUIContent(message));
    35.          
    36.             // Start message past the left side of the screen.
    37.             messageRect.x = -dimensions.x;
    38.             messageRect.width = dimensions.x;
    39.             messageRect.height = dimensions.y;
    40.         }
    41.      
    42.         messageRect.x += Time.deltaTime * scrollSpeed;
    43.      
    44.         // If message has moved past the right side, move it back to the left.
    45.         if ((messageRect.x-1000) > Screen.width)
    46.             messageRect.x = -messageRect.width;
    47.      
    48.         if(!left)
    49.             GUI.Label(messageRect, message, myStyle);
    50.         else
    51.             GUI.Label(new Rect(-messageRect.x-640, messageRect.y+680, messageRect.width, messageRect.height), message, myStyle);
    52.     }
    53.  
    54. }
    55.