Skip to content

Commit 06331bf

Browse files
meeeesKurtGokhan
andauthored
Add <icon> to UIToolkit, + picking mode & text outline (#128)
* adding support for <icon> to UIToolkit * add TK styles for picking mode & text outline * update font asset to include correct glyphs --------- Co-authored-by: Gökhan Kurt <[email protected]>
1 parent 90e7b4c commit 06331bf

File tree

10 files changed

+401
-6
lines changed

10 files changed

+401
-6
lines changed

Assets/Material Icons/Material Icons SDF.asset

Lines changed: 291 additions & 0 deletions
Large diffs are not rendered by default.

Assets/Material Icons/Material Icons SDF.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Material Icons/Material Icons.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ MonoBehaviour:
1414
m_EditorClassIdentifier:
1515
Name: Material Icons
1616
FontAsset: {fileID: 11400000, guid: 2d3b5da16a7081c4b9d07980bc7922c7, type: 2}
17+
ToolkitFontAsset: {fileID: 11400000, guid: f6206075b3056fc44a6a93f14f64c615, type: 2}
1718
Codepoints: {fileID: 4900000, guid: d8af46a16a86a264c9638732d5ecc1e4, type: 3}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using ReactUnity.Styling;
2+
using UnityEngine;
3+
using UnityEngine.UIElements;
4+
5+
namespace ReactUnity.UIToolkit
6+
{
7+
public class IconComponent : UIToolkitComponent<TextElement>, ITextComponent
8+
{
9+
public string Content => Element.text;
10+
public IconSet Set { get; private set; }
11+
12+
13+
public IconComponent(string text, UIToolkitContext context, string tag) : base(context, tag, true)
14+
{
15+
ApplySet(Context.DefaultIconSet);
16+
17+
if (text != null) SetText(text);
18+
}
19+
20+
public void SetText(string text)
21+
{
22+
if(Set != null)
23+
{
24+
Element.text = Set.ConvertTextContent(text);
25+
}
26+
else
27+
{
28+
Element.text = text;
29+
}
30+
}
31+
32+
public override void SetProperty(string property, object value)
33+
{
34+
if (property == "set") ApplySet(value);
35+
else base.SetProperty(property, value);
36+
}
37+
38+
public void ApplySet(object value)
39+
{
40+
if (value == null)
41+
{
42+
Set = Context.DefaultIconSet;
43+
}
44+
else if (value is IconSet i) Set = i;
45+
else
46+
{
47+
var str = value?.ToString();
48+
if (Context.IconSets.TryGetValue(str, out var ic)) Set = ic;
49+
else Set = null;
50+
}
51+
52+
if (Set != null)
53+
{
54+
Element.style.unityFontDefinition = new StyleFontDefinition(Set.ToolkitFontAsset);
55+
SetText(Element.text);
56+
57+
}
58+
}
59+
}
60+
}

Runtime/Frameworks/UIToolkit/Components/IconComponent.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Frameworks/UIToolkit/Components/UIToolkitComponent.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,15 @@ protected override void ApplyStylesSelf()
152152
TargetElement.style.letterSpacing = StylingHelpers.GetStyleFloat(computed, StyleProperties.letterSpacing).value;
153153
TargetElement.style.wordSpacing = StylingHelpers.GetStyleFloat(computed, StyleProperties.wordSpacing).value;
154154
#endif
155+
TargetElement.style.unityTextOutlineColor = StylingHelpers.GetStyleColor(computed, StyleProperties.textStrokeColor).value;
156+
TargetElement.style.unityTextOutlineWidth = StylingHelpers.GetStyleFloat(computed, StyleProperties.textStrokeWidth).value;
155157

156158
if (computed.HasValue(StyleProperties.backgroundImage))
157159
{
158160
var bg = computed.backgroundImage?.Get(0);
159161

160162
TargetElement.style.backgroundImage = null;
161-
bg?.ResolveImage(Context, TargetElement.layout.size, tx =>
162-
{
163+
bg?.ResolveImage(Context, TargetElement.layout.size, tx => {
163164
if (bg != ComputedStyle.backgroundImage?.Get(0)) return;
164165
TargetElement.style.backgroundImage = tx?.Texture;
165166
});
@@ -186,8 +187,7 @@ protected override void ApplyStylesSelf()
186187

187188
if (computed.HasValue(StyleProperties.fontFamily))
188189
{
189-
if (computed.fontFamily != null) computed.fontFamily?.Get(Context, x =>
190-
{
190+
if (computed.fontFamily != null) computed.fontFamily?.Get(Context, x => {
191191
if (x?.Font != null)
192192
{
193193
TargetElement.style.unityFont = x.Font;
@@ -196,7 +196,8 @@ protected override void ApplyStylesSelf()
196196
#endif
197197
}
198198
#if REACT_TMP
199-
else if (x?.TmpFontAsset != null) {
199+
else if (x?.TmpFontAsset != null)
200+
{
200201
TargetElement.style.unityFont = x?.TmpFontAsset?.sourceFontFile;
201202
#if REACT_TEXTCORE
202203
TargetElement.style.unityFontDefinition = FontDefinition.FromFont(x?.TmpFontAsset?.sourceFontFile);
@@ -293,6 +294,8 @@ protected override void ApplyStylesSelf()
293294
#endif
294295
}
295296
else TargetElement.transform.position = translate;
297+
298+
TargetElement.pickingMode = computed.pointerEvents == PointerEvents.None ? PickingMode.Ignore : PickingMode.Position;
296299
}
297300

298301
protected override void DestroySelf()

Runtime/Frameworks/UIToolkit/General/ReactRendererUIToolkit.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#if UNITY_2021_2_OR_NEWER
2+
using System.Collections.Generic;
23
using ReactUnity.Scheduling;
4+
using ReactUnity.Styling;
35
using ReactUnity.Styling.Rules;
46
using UnityEngine;
57
using UnityEngine.UIElements;
@@ -11,6 +13,8 @@ namespace ReactUnity.UIToolkit
1113
public class ReactRendererUIToolkit : ReactRendererBase
1214
{
1315
public VisualElement Root => GetComponent<UIDocument>()?.rootVisualElement;
16+
public IconSet DefaultIconSet;
17+
public List<IconSet> IconSets;
1418

1519
protected override void ClearRoot()
1620
{
@@ -27,6 +31,8 @@ protected override ReactContext CreateContext(ScriptSource script)
2731
Timer = Timer ?? UnscaledTimer.Instance,
2832
MediaProvider = MediaProvider,
2933
OnRestart = () => Render(),
34+
IconSets = IconSets,
35+
DefaultIconSet = DefaultIconSet,
3036
OnAudioPlayback = PlayAudio,
3137
EngineType = EngineType,
3238
Debug = AdvancedOptions.DebugMode != DebugMode.None,

Runtime/Frameworks/UIToolkit/General/ReactRendererUIToolkit.cs.meta

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Frameworks/UIToolkit/General/UIToolkitContext.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class UIToolkitContext : ReactContext
1414
public VisualElement HostElement;
1515
public Action<AudioClip, float, float> OnAudioPlayback;
1616
public override bool CalculatesLayout => false;
17+
18+
public List<IconSet> IconSets;
19+
public IconSet DefaultIconSet;
1720
}
1821

1922
public static Func<string, string, UIToolkitContext, IUIToolkitComponent<VisualElement>> defaultCreator =
@@ -42,6 +45,7 @@ public static Dictionary<string, Func<string, string, UIToolkitContext, IReactCo
4245
{ "style", (tag, text, context) => new Styling.StyleComponent(context, tag, text) },
4346
{ "script", (tag, text, context) => new Scripting.ScriptComponent(context, tag, text) },
4447
{ "html", (tag, text, context) => new Html.HtmlComponent(context, tag) },
48+
{ "icon", (tag, text, context) => new IconComponent(text, context, tag) },
4549
#if UNITY_2020_1_OR_NEWER
4650
{ "helpbox", (tag, text, context) => new UIToolkitComponent<HelpBox>(context, tag) }, // TODO
4751
#endif
@@ -69,6 +73,10 @@ public static Dictionary<string, Func<string, string, UIToolkitContext, IReactCo
6973
{ "hover", typeof(HoverStateHandler) },
7074
};
7175

76+
public IconSet DefaultIconSet { get; }
77+
public Dictionary<string, IconSet> IconSets { get; } = new Dictionary<string, IconSet>() { };
78+
79+
7280
private Action<AudioClip, float, float> OnAudioPlayback = null;
7381

7482
public VisualElement HostElement { get; }
@@ -77,6 +85,18 @@ public UIToolkitContext(Options options) : base(options)
7785
{
7886
OnAudioPlayback = options.OnAudioPlayback;
7987
HostElement = options.HostElement;
88+
89+
if (options.IconSets != null)
90+
{
91+
if (options.IconSets.Count > 0) IconSets["default"] = options.IconSets[0];
92+
foreach (var ic in options.IconSets) IconSets[ic.Name] = ic;
93+
}
94+
95+
DefaultIconSet = options.DefaultIconSet;
96+
if (DefaultIconSet == null)
97+
{
98+
if (IconSets.TryGetValue("default", out var def)) DefaultIconSet = def;
99+
}
80100
}
81101

82102
public virtual void Initialize()

Runtime/Styling/IconSet.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ namespace ReactUnity.Styling
88
public class IconSet : ScriptableObject
99
{
1010
public string Name;
11+
[Tooltip("For UGUI Use")]
1112
public TMPro.TMP_FontAsset FontAsset;
13+
[Tooltip("For UIToolkit Use")]
14+
public UnityEngine.TextCore.Text.FontAsset ToolkitFontAsset;
1215
public TextAsset Codepoints;
1316

1417
[NonSerialized]

0 commit comments

Comments
 (0)