Skip to content

Commit cc4f167

Browse files
committed
Add UIElements support
1 parent 7be78c7 commit cc4f167

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ PostprocessorsFile=Translation\{Lang}\Text\_Postprocessors.txt ;File that c
313313

314314
[TextFrameworks]
315315
EnableUGUI=True ;Enable or disable UGUI translation
316+
EnableUIElements=True ;Enable or disable UIElements translation
316317
EnableNGUI=True ;Enable or disable NGUI translation
317318
EnableTextMeshPro=True ;Enable or disable TextMeshPro translation
318319
EnableTextMesh=False ;Enable or disable TextMesh translation

src/XUnity.AutoTranslator.Plugin.Core/Configuration/Settings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ internal static class Settings
8787
public static string TranslatorsPath;
8888
public static bool EnableIMGUI;
8989
public static bool EnableUGUI;
90+
public static bool EnableUIElements;
9091
public static bool EnableNGUI;
9192
public static bool EnableTextMeshPro;
9293
public static bool EnableTextMesh;
@@ -230,6 +231,7 @@ public static void Configure()
230231

231232
EnableIMGUI = PluginEnvironment.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableIMGUI", false );
232233
EnableUGUI = PluginEnvironment.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableUGUI", true );
234+
EnableUIElements = PluginEnvironment.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableUIElements", true );
233235
EnableNGUI = PluginEnvironment.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableNGUI", true );
234236
EnableTextMeshPro = PluginEnvironment.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableTextMeshPro", true );
235237
EnableTextMesh = PluginEnvironment.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableTextMesh", false );

src/XUnity.AutoTranslator.Plugin.Core/Extensions/ComponentExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public static bool IsKnownTextType( this object ui )
202202
var type = ui.GetUnityType();
203203

