Skip to content

Instantly share code, notes, and snippets.

View karljj1's full-sized avatar

Karl Jones karljj1

View GitHub Profile
@karljj1
karljj1 / DebugHierarchy.cs
Last active September 19, 2024 15:54
Print the VisualElement hierarchy with debug info. Use this with a diff tool to see what changed.
public static class DebugHierarchy
{
public static string HierarchyToString(VisualElement root)
{
var sb = new StringBuilder();
HierarchyToStringRecursive(root, sb, 0);
var s = sb.ToString();
EditorGUIUtility.systemCopyBuffer = s;
return s;
}
@karljj1
karljj1 / UxmlAttribute_SerializeReference.cs
Last active September 16, 2024 13:51
Polymorphism using UxmlAttribute
using System;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
// Our Interface.
public interface IMyInterface
{
}
@karljj1
karljj1 / UIElement_OverlayExample.cs
Created May 31, 2024 10:09
Example of drawing an overlay over another UI Element.
using UnityEngine;
using UnityEngine.UIElements;
public class Example : MonoBehaviour
{
public UIDocument document;
void Start()
{
var document = GetComponent<UIDocument>();
@karljj1
karljj1 / RemoveLocale.cs
Created February 5, 2024 10:54
Example of removing a locale and its assets
public static void RemoveLocale(LocaleIdentifier localeIdentifier)
{
foreach (var collectionStrings in LocalizationEditorSettings.GetStringTableCollections())
{
var table = collectionStrings.GetTable(localeIdentifier);
if (table != null)
{
collectionStrings.RemoveTable(table);
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(table));
}
@karljj1
karljj1 / DropDownButton.cs
Created February 1, 2024 15:50
UI Toolkit version of the IMGUI DropDownButton
abstract class DropDownButton : BaseField<string>
{
TextElement m_TextElement;
VisualElement m_ArrowElement;
public DropDownButton(string label)
: base(label, null)
{
AddToClassList(EnumField.ussClassName);
labelElement.AddToClassList(EnumField.labelUssClassName);
@karljj1
karljj1 / WordCount.cs
Last active January 15, 2024 17:54
Generate String Table Word Count Report
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.SmartFormat;
using UnityEngine.Localization.SmartFormat.Core.Extensions;
@karljj1
karljj1 / AddressablesBinding.cs
Last active November 23, 2023 14:42
Example of a UI Toolkit custom binding for Addressables (2023.3)
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UIElements;
#if UNITY_EDITOR
public class AssetReferenceConverter<T> : UnityEditor.UIElements.UxmlAttributeConverter<AssetReferenceT<T>> where T : Object
{
public override AssetReferenceT<T> FromString(string value)
{
@karljj1
karljj1 / MarkSmart.cs
Created July 18, 2023 09:56
Mark LocalizeStringEvent entry as Smart
using UnityEditor;
using UnityEditor.Localization;
using UnityEngine.Localization.Components;
public class MarkSmart
{
[MenuItem("CONTEXT/Component/Mark Smart", true)]
public static bool MarkEntrySmartValidate(MenuCommand menuCommand) => menuCommand.context is LocalizeStringEvent;
[MenuItem("CONTEXT/Component/Mark Smart", false)]
@karljj1
karljj1 / SerializedDictionaryExample.cs
Last active February 13, 2024 14:04
Serializing a Dictionary in a VisualElement (Unity 2023.2.0a9)
using System;
using System.Collections.Generic;
using UnityEngine.UIElements;
[Serializable]
class DictItem
{
public int key;
public string value;
}
@karljj1
karljj1 / DefaultTmpFont.cs
Last active December 31, 2023 17:48
Localize the default TextMeshPro font.
using System.Reflection;
using TMPro;
using UnityEngine;
using UnityEngine.Localization;
/// <summary>
/// Sets the default TextMeshPro font to be used.
/// </summary>
[ExecuteInEditMode]
public class DefaultTmpFont : MonoBehaviour