Skip to content

Instantly share code, notes, and snippets.

@Fscibe
Created October 5, 2019 07:48
Show Gist options
  • Save Fscibe/04a89d76a0d988f5e8e15b1e68f3af4e to your computer and use it in GitHub Desktop.
Save Fscibe/04a89d76a0d988f5e8e15b1e68f3af4e to your computer and use it in GitHub Desktop.
Drop-down Term selector for Unity I2 Localization
using UnityEngine;
/// <summary>
/// Custom attribute to display a drop-down list containing localized Terms.
///
/// Usage:
///
/// class Foo : ScriptableObject
/// {
/// [LocID]
/// string nameID;
/// }
///
/// </summary>
public class LocIDAttribute : PropertyAttribute
{
}
using I2.Loc;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Editor for LocIDAttribute.
/// </summary>
[CustomPropertyDrawer(typeof(LocIDAttribute))]
public class LocIDAttributeEditor : PropertyDrawer
{
private string[] _termsArray = null;
private List<string> _terms = null;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
// Get term list
if (_terms == null || _termsArray == null)
{
_terms = LocalizationManager.GetTermsList();
_terms.Sort(System.StringComparer.OrdinalIgnoreCase);
_termsArray = _terms.ToArray();
}
// Edit value
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(property.displayName));
var amountRect = new Rect(position.x, position.y, position.width, position.height);
int currentIndex = _terms.IndexOf(property.stringValue);
currentIndex = EditorGUI.Popup(amountRect, currentIndex, _termsArray);
if (currentIndex >= 0)
{
property.stringValue = _termsArray[currentIndex];
}
EditorGUI.EndProperty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment