docs.unity3d.com
    Show / Hide Table of Contents

    Class VariablesGroupAsset

    Collection of IVariable that can be used during formatting of a localized string.

    Inheritance
    Object
    Object
    ScriptableObject
    VariablesGroupAsset
    GlobalVariablesGroup
    Inherited Members
    ScriptableObject.SetDirty()
    ScriptableObject.CreateInstance(String)
    ScriptableObject.CreateInstance(Type)
    ScriptableObject.CreateInstance<T>()
    Object.GetInstanceID()
    Object.GetHashCode()
    Object.Equals(Object)
    Object.Instantiate(Object, Vector3, Quaternion)
    Object.Instantiate(Object, Vector3, Quaternion, Transform)
    Object.Instantiate(Object)
    Object.Instantiate(Object, Transform)
    Object.Instantiate(Object, Transform, Boolean)
    Object.Instantiate<T>(T)
    Object.Instantiate<T>(T, Vector3, Quaternion)
    Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
    Object.Instantiate<T>(T, Transform)
    Object.Instantiate<T>(T, Transform, Boolean)
    Object.Destroy(Object, Single)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, Boolean)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, Single)
    Object.DestroyObject(Object)
    Object.FindSceneObjectsOfType(Type)
    Object.FindObjectsOfTypeIncludingAssets(Type)
    Object.FindObjectsOfType<T>()
    Object.FindObjectOfType<T>()
    Object.FindObjectsOfTypeAll(Type)
    Object.FindObjectOfType(Type)
    Object.name
    Object.hideFlags
    Namespace: UnityEngine.Localization.SmartFormat.PersistentVariables
    Syntax
    [CreateAssetMenu(menuName = "Localization/Variables Group")]
    public class VariablesGroupAsset : ScriptableObject, IVariableGroup, IVariable, IDictionary<string, IVariable>, ICollection<KeyValuePair<string, IVariable>>, IEnumerable<KeyValuePair<string, IVariable>>, IEnumerable, ISerializationCallbackReceiver

    Properties

    Count

    Returns the number of variables in the group.

    Declaration
    public int Count { get; }
    Property Value
    Type Description
    Int32

    IsReadOnly

    Implemented as part of IDictionary but not used. Always returns false.

    Declaration
    public bool IsReadOnly { get; }
    Property Value
    Type Description
    Boolean

    Item[String]

    Gets or sets the IVariable with the specified name.

    Declaration
    public IVariable this[string name] { get; set; }
    Parameters
    Type Name Description
    String name

    The name of the variable.

    Property Value
    Type Description
    IVariable

    The found variable.

    Examples

    This example shows how to get a variable group named "globals".

        var source = LocalizationSettings.StringDatabase.SmartFormatter.GetSourceExtension<PersistentVariablesSource>();
    
        var globalVariables = source["globals"];
    
        var intVariable = new IntVariable { Value = 123 };
    
        // This can be accessed from a Smart String with the following syntax: {globals.my-int}
        globalVariables.Add("my-int", intVariable);
    Exceptions
    Type Condition
    KeyNotFoundException

    Thrown if a variable with the specified name does not exist.

    Keys

    Returns a collection containing all the unique variable names.

    Declaration
    public ICollection<string> Keys { get; }
    Property Value
    Type Description
    ICollection<String>

    Values

    Returns all the variables for this group.

    Declaration
    public ICollection<IVariable> Values { get; }
    Property Value
    Type Description
    ICollection<IVariable>

    Methods

    Add(KeyValuePair<String, IVariable>)

    Declaration
    public void Add(KeyValuePair<string, IVariable> item)
    Parameters
    Type Name Description
    KeyValuePair<String, IVariable> item

    Add(String, IVariable)

    Adds a new Global Variable to use during formatting.

    Declaration
    public void Add(string name, IVariable variable)
    Parameters
    Type Name Description
    String name

    The name of the variable, must be unique. Note the name should not contain any whitespace, if any is found then it will be replaced with with '-'.

    IVariable variable

    The variable to use when formatting. See also BoolVariable, FloatVariable, IntVariable, StringVariable, ObjectVariable.

    Examples

    This example shows how to add a variable named "my-int" to a VariablesGroupAsset named "globals".

        var source = LocalizationSettings.StringDatabase.SmartFormatter.GetSourceExtension<PersistentVariablesSource>();
    
        var globalVariables = source["globals"];
    
        var intVariable = new IntVariable { Value = 123 };
    
        // This can be accessed from a Smart String with the following syntax: {globals.my-int}
        globalVariables.Add("my-int", intVariable);
    Exceptions
    Type Condition
    ArgumentException

    Thrown when name is null or empty.

    ArgumentNullException

    Thrown when variable is null.

    Clear()

    Removes all variables in the group.

    Declaration
    public void Clear()

    Contains(KeyValuePair<String, IVariable>)

    Declaration
    public bool Contains(KeyValuePair<string, IVariable> item)
    Parameters
    Type Name Description
    KeyValuePair<String, IVariable> item

    The item to check for. Both the Key and Value must match.

    Returns
    Type Description
    Boolean

    true if a matching variable could be found or false if one could not.

    ContainsKey(String)

    Returns true if a variable with the specified name exists.

    Declaration
    public bool ContainsKey(string name)
    Parameters
    Type Name Description
    String name

    The variable name to check for.

    Returns
    Type Description
    Boolean

    true if a matching variable could be found or false if one could not.

    ContainsName(String)

    Declaration
    [Obsolete("Please use ContainsKey instead.", false)]
    public bool ContainsName(string name)
    Parameters
    Type Name Description
    String name
    Returns
    Type Description
    Boolean

    CopyTo(KeyValuePair<String, IVariable>[], Int32)

    Copies the variables into an array starting at arrayIndex.

    Declaration
    public void CopyTo(KeyValuePair<string, IVariable>[] array, int arrayIndex)
    Parameters
    Type Name Description
    KeyValuePair<String, IVariable>[] array

    The array to copy the variables into.

    Int32 arrayIndex

    The index to start copying the items into.

    Exceptions
    Type Condition
    ArgumentNullException

    Thrown when the array is null.

    GetEnumerator()

    Returns an enumerator for all variables in this group.

    Declaration
    public IEnumerator GetEnumerator()
    Returns
    Type Description
    IEnumerator

    The enumerator that can be used to iterate through all the variables.

    GetSourceValue(ISelectorInfo)

    The value that will be used when the smart string matches this variable. This value can then be further used by additional sources/formatters.

    Declaration
    public object GetSourceValue(ISelectorInfo _)
    Parameters
    Type Name Description
    ISelectorInfo _
    Returns
    Type Description
    Object
    Implements
    IVariable.GetSourceValue(ISelectorInfo)

    Remove(KeyValuePair<String, IVariable>)

    Removes a variable with the specified key.

    Declaration
    public bool Remove(KeyValuePair<string, IVariable> item)
    Parameters
    Type Name Description
    KeyValuePair<String, IVariable> item

    The item to be removed, only the Key field will be considered.

    Returns
    Type Description
    Boolean

    true if a variable with the specified name was removed, false if one was not.

    Remove(String)

    Removes a variable with the specified name.

    Declaration
    public bool Remove(string name)
    Parameters
    Type Name Description
    String name
    Returns
    Type Description
    Boolean

    true if a variable with the specified name was removed, false if one was not.

    TryGetValue(String, out IVariable)

    Gets the IVariable with the specified name.

    Declaration
    public bool TryGetValue(string name, out IVariable value)
    Parameters
    Type Name Description
    String name

    The name of the variable.

    IVariable value

    The variable that was found or default.

    Returns
    Type Description
    Boolean

    true if a variable was found and false if one could not.

    Implements
    IVariableGroup.TryGetValue(String, out IVariable)
    Examples

    This example shows how to get a variable named "my-float" from a VariablesGroupAsset named "global".

        var source = LocalizationSettings.StringDatabase.SmartFormatter.GetSourceExtension<PersistentVariablesSource>();
    
        // If a group called "globals" does not exist then add one.
        if (!source.TryGetValue("globals", out var globalVariables))
        {
            globalVariables = ScriptableObject.CreateInstance<VariablesGroupAsset>();
            source.Add("globals", globalVariables);
        }
    
        var floatVariable = new FloatVariable { Value = 1.23f };
    
        // This can be accessed from a Smart String with the following syntax: {globals.my-float}
        globalVariables.Add("my-float", floatVariable);

    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Properties
      • Count
      • IsReadOnly
      • Item[String]
      • Keys
      • Values
    • Methods
      • Add(KeyValuePair<String, IVariable>)
      • Add(String, IVariable)
      • Clear()
      • Contains(KeyValuePair<String, IVariable>)
      • ContainsKey(String)
      • ContainsName(String)
      • CopyTo(KeyValuePair<String, IVariable>[], Int32)
      • GetEnumerator()
      • GetSourceValue(ISelectorInfo)
      • Remove(KeyValuePair<String, IVariable>)
      • Remove(String)
      • TryGetValue(String, out IVariable)
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie 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.
    Generated by DocFX on 18 October 2023