-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormattedTMPTextApplicator.cs
68 lines (58 loc) · 1.65 KB
/
FormattedTMPTextApplicator.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
using System;
using TriInspector;
using UnityEngine;
namespace CodeWriter.ViewBinding.Applicators.UI
{
using TMPro;
[DisallowMultipleComponent]
[RequireComponent(typeof(TMP_Text))]
[AddComponentMenu("View Binding/UI/[Binding] Formatted TMP Text Applicator")]
public sealed class FormattedTMPTextApplicator : 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);
try
{
textBuilder.AppendFormat(format, extraContexts);
target.SetText(textBuilder.RawCharArray, 0, textBuilder.Length);
}
finally
{
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
}
}