Class VariablesGroupAsset
Collection of IVariable that can be used during formatting of a localized string.
Inherited Members
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 |
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
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 |
Implements
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);