Skip to content

Instantly share code, notes, and snippets.

@andrew-raphael-lukasik
Last active August 5, 2024 08:06
Show Gist options
  • Save andrew-raphael-lukasik/e4ae9b45a2c24672c0d1218f77235948 to your computer and use it in GitHub Desktop.
Save andrew-raphael-lukasik/e4ae9b45a2c24672c0d1218f77235948 to your computer and use it in GitHub Desktop.
Language selection menu (for UIToolkit @ Unity)
// void* src = https://gist.github.com/andrew-raphael-lukasik/e4ae9b45a2c24672c0d1218f77235948
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace Localization
{
[UnityEngine.Scripting.Preserve]
public class LocaleList : VisualElement
{
#region fields
const int k_itemHeight = 16;
public int itemHeight { get; set; } = k_itemHeight;
public List<Locale> locales = new List<Locale>();
ListView listview = null;
#endregion
#region constructors
public LocaleList ()
{
listview = new ListView(
itemsSource: locales ,
itemHeight: itemHeight ,
makeItem: OnMakeItem ,
bindItem: OnBindItem
);
listview.selectionType = SelectionType.Single;
{
var style = listview.style;
style.minHeight = itemHeight;
style.flexGrow = 1f;
}
this.Add( listview );
listview.onItemsChosen += OnItemsChosen;
// _listview.onSelectionChange += (objects)=> Debug.Log($"selection change: {objects}");
if( Application.isPlaying )
{
var op = LocalizationSettings.SelectedLocaleAsync;
if( op.IsDone ) InitializeCompleted( op );
else op.Completed += InitializeCompleted;
}
else
{
locales.Add(null);
locales.Add(null);
locales.Add(null);
listview.Refresh();
}
}
#endregion
#region private methods
void InitializeCompleted ( AsyncOperationHandle<Locale> op )
{
locales.Clear();
locales.AddRange( LocalizationSettings.AvailableLocales.Locales );
listview.Refresh();
LocalizationSettings.SelectedLocaleChanged += LocalizationSettings_SelectedLocaleChanged;
}
VisualElement OnMakeItem ()
{
return new Label();
}
void OnBindItem ( VisualElement visualElement , int index )
{
Label label = (Label) visualElement;
Locale locale = locales[index];
label.text = locale!=null ? locale.name : "...";
}
void OnItemsChosen ( IEnumerable<object> objects ) => ChangeLocale( (Locale) objects.FirstOrDefault() );
void ChangeLocale ( Locale locale )
{
if( locale!=null )
{
LocalizationSettings.SelectedLocaleChanged -= LocalizationSettings_SelectedLocaleChanged;
LocalizationSettings.SelectedLocale = locale;
LocalizationSettings.SelectedLocaleChanged += LocalizationSettings_SelectedLocaleChanged;
}
else Debug.LogWarning("locale object is null");
}
void LocalizationSettings_SelectedLocaleChanged ( Locale locale )
{
listview.ScrollToItem( locales.IndexOf(locale) );
}
#endregion
#region nested types
public new class UxmlFactory : UxmlFactory<LocaleList,UxmlTraits> {}
public new class UxmlTraits : VisualElement.UxmlTraits
{
UxmlIntAttributeDescription itemHeightAttr = new UxmlIntAttributeDescription { name="item-height" , defaultValue=k_itemHeight };
public override void Init ( VisualElement ve , IUxmlAttributes attributes , CreationContext context )
{
base.Init( ve , attributes , context );
LocaleList instance = (LocaleList) ve;
instance.itemHeight = itemHeightAttr.GetValueFromBag( attributes , context );
instance.listview.itemHeight = instance.itemHeight;
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment