API reference · 日本語API索引

TextMeshProMotionExtensions.cs

Runtime/Bindings/UGUI/TextMeshProMotionExtensions.cs

1 #if MEATMOTION_TMP
2 using System.Buffers;
3 using System.Globalization;
4 using TMPro;
5 #if MEATMOTION_ZSTRING
6 using Cysharp.Text;
7 #endif
8 using Unity.Collections;
9 using UnityEngine;
10 
11 namespace MeatMotion.Extensions
12 {
13     public static partial class TextMeshProMotionExtensions
14     {
15         public static MotionHandle BindToFontSize(this FloatMotionBuilder builder, TMP_Text text)
16             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.fontSize = v);
17 
18         public static MotionHandle BindToFontSize<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
19             where TOptions : unmanaged, IMotionOptions
20             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
21             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.fontSize = v);
22 
23         public static MotionHandle BindToMaxVisibleCharacters<TOptions, TAdapter>(this MotionBuilder<int, TOptions, TAdapter> builder, TMP_Text text)
24             where TOptions : unmanaged, IMotionOptions
25             where TAdapter : unmanaged, IMotionAdapter<int, TOptions>
26             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.maxVisibleCharacters = v);
27 
28         public static MotionHandle BindToMaxVisibleLines<TOptions, TAdapter>(this MotionBuilder<int, TOptions, TAdapter> builder, TMP_Text text)
29             where TOptions : unmanaged, IMotionOptions
30             where TAdapter : unmanaged, IMotionAdapter<int, TOptions>
31             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.maxVisibleLines = v);
32 
33         public static MotionHandle BindToMaxVisibleWords<TOptions, TAdapter>(this MotionBuilder<int, TOptions, TAdapter> builder, TMP_Text text)
34             where TOptions : unmanaged, IMotionOptions
35             where TAdapter : unmanaged, IMotionAdapter<int, TOptions>
36             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.maxVisibleWords = v);
37 
38         public static MotionHandle BindToCharacterSpacing(this FloatMotionBuilder builder, TMP_Text text)
39             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.characterSpacing = v);
40 
41         public static MotionHandle BindToCharacterSpacing<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
42             where TOptions : unmanaged, IMotionOptions
43             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
44             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.characterSpacing = v);
45 
46         public static MotionHandle BindToWordSpacing(this FloatMotionBuilder builder, TMP_Text text)
47             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.wordSpacing = v);
48 
49         public static MotionHandle BindToWordSpacing<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
50             where TOptions : unmanaged, IMotionOptions
51             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
52             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.wordSpacing = v);
53 
54         public static MotionHandle BindToParagraphSpacing(this FloatMotionBuilder builder, TMP_Text text)
55             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.paragraphSpacing = v);
56 
57         public static MotionHandle BindToParagraphSpacing<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
58             where TOptions : unmanaged, IMotionOptions
59             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
60             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.paragraphSpacing = v);
61 
62         public static MotionHandle BindToLineSpacing(this FloatMotionBuilder builder, TMP_Text text)
63             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.lineSpacing = v);
64 
65         public static MotionHandle BindToLineSpacing<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
66             where TOptions : unmanaged, IMotionOptions
67             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
68             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.lineSpacing = v);
69 
70         public static MotionHandle BindToColor<TOptions, TAdapter>(this MotionBuilder<Color, TOptions, TAdapter> builder, TMP_Text text)
71             where TOptions : unmanaged, IMotionOptions
72             where TAdapter : unmanaged, IMotionAdapter<Color, TOptions>
73             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => t.color = v);
74 
75         public static MotionHandle BindToColorR(this FloatMotionBuilder builder, TMP_Text text)
76             => builder.AsMotionBuilder().BindToColorR(text);
77 
78         public static MotionHandle BindToColorR<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
79             where TOptions : unmanaged, IMotionOptions
80             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
81             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) =>
82             {
83                 var current = t.color;
84                 current.r = v;
85                 t.color = current;
86             });
87 
88         public static MotionHandle BindToColorG(this FloatMotionBuilder builder, TMP_Text text)
89             => builder.AsMotionBuilder().BindToColorG(text);
90 
91         public static MotionHandle BindToColorG<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
92             where TOptions : unmanaged, IMotionOptions
93             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
94             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) =>
95             {
96                 var current = t.color;
97                 current.g = v;
98                 t.color = current;
99             });
100 
101         public static MotionHandle BindToColorB(this FloatMotionBuilder builder, TMP_Text text)
102             => builder.AsMotionBuilder().BindToColorB(text);
103 
104         public static MotionHandle BindToColorB<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
105             where TOptions : unmanaged, IMotionOptions
106             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
107             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) =>
108             {
109                 var current = t.color;
110                 current.b = v;
111                 t.color = current;
112             });
113 
114         public static MotionHandle BindToColorA(this FloatMotionBuilder builder, TMP_Text text)
115             => builder.AsMotionBuilder().BindToColorA(text);
116 
117         public static MotionHandle BindToColorA<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
118             where TOptions : unmanaged, IMotionOptions
119             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
120             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) =>
121             {
122                 var current = t.color;
123                 current.a = v;
124                 t.color = current;
125             });
126 
127         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<FixedString32Bytes, TOptions, TAdapter> builder, TMP_Text text)
128             where TOptions : unmanaged, IMotionOptions
129             where TAdapter : unmanaged, IMotionAdapter<FixedString32Bytes, TOptions>
130             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => SetUtf8(ref v, t));
131 
132         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<FixedString64Bytes, TOptions, TAdapter> builder, TMP_Text text)
133             where TOptions : unmanaged, IMotionOptions
134             where TAdapter : unmanaged, IMotionAdapter<FixedString64Bytes, TOptions>
135             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => SetUtf8(ref v, t));
136 
137         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<FixedString128Bytes, TOptions, TAdapter> builder, TMP_Text text)
138             where TOptions : unmanaged, IMotionOptions
139             where TAdapter : unmanaged, IMotionAdapter<FixedString128Bytes, TOptions>
140             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => SetUtf8(ref v, t));
141 
142         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<FixedString512Bytes, TOptions, TAdapter> builder, TMP_Text text)
143             where TOptions : unmanaged, IMotionOptions
144             where TAdapter : unmanaged, IMotionAdapter<FixedString512Bytes, TOptions>
145             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => SetUtf8(ref v, t));
146 
147         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<FixedString4096Bytes, TOptions, TAdapter> builder, TMP_Text text)
148             where TOptions : unmanaged, IMotionOptions
149             where TAdapter : unmanaged, IMotionAdapter<FixedString4096Bytes, TOptions>
150             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => SetUtf8(ref v, t));
151 
152         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<int, TOptions, TAdapter> builder, TMP_Text text)
153             where TOptions : unmanaged, IMotionOptions
154             where TAdapter : unmanaged, IMotionAdapter<int, TOptions>
155             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => SetInteger(v, t));
156 
157         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<int, TOptions, TAdapter> builder, TMP_Text text, string format)
158             where TOptions : unmanaged, IMotionOptions
159             where TAdapter : unmanaged, IMotionAdapter<int, TOptions>
160             => builder.Bind(UIBinding.Require(text, nameof(text)), format, static (v, t, pattern) => SetFormatted(t, pattern, v));
161 
162         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<long, TOptions, TAdapter> builder, TMP_Text text)
163             where TOptions : unmanaged, IMotionOptions
164             where TAdapter : unmanaged, IMotionAdapter<long, TOptions>
165             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => SetInteger(v, t));
166 
167         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<long, TOptions, TAdapter> builder, TMP_Text text, string format)
168             where TOptions : unmanaged, IMotionOptions
169             where TAdapter : unmanaged, IMotionAdapter<long, TOptions>
170             => builder.Bind(UIBinding.Require(text, nameof(text)), format, static (v, t, pattern) => SetFormatted(t, pattern, v));
171 
172         public static MotionHandle BindToText(this FloatMotionBuilder builder, TMP_Text text)
173             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => SetFloat(t, v));
174 
175         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text)
176             where TOptions : unmanaged, IMotionOptions
177             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
178             => builder.Bind(UIBinding.Require(text, nameof(text)), static (v, t) => SetFloat(t, v));
179 
180         public static MotionHandle BindToText(this FloatMotionBuilder builder, TMP_Text text, string format)
181             => builder.Bind(UIBinding.Require(text, nameof(text)), format, static (v, t, pattern) => SetFormatted(t, pattern, v));
182 
183         public static MotionHandle BindToText<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, TMP_Text text, string format)
184             where TOptions : unmanaged, IMotionOptions
185             where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
186             => builder.Bind(UIBinding.Require(text, nameof(text)), format, static (v, t, pattern) => SetFormatted(t, pattern, v));
187 
188         static void SetFormatted<T>(TMP_Text target, string pattern, T value)
189         {
190 #if MEATMOTION_ZSTRING
191             target.SetTextFormat(pattern, value);
192 #else
193             target.text = string.Format(pattern, value);
194 #endif
195         }
196 
197         static void SetFloat(TMP_Text target, float value)
198         {
199 #if MEATMOTION_ZSTRING
200             target.SetTextFormat("{0}", value);
201 #else
202             target.SetText("{0}", value);
203 #endif
204         }
205 
206         static unsafe void SetUtf8<T>(ref T value, TMP_Text target)
207             where T : unmanaged, INativeList<byte>, IUTF8Bytes
208         {
209             var chars = ArrayPool<char>.Shared.Rent(System.Math.Max(1, value.Length));
210             try
211             {
212                 int length;
213                 fixed (T* source = &value)
214                 fixed (char* dest = chars)
215                     Unicode.Utf8ToUtf16(source->GetUnsafePtr(), source->Length, dest, out length, chars.Length);
216                 target.SetText(chars, 0, length);
217             }
218             finally { ArrayPool<char>.Shared.Return(chars); }
219         }
220 
221         static void SetInteger(long value, TMP_Text target)
222         {
223             var chars = ArrayPool<char>.Shared.Rent(20);
224             try
225             {
226                 value.TryFormat(chars, out int length, provider: CultureInfo.InvariantCulture);
227                 target.SetText(chars, 0, length);
228             }
229             finally { ArrayPool<char>.Shared.Return(chars); }
230         }
231     }
232 }
233 #endif