-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalizedTMPTextApplicator.cs
72 lines (62 loc) · 1.96 KB
/
LocalizedTMPTextApplicator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using TriInspector;
namespace CodeWriter.ViewBinding
{
using TMPro;
using UnityEngine;
[DisallowMultipleComponent]
[RequireComponent(typeof(TMP_Text))]
[AddComponentMenu("View Binding/UI/[Binding] Localized TMP Text Applicator")]
public class LocalizedTMPTextApplicator : ApplicatorBase
{
[Required]
[SerializeField]
private TMP_Text target;
[Required]
[SerializeField]
[OnValueChanged(nameof(Apply))]
private string format;
[Required]
[SerializeField]
[ViewContextCollection]
private ViewContextBase[] extraContexts = Array.Empty<ViewContextBase>();
protected override void Apply()
{
var textBuilder = new ValueTextBuilder(ValueTextBuilder.DefaultCapacity);
var localizedTextBuilder = new ValueTextBuilder(ValueTextBuilder.DefaultCapacity);
try
{
textBuilder.AppendFormat(format, extraContexts);
var localizedString = BindingsLocalization.Localize(ref textBuilder);
localizedTextBuilder.AppendFormat(localizedString, extraContexts);
target.SetText(localizedTextBuilder.RawCharArray, 0, localizedTextBuilder.Length);
}
finally
{
localizedTextBuilder.Dispose();
textBuilder.Dispose();
}
#if UNITY_EDITOR
if (!Application.isPlaying)
{
UnityEditor.EditorUtility.SetDirty(target);
}
#endif
}
#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();
if (target == null || target.gameObject != gameObject)
{
target = GetComponent<TMP_Text>();
}
}
protected override void Reset()
{
base.Reset();
target = GetComponent<TMP_Text>();
}
#endif
}
}