• 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 Alengthyname · May 30, 2016 at 09:32 PM · guitextstringsguibox

Story Text to GUI Boxes

Hello,

I'm attempting to have text that is cycled through a series of boxes with player interaction advancing to the next box with its block of text. Right now t$$anonymous$$ngs are as below, and it works fine.

What I'd like to do is to write the text for the story, and have a function that converts that into the box text. Right now I'm t$$anonymous$$nking, where: box height = h, box width = w, character width = c, character height = H
at the character in the string at ((w/c)-1) appending "-"+"/n" if mid word or if a space just "/n" at that position, then after h/H lines cutting the text at the space nearest to the end of the line and starting from there in the next box.

I'm not entirely sure how to implement t$$anonymous$$s though, does anyone have any suggestions?

Thank you for your time.

[EDIT] I did some more reading through the manual and found the wordWrap and lineHeight functions, w$$anonymous$$ch are a step in the right direction. Still unsure as to how to cut the string after a set number of lines and start a new box though.

 public class Story : MonoBehaviour
 {

 public Texture boxTexture;
 const int BOX_HEIGHT = 64;
 const int BOX_WIDTH = 256;
 const int SPEAKER_BOX_WIDTH = 64;

 public int storyCounter = 0;

 void Update () 
 {
 
     if(Input.GetMouseButtonDown(0))
     {
         storyCounter++;
     }
 }

 void OnGUI()
 {
     if (storyCounter == 0)
     {
         GUI.Box(new Rect(0, Screen.height - BOX_HEIGHT, SPEAKER_BOX_WIDTH, BOX_HEIGHT, boxTexture);
         GUI.Box(new Rect(SPEAKER_BOX_WIDTH, Screen.height - BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT), "Welcome (click to continue) ");
     }
     if (storyCounter == 1)
     {
         GUI.Box(new Rect(0, Screen.height - BOX_HEIGHT, SPEAKER_BOX_WIDTH, BOX_HEIGHT), boxTexture);
         GUI.Box(new Rect(SPEAKER_BOX_WIDTH, Screen.height -BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT), "Text");
     }
   
 }

 }


[SECOND EDIT] I t$$anonymous$$nk I'm making some headway.

 public class Story : MonoBehaviour {

 public Texture boxTexture;
 const int BOX_HEIGHT = 64;
 const int BOX_WIDTH = 256;
 const int SPEAKER_BOX_WIDTH = 64;
   
 public int storyCounter = 0;
 public int maxLinesInBox;
 string story = "text";

 int storyLineLength;
   
 public int LinesInBox(int lineH)
 {       
     int linesInBox = BOX_HEIGHT / lineH;

     return linesInBox;
 }
 
 
 void Update () {
 
     if(Input.GetMouseButtonDown(0))
     {
         storyCounter++;
     }
 }

 void OnGUI()
 {
     
     if (storyCounter == 0)
     {
         GUI.Box(new Rect(0, Screen.height - BOX_HEIGHT, SPEAKER_BOX_WIDTH, BOX_HEIGHT), boxTexture);
         GUI.Box(new Rect(SPEAKER_BOX_WIDTH, Screen.height - BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT), "Welcome  (click to continue) ");
         Debug.Log(GUI.skin.box.lineHeight);
         Debug.Log(GUI.skin.box.wordWrap = true);
         Debug.Log(GUI.skin.box.clipping);
         maxLinesInBox = LinesInBox((int)GUI.skin.box.lineHeight);
         storyLineLength = story.Length / maxLinesInBox;
         Debug.Log("story line length:" + storyLineLength + ", story string length: " + story.Length + ", max box lines:" + maxLinesInBox);
     }
 }
 }

From here, I tried placing the below after the (storyCounter == 0) statement

         for(int i = 0; i <= storyCounter; i++) { 
         char[] arr = story.ToCharArray((storyCounter-1) * (storyLineLength-1), (storyCounter) * (storyLineLength-1));
         string temp = new string(arr);
         GUI.Box(new Rect(0, Screen.height - BOX_HEIGHT, SPEAKER_BOX_WIDTH, BOX_HEIGHT), boxTexture);
         GUI.Box(new Rect(SPEAKER_BOX_WIDTH, Screen.height - BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT), temp);

         if (storyCounter * storyLineLength >= story.Length)
         {
             break;
         } 

It's sort of working, but it's throwing two argument out of range errors for the index, the first loop throws a "<0" error, the last throws "must be greater than the length of the string". It's showing the first loop's block of text, but not the last.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Move the text in a GUI Button/Box 1 Answer

Big text field GUI. 1 Answer

Displaying varying text 1 Answer

How can I get the on-screen render bounds of a Unity 4.6 GUI component whose canvas is set to World Space? 1 Answer

Draw GUI Text if clicked out of webplayer 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