Version: Unity 6.7 Beta (6000.7)
LanguageEnglish
  • C#

IVariantSelector

interface in Unity.Localization

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Picks which variant key applies right now, for example the current platform, storefront, or gender.

A variant-driven key holds one value per key the selector can report, and resolution returns the value whose key equals IVariantSelector.CurrentKey. Keys are free-form strings, so a selector can model anything: the built-in platform selector reports the current Application.platform, and a custom selector can report a storefront, a day of the week, or a player choice instead. Implement the interface on one serializable class with a public parameterless constructor; the Localization Tables window's selector picker skips types without one, and loading file-based tables recreates selectors with it. The implementation appears in the picker automatically, and its IVariantSelector.AvailableKeys fill the window's Add Variant menu.

Additional resources: ResourceTable, SharedTableData

A selector that varies text by storefront, for builds that ship on more than one store per platform.

using System;
using System.Collections.Generic;

namespace Unity.Localization.Samples { // Varies text by store rather than platform, for a game that ships on Steam and GOG. [Serializable] public class StorefrontVariantSelector : IVariantSelector { // Set once by the game's boot code, for example from a build define or launcher detection. public static string CurrentStorefront = "Steam";

public string SelectorId => "storefront";

public string CurrentKey => CurrentStorefront;

public IEnumerable<string> AvailableKeys => new[] { "Steam", "GOG" }; } }

A selector that combines the two: storefront keys where a storefront is known, platform keys everywhere else.

using System;
using System.Collections.Generic;
using UnityEngine;

namespace Unity.Localization.Samples { // Combines platform and storefront keys: storefront variants win where a storefront is known, // and every other platform resolves through Unity's platform names as usual. [Serializable] public class PlatformOrStorefrontSelector : IVariantSelector { // Set by the game's boot code on PC; left null elsewhere. public static string Storefront;

public string SelectorId => "platform-storefront";

public string CurrentKey => Storefront ?? Application.platform.ToString();

public IEnumerable<string> AvailableKeys { get { foreach (var platform in Enum.GetNames(typeof(RuntimePlatform))) yield return platform; yield return "Steam"; yield return "GOG"; } } } }

Properties

Property Description
AvailableKeys Every key this selector can report.
CurrentKey The variant key that applies to the current runtime context.
SelectorId Stable identifier for this selector kind.