Skip to content

majecty/Unity3dRichTextHelper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity3dRichTextHelper

This library helps you get substring of unity3d's rich text. You can use this to implement a dialogue system

How to use(simple)

using RichTextSubstringHelper;

var richText = "<color=blue>blue</color>black";
richText.RichTextSubString(3); // <color=blue>blu</color>
richText.RichTextSubString(6); // <color=blue>blue</color>bl

When you want to make a dialogue system, displaying richtext may cause a trouble. CommonError

This library helps you get substring of richtext easily.

How to Install

Get unity package from below link

https://github.com/majecty/Unity3dRichTextHelper/releases

Or just copy and paste this file

https://github.com/majecty/Unity3dRichTextHelper/blob/master/Assets/RichTextHelper/RichTextHelper.cs

More example

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using RichTextSubstringHelper;

public class NewBehaviourScript : MonoBehaviour {
    [SerializeField]
    Text uiText;
    IEnumerator Start () {
        var firstText = "<color=blue>blah</color>x";
        for (int i = 0; i < firstText.RichTextLength(); i++)
        {
            yield return new WaitForSeconds(0.5f);
            uiText.text = firstText.RichTextSubString(i + 1);
        }
    }

}

Performance

richText.RichTextSubString(i) takes O(i) time because it should read string character by character every time.

If you consider performance, use like below example.

using System.Collections;
using UnityEngine;
using RichTextSubstringHelper;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour {
    [SerializeField]
    Text uiText;

    IEnumerator Start ()
    {
        var richText = "<color=blue>blue</color>black";
        var maker = new RichTextSubStringMaker(richText);
        
        while (maker.IsConsumable())
        {
            maker.Consume();
            uiText.text = maker.GetRichText();
            yield return new WaitForSeconds(0.5f);
        }
    }
}

About

This library helps you get substring of unity3d's rich text. You can use this to implement a dialogue system

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages