Created
November 25, 2024 17:12
-
-
Save karljj1/2edc2ce0cd107571f0a0cbfede47b05c to your computer and use it in GitHub Desktop.
StringListBinding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using UnityEngine; | |
using UnityEngine.Localization; | |
using UnityEngine.Localization.Settings; | |
using UnityEngine.UIElements; | |
[UxmlObject] | |
public partial class StringListBinding : LocalizedString | |
{ | |
protected override BindingResult Update(in BindingContext context) | |
{ | |
if (IsEmpty) | |
return new BindingResult(BindingStatus.Success); | |
#if UNITY_EDITOR | |
// When not in playmode and not previewing a language we want to show something, so we revert to the project locale. | |
if (!Application.isPlaying && LocaleOverride == null && LocalizationSettings.SelectedLocale == null) | |
{ | |
LocaleOverride = LocalizationSettings.ProjectLocale; | |
} | |
#endif | |
if (!CurrentLoadingOperationHandle.IsDone) | |
return new BindingResult(BindingStatus.Pending); | |
// Convert to a list | |
var list = GetLocalizedString().Split(',').ToList(); | |
var element = context.targetElement; | |
if (ConverterGroups.TrySetValueGlobal(ref element, context.bindingId, list, out var errorCode)) | |
return new BindingResult(BindingStatus.Success); | |
return new BindingResult(BindingStatus.Failure); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related thread https://discussions.unity.com/t/ui-builder-localization-for-dropdown-radio-buttons/1559578/2