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

multiple lines of story in one GUI Box

Discussion in 'Scripting' started by Feyhu, Jan 22, 2011.

  1. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    How can I do it? whatever I try, I get one line that doesnt fit inside of the box
     
  2. AVividLight

    AVividLight

    Joined:
    Jan 8, 2010
    Posts:
    117
    Couldn't you just make the box bigger or the text smaller?
     
  3. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    If you use a GUIStyle for the box, you can set the text to Word Wrap which should restrict it to multiple lines inside the box. You'll lose the default Unity default style as a result, but you can always add in a background texture of your own.
     
  4. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    ok, thanks!
     
  5. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    is there any way I can do that with the standard GUI?
     
  6. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    can you just put \n between the lines?
     
    Rickenbaker likes this.
  7. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    doesnt work
     
  8. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    You could do what I did, and have the GUIBox point to an external .txt file.

    Code (csharp):
    1. var dialogue : String;
    2. // The dialogue of the NPC (i.e. what the NPC says)
    3. // NOTE: the above variable should be left blank -- the dialogue will be assigned using the questText variables.
    4.  
    5.  
    6. var questText1 : TextAsset;  // Points to the text file that contains the NPC dialogue
    7. dialogue = questText1.text;  // Assigns the text file to the dialogue variable
    Use the above code, and just replace the text you want to appear in the GUI Box code with the dialogue variable.
     
    Last edited: Jan 23, 2011
  9. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    I am trying to keep it all in unity
     
  10. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    Why? Game development is about getting things to work by whatever means necessary. You shouldn't shun a working solution just because it functions in a particular way.
     
  11. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    one: I am lazy
    two: I am going to upload it to a games site so I dont think an external file will work.
     
  12. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    It'll work just fine if the text file is in your projects folder. Once you build your project, everything that your game uses will get compressed into the executable anyway.
     
  13. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    also, that still doesnt fix what the problem was. I want to know how to make the text wrap with the default GUI style.
     
  14. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    So you want to achieve something like this, correct?

     
  15. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    yes, please
     
  16. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    Alright, well that's what my method accomplishes. Here, I'll even give you the full code for it. All you have to do is drag it onto the mesh of any object (make sure it has a renderer attached), and then create a .txt file and drag it onto the QuestText1 variable in the Unity Inspector.

    But if you really, really don't want to use a plain text file in your project, you can just copy/paste some pre-formatted text into the Dialogue variable instead (keep in mind you'll have to either remove or comment out the QuestText1 variable, as you'll get an error if you don't assign anything to it, and it overrides anything you type into the Dialogue variable).

    Anyway, here's the code (written in JavaScript):
    Code (csharp):
    1. var highlighted = false;        // Used to determine whether or not NPC is highlighted
    2. var talk = false;               // Used to determine whether or not NPC is talking
    3. var title : String;             // Title of the textbox for the NPC dialogue
    4.  
    5. var dialogue : String;      // The dialogue of the NPC (i.e. what the NPC says) -NOTE: this variable should be left blank -- the dialogue will be assigned using the questText variables.
    6. var questText1 : TextAsset;     // Points to the text file that contains the NPC dialogue (regular dialogue)
    7. dialogue = questText1.text;     // Assigns the text file to the dialogue variable
    8.  
    9. var textLength : int = 335;
    10. // Determines the length of the dialogue text area
    11. // Adjust textLength value if text is cut off or if there is too much empty space (best to do this in the Unity Inspector)
    12. // Set default textLength to 335 if all text is visible without scrolling
    13.  
    14. var scrollViewVector : Vector2 = Vector2.zero;      // Tracks and stores the position of the scrollbar
    15.  
    16.  
    17.  
    18.  
    19. // Changes the color of the material while the mouse is over the mesh
    20. function OnMouseOver ()
    21.     {
    22.         renderer.material.color = Color(1.5, 1.5, 1.5);
    23.         highlighted = true;
    24.     }
    25.  
    26. function OnMouseExit ()
    27.     {
    28.         renderer.material.color  = Color(0.5, 0.5, 0.5);
    29.         highlighted = false;
    30.     }
    31.  
    32. // Detect left mouse click and print it in the debug log.
    33. function OnGUI()
    34. {
    35.     var click : Event = Event.current;
    36.  
    37.     if(click.button == 0  click.isMouse)
    38.     {
    39.         if (highlighted == true)
    40.         {
    41.             //Debug.Log("Left Click");
    42.             talk = true;
    43.         }
    44.     }
    45.  
    46.     if (talk == true)
    47.     {
    48.         //(horizontal position, vertical position, horizontal size, vertical size)
    49.         GUI.Box(Rect(50,50,325,400),title);
    50.  
    51.  
    52.         // Begin the ScrollView
    53.         //(h-position, v-position, h-size, v-size)
    54.         scrollViewVector = GUI.BeginScrollView (Rect (60, 75, 310, 345), scrollViewVector, Rect (0, 0, 90, textLength+10));
    55.  
    56.             // Put something inside the ScrollView
    57.             innerText = GUI.TextArea (Rect (5, 5, 287, textLength), dialogue);
    58.  
    59.         // End the ScrollView
    60.         GUI.EndScrollView();
    61.  
    62.         // Close button
    63.         if (GUI.Button(Rect(350,53,22,15),"x"))
    64.         {
    65.             //Debug.Log("Closed the dialogue box.");
    66.             talk = false;
    67.         }
    68.  
    69.         //(h-position, v-position, h-size, v-size)
    70.         if (GUI.Button(Rect(307,425,64,20),"Goodbye"))
    71.         {
    72.             talk = false;
    73.         }
    74.     }
    75. }
     
    Last edited: Jan 23, 2011
  17. Feyhu

    Feyhu

    Joined:
    Sep 14, 2010
    Posts:
    211
    thanx!