docs.unity.cn
    Show / Hide Table of Contents

    Creating a custom source

    You can create custom sources using the ISource interface.

    The following example shows how to create a source that generates a random number. This could then be combined with a choose Formatter to produce different random responses.

    [Serializable]
    public class RandomValueSource : ISource
    {
        public int min = 1;
        public int max = 5;
    
        public string selector = "random";
    
        public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
        {
            if (selectorInfo.SelectorText != selector)
                return false;
    
            selectorInfo.Result = Random.Range(min, max);
            return true;
        }
    }
    
    Example Smart String Result
    {random:choose(1|2|3}:Hello|Greetings|Welcome|Hi}! Welcome!
    Back to top Copyright © 2020 Unity Technologies
    Generated by DocFX
    on Thursday, October 28, 2021