204204
return ( Settings.EnableIMGUI && !_guiContentCheckFailed && IsGUIContentSafe( ui ) )
205+
|| ( Settings.EnableUIElements && UnityTypes.TextElement != null && UnityTypes.TextElement.IsAssignableFrom( type ) )
205206
|| ( Settings.EnableUGUI && UnityTypes.Text != null && UnityTypes.Text.IsAssignableFrom( type ) )
206207
|| ( Settings.EnableNGUI && UnityTypes.UILabel != null && UnityTypes.UILabel.IsAssignableFrom( type ) )
207208
|| ( Settings.EnableTextMesh && UnityTypes.TextMesh != null && UnityTypes.TextMesh.IsAssignableFrom( type ) )
@@ -429,6 +430,10 @@ public static object CreateDerivedProxyIfRequiredAndPossible( this Component ui
429430
{
430431
return Il2CppUtilities.CreateProxyComponentWithDerivedType( ui.Pointer, UnityTypes.Text.ClrType );
431432
}
433+
if( Settings.EnableUIElements && UnityTypes.TextElement != null && UnityTypes.TextElement.IsAssignableFrom( unityType ) )
434+
{
435+
return Il2CppUtilities.CreateProxyComponentWithDerivedType( ui.Pointer, UnityTypes.TextElement.ClrType );
436+
}
432437
else if( Settings.EnableTextMesh && UnityTypes.TextMesh != null && UnityTypes.TextMesh.IsAssignableFrom( unityType ) )
433438
{
434439
return Il2CppUtilities.CreateProxyComponentWithDerivedType( ui.Pointer, UnityTypes.TextMesh.ClrType );
@@ -552,6 +557,13 @@ public static IEnumerable<Component> GetAllTextComponentsInChildren( this GameOb
552557
yield return comp;
553558
}
554559
}
560+
if( Settings.EnableUIElements && UnityTypes.TextElement != null )
561+
{
562+
foreach( var comp in go.GetComponentsInChildren( UnityTypes.TextElement.UnityType, true ) )
563+
{
564+
yield return comp;
565+
}
566+
}
555567
if( Settings.EnableTextMesh && UnityTypes.TextMesh != null )
556568
{
557569
foreach( var comp in go.GetComponentsInChildren( UnityTypes.TextMesh.UnityType, true ) )

src/XUnity.AutoTranslator.Plugin.Core/Hooks/HooksSetup.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using XUnity.AutoTranslator.Plugin.Core.Hooks.NGUI;
1212
using XUnity.AutoTranslator.Plugin.Core.Hooks.TextMeshPro;
1313
using XUnity.AutoTranslator.Plugin.Core.Hooks.UGUI;
14+
using XUnity.AutoTranslator.Plugin.Core.Hooks.UIElements;
1415
using XUnity.Common.Logging;
1516
using XUnity.Common.Utilities;
1617

@@ -102,6 +103,17 @@ public static void InstallTextHooks()
102103
{
103104
XuaLogger.AutoTranslator.Error( e, "An error occurred while setting up hooks for UGUI." );
104105
}
106+
try
107+
{
108+
if( Settings.EnableUIElements )
109+
{
110+
HookingHelper.PatchAll( UIElementsHooks.All, Settings.ForceMonoModHooks );
111+
}
112+
}
113+
catch( Exception e )
114+
{
115+
XuaLogger.AutoTranslator.Error( e, "An error occurred while setting up hooks for UIElements." );
116+
}
105117

106118
try
107119
{
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using UnityEngine;
8+
using XUnity.AutoTranslator.Plugin.Core.Constants;
9+
using XUnity.AutoTranslator.Plugin.Core.Extensions;
10+
using XUnity.AutoTranslator.Plugin.Core.Text;
11+
using XUnity.Common.Constants;
12+
using XUnity.Common.Extensions;
13+
using XUnity.Common.Harmony;
14+
using XUnity.Common.Logging;
15+
using XUnity.Common.MonoMod;
16+
using XUnity.Common.Utilities;
17+
18+
namespace XUnity.AutoTranslator.Plugin.Core.Hooks.UIElements
19+
{
20+
internal static class UIElementsHooks
21+
{
22+
public static readonly Type[] All = new[] {
23+
typeof( TextElement_text_Hook ),
24+
};
25+
}
26+
27+
[HookingHelperPriority( HookPriority.Last )]
28+
internal static class TextElement_text_Hook
29+
{
30+
static bool Prepare( object instance )
31+
{
32+
return UnityTypes.TextElement != null;
33+
}
34+
35+
static MethodBase TargetMethod( object instance )
36+
{
37+
return AccessToolsShim.Property( UnityTypes.TextElement?.ClrType, "text" )?.GetSetMethod();
38+
}
39+
40+
#if MANAGED
41+
static void Postfix( object __instance )
42+
#else
43+
static void Postfix( Il2CppInterop.Runtime.InteropTypes.Il2CppObjectBase __instance )
44+
#endif
45+
{
46+
#if IL2CPP
47+
__instance = Il2CppUtilities.CreateProxyComponentWithDerivedType( __instance.Pointer, UnityTypes.TextElement.ClrType );
48+
#endif
49+
50+
AutoTranslationPlugin.Current.Hook_TextChanged( __instance, false );
51+
}
52+
53+
#if MANAGED
54+
static Action<object, string> _original;
55+
56+
static void MM_Init( object detour )
57+
{
58+
_original = detour.GenerateTrampolineEx<Action<object, string>>();
59+
}
60+
61+
static void MM_Detour( object __instance, string value )
62+
{
63+
_original( __instance, value );
64+
65+
Postfix( __instance );
66+
}
67+
#endif
68+
}
69+
}

src/XUnity.Common/Constants/UnityTypes.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private static void Initialize()
9999
public static readonly TypeContainer Transform = FindType( "UnityEngine.Transform" );
100100
public static readonly TypeContainer TextMesh = FindType( "UnityEngine.TextMesh" );
101101
public static readonly TypeContainer Text = FindType( "UnityEngine.UI.Text" );
102+
public static readonly TypeContainer TextElement = FindType( "UnityEngine.UIElements.TextElement" );
102103
public static readonly TypeContainer Image = FindType( "UnityEngine.UI.Image" );
103104
public static readonly TypeContainer RawImage = FindType( "UnityEngine.UI.RawImage" );
104105
public static readonly TypeContainer MaskableGraphic = FindType( "UnityEngine.UI.MaskableGraphic" );
@@ -314,6 +315,17 @@ public static class IL2CPP
314315
#endif
315316
}
316317

318+
public static class TextElement_Methods
319+
{
320+
#if IL2CPP
321+
public static class IL2CPP
322+
{
323+
public static readonly IntPtr set_text = Il2CppUtilities.GetIl2CppMethod( UnityTypes.TextElement?.ClassPointer, "set_text", typeof( void ), typeof( string ) );
324+
public static readonly IntPtr get_text = Il2CppUtilities.GetIl2CppMethod( UnityTypes.TextElement?.ClassPointer, "get_text", typeof( string ) );
325+
}
326+
#endif
327+
}
328+
317329
public static class InputField_Methods
318330
{
319331
#if IL2CPP

0 commit comments

Comments
 (0)