@@ -21,13 +21,7 @@ namespace JCSUnity
2121 // Function pointer
2222 public delegate float TweenDelegate ( float t , float b , float c , float d ) ;
2323
24- /// <summary>
25- /// Callback when time is up.
26- /// </summary>
27- public delegate void TimeIsUpFunc ( ) ;
28-
2924 public delegate int JCS_Range ( int min , int max ) ;
30- public delegate void ReattachCallback ( Transform parent ) ;
3125
3226 /// <summary>
3327 /// All the utility function put here.
@@ -88,6 +82,117 @@ public static bool Parse(string str, bool defaultValue)
8882
8983 #endregion
9084
85+ #region Number
86+
87+ /// <summary>
88+ /// Delta the `num` with `val` and clamp the result with `min`
89+ /// and `max`.
90+ /// </summary>
91+ public static int Delta ( this int data , int val , int max )
92+ {
93+ return data . Delta ( val , 0 , max ) ;
94+ }
95+ public static int Delta ( this int data , int val , int min , int max )
96+ {
97+ return Mathf . Clamp ( data + val , min , max ) ;
98+ }
99+
100+ public static float Delta ( this float data , float val , float max )
101+ {
102+ return data . Delta ( val , 0.0f , max ) ;
103+ }
104+ public static float Delta ( this float data , float val , float min , float max )
105+ {
106+ return Mathf . Clamp ( data + val , min , max ) ;
107+ }
108+
109+ /// <summary>
110+ /// Delta the `num` with `val` by percentage and clamp the
111+ /// result with `min` and `max`.
112+ /// </summary>
113+ public static int DeltaP ( this int data , int p , int max )
114+ {
115+ return data . DeltaP ( p , 0 , max ) ;
116+ }
117+ public static int DeltaP ( this int data , int p , int min , int max )
118+ {
119+ int val = ( int ) ( max * p / 100.0f ) ;
120+
121+ return data . Delta ( val , min , max ) ;
122+ }
123+
124+ public static float DeltaP ( this float data , float p , float max )
125+ {
126+ return data . DeltaP ( p , 0.0f , max ) ;
127+ }
128+ public static float DeltaP ( this float data , float p , float min , float max )
129+ {
130+ float val = ( max * p / 100.0f ) ;
131+
132+ return data . Delta ( val , min , max ) ;
133+ }
134+
135+ #endregion
136+
137+ #region String
138+
139+ /// <summary>
140+ /// Convert byte array to string by charset type.
141+ /// </summary>
142+ /// <param name="data"> Byte array data to convert to string data. </param>
143+ /// <param name="charset"> Target charset type. </param>
144+ /// <returns> String data that had been converted. </returns>
145+ public static string BytesToString ( byte [ ] data , JCS_CharsetType charset )
146+ {
147+ switch ( charset )
148+ {
149+ case JCS_CharsetType . DEFAULT : return Encoding . Default . GetString ( data ) ;
150+ case JCS_CharsetType . ASCII : return Encoding . ASCII . GetString ( data ) ;
151+ case JCS_CharsetType . UTF7 : return Encoding . UTF7 . GetString ( data ) ;
152+ case JCS_CharsetType . UTF8 : return Encoding . UTF8 . GetString ( data ) ;
153+ case JCS_CharsetType . UTF32 : return Encoding . UTF32 . GetString ( data ) ;
154+ case JCS_CharsetType . Unicode : return Encoding . Unicode . GetString ( data ) ;
155+ case JCS_CharsetType . BigEndianUnicode : return Encoding . BigEndianUnicode . GetString ( data ) ;
156+ }
157+ JCS_Debug . LogError ( "This shouldn't happens, charset `bytes to string`" ) ;
158+ return null ;
159+ }
160+
161+ /// <summary>
162+ /// Convert string to byte array by charset type.
163+ /// </summary>
164+ /// <param name="data"> String data to convert to byte array. </param>
165+ /// <param name="charset"> Target charset type. </param>
166+ /// <returns> Byte array that had been converted. </returns>
167+ public static byte [ ] StringToBytes ( string data , JCS_CharsetType charset )
168+ {
169+ switch ( charset )
170+ {
171+ case JCS_CharsetType . DEFAULT : return Encoding . Default . GetBytes ( data ) ;
172+ case JCS_CharsetType . ASCII : return Encoding . ASCII . GetBytes ( data ) ;
173+ case JCS_CharsetType . UTF7 : return Encoding . UTF7 . GetBytes ( data ) ;
174+ case JCS_CharsetType . UTF8 : return Encoding . UTF8 . GetBytes ( data ) ;
175+ case JCS_CharsetType . UTF32 : return Encoding . UTF32 . GetBytes ( data ) ;
176+ case JCS_CharsetType . Unicode : return Encoding . Unicode . GetBytes ( data ) ;
177+ case JCS_CharsetType . BigEndianUnicode : return Encoding . BigEndianUnicode . GetBytes ( data ) ;
178+ }
179+ JCS_Debug . LogError ( "This shouldn't happens, charset `string to bytes`" ) ;
180+ return null ;
181+ }
182+
183+ /// <summary>
184+ /// Simple version of escape url.
185+ /// </summary>
186+ /// <param name="url"> Url you want to escape. </param>
187+ /// <returns> Return the escaped url. </returns>
188+ public static string EscapeURL ( string url )
189+ {
190+ url = url . Replace ( " " , "%20" ) ;
191+ return url ;
192+ }
193+
194+ #endregion
195+
91196 #region Enum
92197
93198 /// <summary>
@@ -398,65 +503,6 @@ public static List<T> RemoveEmptySlotIncludeMissing<T>(List<T> inList)
398503
399504 #endregion
400505
401- #region String
402-
403- /// <summary>
404- /// Convert byte array to string by charset type.
405- /// </summary>
406- /// <param name="data"> Byte array data to convert to string data. </param>
407- /// <param name="charset"> Target charset type. </param>
408- /// <returns> String data that had been converted. </returns>
409- public static string BytesToString ( byte [ ] data , JCS_CharsetType charset )
410- {
411- switch ( charset )
412- {
413- case JCS_CharsetType . DEFAULT : return Encoding . Default . GetString ( data ) ;
414- case JCS_CharsetType . ASCII : return Encoding . ASCII . GetString ( data ) ;
415- case JCS_CharsetType . UTF7 : return Encoding . UTF7 . GetString ( data ) ;
416- case JCS_CharsetType . UTF8 : return Encoding . UTF8 . GetString ( data ) ;
417- case JCS_CharsetType . UTF32 : return Encoding . UTF32 . GetString ( data ) ;
418- case JCS_CharsetType . Unicode : return Encoding . Unicode . GetString ( data ) ;
419- case JCS_CharsetType . BigEndianUnicode : return Encoding . BigEndianUnicode . GetString ( data ) ;
420- }
421- JCS_Debug . LogError ( "This shouldn't happens, charset `bytes to string`" ) ;
422- return null ;
423- }
424-
425- /// <summary>
426- /// Convert string to byte array by charset type.
427- /// </summary>
428- /// <param name="data"> String data to convert to byte array. </param>
429- /// <param name="charset"> Target charset type. </param>
430- /// <returns> Byte array that had been converted. </returns>
431- public static byte [ ] StringToBytes ( string data , JCS_CharsetType charset )
432- {
433- switch ( charset )
434- {
435- case JCS_CharsetType . DEFAULT : return Encoding . Default . GetBytes ( data ) ;
436- case JCS_CharsetType . ASCII : return Encoding . ASCII . GetBytes ( data ) ;
437- case JCS_CharsetType . UTF7 : return Encoding . UTF7 . GetBytes ( data ) ;
438- case JCS_CharsetType . UTF8 : return Encoding . UTF8 . GetBytes ( data ) ;
439- case JCS_CharsetType . UTF32 : return Encoding . UTF32 . GetBytes ( data ) ;
440- case JCS_CharsetType . Unicode : return Encoding . Unicode . GetBytes ( data ) ;
441- case JCS_CharsetType . BigEndianUnicode : return Encoding . BigEndianUnicode . GetBytes ( data ) ;
442- }
443- JCS_Debug . LogError ( "This shouldn't happens, charset `string to bytes`" ) ;
444- return null ;
445- }
446-
447- /// <summary>
448- /// Simple version of escape url.
449- /// </summary>
450- /// <param name="url"> Url you want to escape. </param>
451- /// <returns> Return the escaped url. </returns>
452- public static string EscapeURL ( string url )
453- {
454- url = url . Replace ( " " , "%20" ) ;
455- return url ;
456- }
457-
458- #endregion
459-
460506 #region JSON
461507
462508 /// <summary>
@@ -785,20 +831,19 @@ public static void AttachChildren(RectTransform trans, List<RectTransform> child
785831 /// </summary>
786832 /// <param name="trans"> Transform you want to detach and reattach after callback. </param>
787833 /// <param name="callback"> Callback after detach and before reattach. </param>
788- public static void ReattachSelf ( Transform trans , ReattachCallback callback )
834+ public static void ReattachSelf ( Transform trans , System . Action < Transform > callback )
789835 {
790836 if ( trans == null || callback == null )
791837 return ;
792838
793839 var parent = trans . parent ;
794840 trans . SetParent ( null ) ;
795841
796- if ( callback != null )
797- callback . Invoke ( parent ) ;
842+ callback ? . Invoke ( parent ) ;
798843
799844 trans . SetParent ( parent ) ;
800845 }
801- public static void ReattachSelf ( RectTransform trans , ReattachCallback callback )
846+ public static void ReattachSelf ( RectTransform trans , System . Action < Transform > callback )
802847 {
803848 if ( trans == null || callback == null )
804849 return ;
@@ -808,8 +853,7 @@ public static void ReattachSelf(RectTransform trans, ReattachCallback callback)
808853 var parent = trans . parent ;
809854 trans . SetParent ( canvas . AppRect ) ;
810855
811- if ( callback != null )
812- callback . Invoke ( parent ) ;
856+ callback ? . Invoke ( parent ) ;
813857
814858 trans . SetParent ( parent ) ;
815859 }
0 commit comments