• 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 coco · Feb 24, 2011 at 12:28 PM · javascriptstringwordsentences

how to display a long string sentence by sentence instead of character by character?

Hi, I am new to Unity. I get the following code from this http://answers.unity3d.com/questions/11151/multi-line-label-with-different-font, instead of type-writing it character by character, I would like to make it display sentence by sentence, is there any one know how to do it? Thanks for helping me solve the problem.

var gFont : Font; var letterPause = 0.01; var Story : int = 1; var words : String = ""; var fontStyle : GUIStyle; private var word;

function Start () { if(Story == 1) { word = "Today is a beautiful sunny day.\n I had fun with my family.\n etc....."; } TypeText(); }

function TypeText() { for (var letter in word.ToCharArray()) { words += letter; yield WaitForSeconds (letterPause); }
}

function OnGUI() { GUI.skin.font = gFont;

GUI.Label(Rect(250,80,1000,500),words,fontStyle);

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Alec-Slayden · Feb 24, 2011 at 04:29 PM

I would recommend using the method string.Split() with a string array.

For example in Javascript, if you had

var Sentences : String[];
var myString = "This is the first sentence. This is the second sentence.";

You could use the split method like so:

Sentences = myString.Split("."[0]);

This will split the sentences at each period (which means you'll need to add the periods later). What you get from the above is that Sentences[0] is "This is the first sentence", and Sentences[1] comes out to be "This is the second sentence"

Then instead of using an invoke to grab the next letter, you can just use the invoke to grab the next Sentence[n];

EDIT:

If you'll be using other punctuation for sentences, you would probably do this:

Sentences = myString.Split("."[0], "!"[0], "?"[0], ":"[0]);

the split method needs characters, not strings, which is why we have to add [0], which grabs the first (and only) character in the strings we're using.

EDIT 2 : Per your request, here's an example of how to change the script to show delayed sentences:

var gFont : Font; var letterPause = 0.01; var Story : int = 1; var words : String = ""; var fontStyle : GUIStyle;

private var sentences : String[]; private var word;

function Start (){

 if(Story == 1) { 
     word = "Today is a beautiful sunny day.\n-I had fun with my family.\n-etc.....";    
 } 

 sentences = word.Split("-"[0]); 
 TypeText(); 

}

function TypeText(){ for (var line in sentences){ words += line; yield WaitForSeconds (letterPause); } }

function OnGUI() { GUI.skin.font = gFont; GUI.Label(Rect(250,80,1000,500),words, fontStyle); }

This will work well if you don't plan to use any dashes "-". It's set up to split sentences at dashes you put between them, that way you can still include punctuation. At the start of this script we're splitting up the sentences and storing it in a string array called "sentences". Then, instead of grabbing the characters of the whole string, we're cycling through each sentence that is in our "sentences" array.

Comment
Add comment · Show 4 · Share
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
avatar image coco · Feb 26, 2011 at 04:47 PM 0
Share

Do you mind to give me an example of the script? and how to add the delay in each sentences, do I still need to use this function?

function TypeText() { for (var letter in word.ToCharArray()) { words += letter; yield WaitForSeconds (letterPause); }

based on above "var letter in word.ToCharArray()", how I should change it so that it can function properly in string ins$$anonymous$$d of char?

I want to display something like this:

This is the first sentence. This is the second sentence.

avatar image Alec-Slayden · Feb 26, 2011 at 05:10 PM 0
Share

I'll try to give a hand with this soon if no one else does before tuesday. When I say the method needs characters, I only mean for determining where to split the strings. It will still return strings fine, I was just explaining the purpose of [0] next to the delimiters.

avatar image coco · Mar 02, 2011 at 01:33 PM 0
Share

hm... okay, sorry to trouble you, as I am still new to unity... I would still need your guide on this problem...thanks in advance...

avatar image coco · Mar 03, 2011 at 12:51 PM 0
Share

hi... thank you so much... it does work well...

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Access a GameObject through it's tag, using a string. 1 Answer

JavaScript String Question 2 Answers

Setting Scroll View Width GUILayout 1 Answer

3d text will not display float 1 Answer

Creating multidimensional string array 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges