Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 KenanTheFab · Oct 28, 2018 at 09:40 AM · uistringspeeddisplaydialogue

Ignoring/not displaying text/numbers in string if surrounded with (), [] or <>?

Hello, I've got a dialogue system and I'd like to be able to set the speed at which the letters appear but change this per line of dialogue, my idea was to add numbers in the dialogue itself (IE, "Hello there young adventurer! [0.05]) and then having unity read the numbers within the brackets but not display the brackets nor the numbers on screen?

Is this possible? I've tried experimenting and googling but I've been unable to find anything that helps...

Comment
Add comment · Show 2
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 ranch000 · Oct 29, 2018 at 04:22 AM 0
Share

You could simply read from a JSON file.

{ "dialog1": { "text": "Hello there young adventurer", "speed": 0.05 } }

You can easily read this with any JSON Parser like $$anonymous$$iniJSON

avatar image Casiell · Oct 29, 2018 at 09:48 AM 0
Share

As @ranch000 mentioned, JSON is the best option for you. But if you really don't want to use it, you should look into regex. It's not easy to get into, but it's the best tool for the job (outside JSON).

Only other option is to parse it manually. You would use methods like Substring to get the string from first index of '[' to first index of ']' and then parse that with double.Parse. This is very error-prone method, but it exists

1 Reply

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

Answer by Legend_Bacon · Oct 29, 2018 at 11:15 AM

Hello there,


As mentioned above, JSON might be an option. However, depending on the amount of tags you want your text to have (bold, italic, display speed, font size, font color etc...) the JSON might get a little big.


Instead, you can try this:


 using System.Collections;
 using System.Collections.Generic;
 using System.Text.RegularExpressions;
 using UnityEngine;
 
 public class StringTest : MonoBehaviour
 {
     public string myInput = "This is a test[>s:0.5s<][>fsize:14fsize<]";
 
     //Define you start and end tags here
     public string speedTagStart = "[>s:";
     public string speedTagEnd = "s<]";
     public string fontSizeTagStart = "[>fsize:";
     public string fontSizeTagEnd = "fsize<]";
 
     private void Update()
     {
         Debug.Log(ApplyDialogueLineTags(myInput));
     }
 
     public string ApplyDialogueLineTags(string dialogueLine)
     {
         //your default values
         float speed = 1.0f;
         int fontSize = 12;
 
         if (myInput.Contains(speedTagStart))
         {
             //You might want to add some security (try-catch?) here to make sure the value is correct
             speed = float.Parse(ExtractTextInBetween(speedTagStart, speedTagEnd, dialogueLine));
         }
 
         if (myInput.Contains(fontSizeTagStart))
         {
             //You might want to add some security (try-catch?) here to make sure the value is correct
             fontSize = int.Parse(ExtractTextInBetween(fontSizeTagStart, fontSizeTagEnd, dialogueLine));
         }
 
         //Do what you will with the values
         Debug.Log("speed:" + speed.ToString());
         Debug.Log("font size:" + fontSize.ToString());
 
         //Remove all the tags from the string and return it
         return Regex.Replace(dialogueLine, "\\[>.*?<\\]", string.Empty);
     }
 
     public string ExtractTextInBetween(string tagStart, string tagEnd, string input)
     {
         return input.Substring((input.IndexOf(tagStart) + tagStart.Length), (input.IndexOf(tagEnd) - input.IndexOf(tagStart) - tagStart.Length));
     }
 }
 



The code above outputs:

• speed: 0.5

• font size: 14

• This is a test


This code above allows you to have as many tags as you want, as long as you define the Start and End properly. Once all the values have been extracted, it returns the whole string without all the tags so you can display it as it should be.


Please note that I wrote this fast, I didn't include try-catch for invalid values. There may be easier ways to do this, but this should at least give you a lead.



I hope that helps!

Cheers,

~LegendBacon

Comment
Add comment · Show 8 · 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 KenanTheFab · Oct 29, 2018 at 11:46 AM 0
Share

Thank you for your reply! But I'm not really sure how I'd go about implementing this alongside the code from https://www.youtube.com/watch?v=_nRzoTzeyxU this video? Esp considering the string/char(?) is written in the inspector per character/object and whatnot?

avatar image Legend_Bacon KenanTheFab · Oct 29, 2018 at 12:28 PM 1
Share

I didn't watch the whole video, but from what I've seen it shouldn't be too complicated.

Since you already have a reference to the Text component in the IEnumerator TypeSentence(),

you could do sentence = ApplyDialogueLineTags(sentence); before you go char by char. That way you get the sentence without the tags so it outputs properly. Of course, ins$$anonymous$$d of debugging the speed, fontsize etc you would apply it to your Text reference.

avatar image KenanTheFab Legend_Bacon · Oct 29, 2018 at 12:40 PM 0
Share

Ok! I'll try to implement it to the best of my ability, thank you so much for your help!

Show more comments

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.

Follow this Question

Answers Answers and Comments

161 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game slows down when printing text 1 Answer

How Do I Change UI Text With A Button Click? 1 Answer

Changing just the name of person in UI Text 0 Answers

String.Remove not working 1 Answer

Dialogue box wont show in game unless it's in the canvas but... 2 Answers


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