Ignore AIClient/Library and AIClient/HybridCLRData permanently; include HybridCLR/YooAsset project setup and UI tooling. Co-authored-by: Cursor <cursoragent@cursor.com>
106 lines
2.9 KiB
C#
106 lines
2.9 KiB
C#
#if UNITY_EDITOR
|
|
using System;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace AIGame.EditorTools
|
|
{
|
|
/// <summary>
|
|
/// Unity 获得焦点时自动转换 Doc/UIPrefabCreater 中变更的 UI JSON。
|
|
/// </summary>
|
|
[InitializeOnLoad]
|
|
public static class JsonUIAutoConvert
|
|
{
|
|
private const string PrefKey = "AIGame.UI.AutoConvertJsonOnFocus";
|
|
private static bool s_Busy;
|
|
private static double s_LastTime;
|
|
|
|
static JsonUIAutoConvert()
|
|
{
|
|
EditorApplication.focusChanged += OnFocusChanged;
|
|
}
|
|
|
|
private static void OnFocusChanged(bool focused)
|
|
{
|
|
if (!focused)
|
|
{
|
|
return;
|
|
}
|
|
AutoConvertOnFocus();
|
|
}
|
|
|
|
[MenuItem("Tools/UI/Convert Changed JSON Prefabs Now", false, 200)]
|
|
public static void ConvertChangedNow()
|
|
{
|
|
ConvertChanged(false, "manual");
|
|
}
|
|
|
|
[MenuItem("Tools/UI/Auto Convert JSON On Focus", false, 202)]
|
|
public static void ToggleAutoConvert()
|
|
{
|
|
bool enabled = !IsEnabled();
|
|
EditorPrefs.SetBool(PrefKey, enabled);
|
|
Menu.SetChecked("Tools/UI/Auto Convert JSON On Focus", enabled);
|
|
Debug.Log("[UIGen] Auto convert JSON on focus: " + (enabled ? "enabled" : "disabled"));
|
|
}
|
|
|
|
[MenuItem("Tools/UI/Auto Convert JSON On Focus", true)]
|
|
public static bool ToggleAutoConvertValidate()
|
|
{
|
|
Menu.SetChecked("Tools/UI/Auto Convert JSON On Focus", IsEnabled());
|
|
return true;
|
|
}
|
|
|
|
public static void AutoConvertOnFocus()
|
|
{
|
|
if (!IsEnabled())
|
|
{
|
|
return;
|
|
}
|
|
ConvertChanged(false, "focus");
|
|
}
|
|
|
|
private static bool IsEnabled()
|
|
{
|
|
if (EditorPrefs.HasKey(PrefKey))
|
|
{
|
|
return EditorPrefs.GetBool(PrefKey, true);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static void ConvertChanged(bool force, string reason)
|
|
{
|
|
if (s_Busy)
|
|
{
|
|
return;
|
|
}
|
|
if (!force && EditorApplication.timeSinceStartup - s_LastTime < 1.0)
|
|
{
|
|
return;
|
|
}
|
|
if (EditorApplication.isCompiling || EditorApplication.isUpdating || EditorApplication.isPlayingOrWillChangePlaymode)
|
|
{
|
|
return;
|
|
}
|
|
|
|
s_Busy = true;
|
|
s_LastTime = EditorApplication.timeSinceStartup;
|
|
try
|
|
{
|
|
string jsonRoot = JsonUIPrefabBuilder.ResolveDefaultJsonRoot();
|
|
JsonUIPrefabBuilder.BuildChanged(jsonRoot, reason, force);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogException(ex);
|
|
}
|
|
finally
|
|
{
|
|
s_Busy = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|