初始化

This commit is contained in:
come
2025-07-26 16:56:42 +08:00
parent 8291dbb91c
commit fa81439a8c
2574 changed files with 328492 additions and 2170 deletions

View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using Random = UnityEngine.Random;
namespace YIUIFramework
{
/// <summary>
/// <see cref="Array"/>.
/// </summary>
public static class ArrayExtensions
{
/// <summary>
/// 洗牌
/// </summary>
public static void Shuffle<T>(this T[] array)
{
for (int i = 0; i < array.Length; ++i)
{
var randIdx = Random.Range(0, array.Length);
(array[randIdx], array[i]) = (array[i], array[randIdx]);
}
}
/// <summary>
/// 删除重复
/// </summary>
public static T[] RemoveDuplicate<T>(this T[] array)
{
var lookup = new HashSet<T>();
foreach (var i in array)
{
if (!lookup.Contains(i))
{
lookup.Add(i);
}
}
var newArray = new T[lookup.Count];
int index = 0;
foreach (var i in lookup)
{
newArray[index++] = i;
}
return newArray;
}
/// <summary>
/// 将数组强制转换为子类
/// </summary>
public static U[] Cast<T, U>(this T[] array)
where U : class, T
where T : class
{
return Array.ConvertAll<T, U>(array, input => input as U);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0cd2498236654c55b05babc0c6f337cb
timeCreated: 1679041183

View File

@@ -0,0 +1,76 @@
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// <see cref="UnityEngine.Color"/>.
/// </summary>
public static class ColorExtensions
{
/// <summary>
/// 设置颜色的alpha值。
/// </summary>
public static Color[] SetAlpha(this Color[] colors, float alpha)
{
for (int i = 0; i < colors.Length; ++i)
{
colors[i].a = alpha;
}
return colors;
}
/// <summary>
/// 设置颜色的alpha值。
/// </summary>
public static Color SetAlpha(this Color color, float alpha)
{
color.a = alpha;
return color;
}
/// <summary>
/// 将颜色值固定为[0,1]
/// </summary>
public static Color Clamp(this Color c)
{
for (int i = 0; i < 4; ++i)
{
if (float.IsNaN(c[i]) || float.IsNegativeInfinity(c[i]))
{
c[i] = 0.0f;
}
else if (float.IsPositiveInfinity(c[i]))
{
c[i] = 1.0f;
}
else
{
c[i] = Mathf.Clamp(c[i], 0.0f, 1.0f);
}
}
return c;
}
/// <summary>
/// 计算颜色的亮度。
/// </summary>
public static float Luminance(this Color c)
{
return (0.3f * c.r) + (0.59f * c.g) + (0.11f * c.b);
}
/// <summary>
/// 只使用颜色中的RGB组件
/// </summary>
public static Color LerpRGB(Color a, Color b, float t)
{
return new Color(
Mathf.Lerp(a.r, b.r, t),
Mathf.Lerp(a.g, b.g, t),
Mathf.Lerp(a.b, b.b, t),
a.a);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 90c0b72094f94cd2ae0f33236da8a9d0
timeCreated: 1679041183

View File

@@ -0,0 +1,98 @@
using UnityEngine;
using UnityEngine.Assertions;
namespace YIUIFramework
{
/// <summary>
/// <see cref="UnityEngine.Component"/>.
/// </summary>
public static class ComponentExtensions
{
/// <summary>
/// 从目标组件中获取一个组件,如果是组件类型不存在,则添加
/// </summary>
public static Component GetOrAddComponent(
this Component obj, System.Type type)
{
var component = obj.GetComponent(type);
if (component == null)
{
component = obj.gameObject.AddComponent(type);
}
return component;
}
/// <summary>
/// 从目标组件中获取一个组件,如果是组件类型不存在,则添加
/// </summary>
public static T GetOrAddComponent<T>(
this Component obj) where T : Component
{
var component = obj.GetComponent<T>();
if (component == null)
{
component = obj.gameObject.AddComponent<T>();
}
return component;
}
/// <summary>
/// 从目标组件中获取一个组件,如果是组件类型不存在,则添加
/// 标记不保存
/// </summary>
public static T GetOrAddComponentDontSave<T>(
this Component obj) where T : Component
{
var component = obj.GetComponent<T>();
if (component == null)
{
component = obj.gameObject.AddComponent<T>();
component.hideFlags = HideFlags.DontSave;
}
return component;
}
/// <summary>
/// 检查目标组件的GameObject上是否有一个或多个指定类型的组件
/// </summary>
public static bool HasComponent(
this Component obj, System.Type type)
{
return obj.GetComponent(type) != null;
}
/// <summary>
/// 检查目标组件的GameObject上是否有一个或多个指定类型的组件
/// </summary>
public static bool HasComponent<T>(
this Component obj) where T : Component
{
return obj.GetComponent<T>() != null;
}
/// <summary>
/// 在parent中查找组件即使该组件处于非活动状态或已禁用 都能查询
/// </summary>
public static T GetComponentInParentHard<T>(
this Component obj) where T : Component
{
Assert.IsNotNull(obj);
var transform = obj.transform;
while (transform != null)
{
var component = transform.GetComponent<T>();
if (component != null)
{
return component;
}
transform = transform.parent;
}
return null;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 54e3cb3cb3a346ca8844971eff47f7a2
timeCreated: 1679041183

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
namespace YIUIFramework
{
/// <summary>
/// <see cref="Dictionary"/>.
/// </summary>
public static class DictionaryExtensions
{
/// <summary>
/// 删除过滤器检查的所有值
/// </summary>
public static void RemoveAll<K, V>(
this Dictionary<K, V> dic, Func<K, V, bool> filter)
{
var sweep = RemoveList<K>.SweepList;
var itr = dic.GetEnumerator();
while (itr.MoveNext())
{
var kv = itr.Current;
if (filter(kv.Key, kv.Value))
{
sweep.Add(kv.Key);
}
}
for (int i = 0; i < sweep.Count; ++i)
{
dic.Remove(sweep[i]);
}
sweep.Clear();
}
private static class RemoveList<T>
{
private static List<T> sweepList = new List<T>();
public static List<T> SweepList
{
get { return sweepList; }
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a22ea6f1b8894437ba02170d16dc731e
timeCreated: 1679041183

View File

@@ -0,0 +1,93 @@
using UnityEngine;
using UnityEngine.Assertions;
namespace YIUIFramework
{
/// <summary>
/// <see cref="GameObject"/>.
/// </summary>
public static class GameObjectExtensions
{
/// <summary>
/// 从目标组件中获取一个组件,如果是组件类型不存在,则添加
/// </summary>
public static Component GetOrAddComponent(
this GameObject obj, System.Type type)
{
var component = obj.GetComponent(type);
if (component == null)
{
component = obj.gameObject.AddComponent(type);
}
return component;
}
/// <summary>
/// 从目标组件中获取一个组件,如果是组件类型不存在,则添加
/// </summary>
public static T GetOrAddComponent<T>(
this GameObject obj) where T : Component
{
var component = obj.GetComponent<T>();
if (component == null)
{
component = obj.gameObject.AddComponent<T>();
}
return component;
}
/// <summary>
/// 检查目标组件上是否有一个或多个特定类型的组件
/// </summary>
public static bool HasComponent(
this GameObject obj, System.Type type)
{
return obj.GetComponent(type) != null;
}
/// <summary>
/// 检查目标组件上是否有一个或多个特定类型的组件
/// </summary>
public static bool HasComponent<T>(
this GameObject obj) where T : Component
{
return obj.GetComponent<T>() != null;
}
/// <summary>
/// 为GameObject及其所有子对象设置图层。
/// </summary>
public static void SetLayerRecursively(this GameObject obj, int layer)
{
obj.layer = layer;
foreach (Transform t in obj.transform)
{
t.gameObject.SetLayerRecursively(layer);
}
}
/// <summary>
/// 在parent中查找组件即使该组件处于非活动状态或禁用状态。
/// </summary>
public static T GetComponentInParentHard<T>(
this GameObject obj) where T : Component
{
Assert.IsNotNull(obj);
var transform = obj.transform;
while (transform != null)
{
var component = transform.GetComponent<T>();
if (component != null)
{
return component;
}
transform = transform.parent;
}
return null;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 13290aaa4b6649b787fc3f8a6cb1275c
timeCreated: 1679041183

View File

@@ -0,0 +1,284 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace YIUIFramework
{
public static class ListExt
{
/// <summary>
/// 将列表转为字典
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TItem"></typeparam>
/// <param name="list"></param>
/// <param name="getKeyHandler"></param>
/// <returns></returns>
public static Dictionary<TKey, TItem> ToDictionary<TKey, TItem>(
this IList<TItem> list, Func<TItem, TKey> getKeyHandler)
{
if (list == null)
{
return EmptyValue<Dictionary<TKey, TItem>>.Value;
}
var dic = new Dictionary<TKey, TItem>(list.Count);
for (int i = 0; i < list.Count; i++)
{
dic[getKeyHandler(list[i])] = list[i];
}
return dic;
}
/// <summary>
/// 排序自身
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="source"></param>
/// <param name="keySelector"></param>
/// <param name="isDescending"></param>
public static void OrderSelf<TSource, TKey>(this IList<TSource> source, Func<TSource, TKey> keySelector,
bool isDescending = false)
{
IOrderedEnumerable<TSource> result;
if (isDescending)
{
result = source.OrderByDescending(keySelector);
}
else
{
result = source.OrderBy(keySelector);
}
int i = 0;
foreach (TSource item in result)
{
source[i] = item;
i++;
}
}
/// <summary>
/// 取出数据最后一个元素(取出后,这个元素就不在数据里)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static T Pop<T>(this IList<T> list)
{
if (list.Count < 1)
{
return default;
}
var lastIndex = list.Count - 1;
var lastItem = list[lastIndex];
list.RemoveAt(lastIndex);
return lastItem;
}
/// <summary>
/// 取出数据第一个元素(取出后,这个元素就不在数据里)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static T Shift<T>(this IList<T> list)
{
if (list.Count < 1)
{
return default;
}
var firstItem = list[0];
list.RemoveAt(0);
return firstItem;
}
/// <summary>
/// 在数组开头,添加一个或多个元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="inst"></param>
/// <param name="items"></param>
/// <returns></returns>
public static T[] Unshift<T>(this IList<T> inst, params T[] items)
{
var addCount = items.Length;
var newArr = new T[addCount + inst.Count];
for (int i = 0; i < addCount; i++)
{
newArr[i] = items[i];
}
for (int i = 0; i < inst.Count; i++)
{
newArr[addCount + i] = inst[i];
}
return newArr;
}
/// <summary>
/// 用于IndexOf
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool EqualsUInt(uint a, uint b)
{
return a == b;
}
public static int IndexOf<T>(this IList<T> list, T value,
int start = 0, int count = 0, Func<T, T, bool> equalsFun = null)
{
count = count < 1
? list.Count
: MathUtil.FixedByRange(count, 0, list.Count);
if (equalsFun == null)
{
for (int i = start; i < count; i++)
{
if (list[i].Equals(value))
{
return i;
}
}
}
else
{
for (int i = start; i < count; i++)
{
if (equalsFun(list[i], value))
{
return i;
}
}
}
return -1;
}
/// <summary>
/// 快速数组数据移除方法
/// 当数组顺序不敏感时使用
/// </summary>
/// <param name="list"></param>
/// <param name="index"></param>
/// <returns></returns>
public static bool FastRemoveAt(this IList list, int index)
{
return FastRemoveAt(list, index, out _);
}
/// <summary>
/// 快速数组数据移除方法
/// 当数组顺序不敏感时使用
/// </summary>
/// <param name="list"></param>
/// <param name="index"></param>
/// <param name="curMoveItemIndex">被移动的对象现在所在的index, 当值小于零时表示没有被移动的物体</param>
/// <returns></returns>
public static bool FastRemoveAt(this IList list, int index, out int curMoveItemIndex)
{
int len = list.Count;
if (len < 1 || index >= len)
{
curMoveItemIndex = -1;
return false;
}
int lastIndex = len - 1;
object lastItem = list[lastIndex];
list.RemoveAt(lastIndex);
if (index != lastIndex)
{
list[index] = lastItem;
curMoveItemIndex = index;
}
else
{
curMoveItemIndex = -1;
}
return true;
}
public static bool FastRemove(this IList list, object item)
{
var index = list.IndexOf(item);
if (index < 0)
{
return false;
}
return FastRemoveAt(list, index, out _);
}
public static bool VerifyIndex(this IList list, int index, object item)
{
int len = list.Count;
if (len < 1
|| index >= len
|| index < 0)
{
return false;
}
return list[index] == item;
}
public static bool SafeSetValue<T>(this IList<T> list, int index, T value)
{
if (null == list
|| index < 0
|| index >= list.Count)
{
return false;
}
list[index] = value;
return true;
}
public static T SafeGetValue<T>(this IList<T> list, int index, T defValue = default(T))
{
if (list == null ||
index >= list.Count)
{
return defValue;
}
return list[index];
}
public static T GetRnd<T>(this IList<T> list, Random random)
{
int len = list.Count;
if (len < 1)
{
return default(T);
}
return list[random.Next(0, len)];
}
public static bool ContainsByUint(this uint[] list, uint value)
{
for (int i = 0; i < list.Length; i++)
{
if (list[i] == value)
{
return true;
}
}
return false;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 689b50cce9e840aeb66c81fabf27db5b
timeCreated: 1681720464

View File

@@ -0,0 +1,46 @@
using System.Collections.Generic;
using Random = UnityEngine.Random;
namespace YIUIFramework
{
/// <summary>
/// <see cref="List"/>.
/// </summary>
public static class ListExtensions
{
/// <summary>
/// 洗牌
/// </summary>
public static void Shuffle<T>(this IList<T> array)
{
for (int i = 0; i < array.Count; ++i)
{
var randIdx = Random.Range(0, array.Count);
(array[randIdx], array[i]) = (array[i], array[randIdx]);
}
}
/// <summary>
/// 删除重复元素。
/// </summary>
public static void RemoveDuplicate<T>(this List<T> list)
{
var lookup = new Dictionary<T, int>();
foreach (var i in list)
{
int value = 0;
if (!lookup.TryGetValue(i, out value))
{
lookup.Add(i, 0);
}
}
list.Clear();
foreach (var i in lookup)
{
list.Add(i.Key);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: df81df60414f4f6689c3d57cec624bae
timeCreated: 1679041183

View File

@@ -0,0 +1,23 @@
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// <see cref="UnityEngine.Quaternion"/>.
/// </summary>
public static class QuaternionExtensions
{
/// <summary>
/// 从字符串中解析四元素
/// </summary>
public static Quaternion Parse(string text)
{
var elements = text.Substring(1, text.Length - 2).Split(',');
float x = float.Parse(elements[0]);
float y = float.Parse(elements[1]);
float z = float.Parse(elements[2]);
float w = float.Parse(elements[3]);
return new Quaternion(x, y, z, w);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9802e51fadd64f95a771dd6a037fea4c
timeCreated: 1679041183

View File

@@ -0,0 +1,74 @@
using System;
using System.Text.RegularExpressions;
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// <see cref="Rect"/>
/// </summary>
public static class RectExtensions
{
private static Regex parseRegex;
public static Rect Parse(string text)
{
Rect value;
if (!TryParse(text, out value))
{
var msg = string.Format(
"The string {0} can not convert to Rect.", text);
throw new FormatException(msg);
}
return value;
}
public static bool TryParse(string text, out Rect rect)
{
if (parseRegex == null)
{
parseRegex = new Regex(
@"^\(x:(.*), y:(.*), width:(.*), height:(.*)\)$");
}
var match = parseRegex.Match(text);
if (!match.Success || match.Groups.Count != 5)
{
rect = Rect.zero;
return false;
}
float x;
if (!float.TryParse(match.Groups[1].Value, out x))
{
rect = Rect.zero;
return false;
}
float y;
if (!float.TryParse(match.Groups[2].Value, out y))
{
rect = Rect.zero;
return false;
}
float width;
if (!float.TryParse(match.Groups[3].Value, out width))
{
rect = Rect.zero;
return false;
}
float height;
if (!float.TryParse(match.Groups[4].Value, out height))
{
rect = Rect.zero;
return false;
}
rect = new Rect(x, y, width, height);
return true;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9c7ddd6439c541a78db4c52118ebab44
timeCreated: 1679041183

View File

@@ -0,0 +1,49 @@
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// <see cref="RectTransform"/>.
/// </summary>
public static class RectTransformExtensions
{
private static Vector3[] corners = new Vector3[4];
/// <summary>
/// 获取世界中心位置
/// </summary>
public static Vector3 GetWorldCenter(this RectTransform transform)
{
transform.GetWorldCorners(corners);
return (corners[0] + corners[2]) / 2;
}
/// <summary>
/// 获取世界中心位置 X
/// </summary>
public static float GetWorldCenterX(this RectTransform transform)
{
transform.GetWorldCorners(corners);
return (corners[0].x + corners[2].x) / 2;
}
/// <summary>
/// 获取世界中心位置 Y
/// </summary>
public static float GetWorldCenterY(this RectTransform transform)
{
transform.GetWorldCorners(corners);
return (corners[0].y + corners[2].y) / 2;
}
/// <summary>
/// 获取世界中心位置 Z
/// </summary>
public static float GetWorldCenterZ(this RectTransform transform)
{
transform.GetWorldCorners(corners);
return (corners[0].z + corners[2].z) / 2;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 88964973e0524b3f806e5726a3fd5643
timeCreated: 1679041183

View File

@@ -0,0 +1,276 @@
using System.Text;
using UnityEngine;
using UnityEngine.Assertions;
namespace YIUIFramework
{
/// <summary>
/// <see cref="StringBuilder"/>.
/// </summary>
public static class StringBuilderExtensions
{
// These digits are here in a static array to support hex with simple,
// easily-understandable code. Since A-Z don't sit next to 0-9 in the
// ASCII table.
private static readonly char[] Digits = new char[]
{
'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'
};
// Matches standard .NET formatting dp's
private static readonly uint DefaultDecimalPlaces = 5;
private static readonly char DefaultPadChar = '0';
/// <summary>
/// Convert a given unsigned integer value to a string and concatenate
/// onto the StringBuilder. Any base value allowed.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder,
uint uintVal,
uint padAmount,
char padChar,
uint baseVal)
{
Assert.IsTrue(padAmount >= 0);
Assert.IsTrue(baseVal > 0 && baseVal <= 16);
// Calculate length of integer when written out
uint length = 0;
uint lengthCalc = uintVal;
do
{
lengthCalc /= baseVal;
++length;
} while (lengthCalc > 0);
// Pad out space for writing.
builder.Append(padChar, (int)Mathf.Max(padAmount, length));
int strpos = builder.Length;
// We're writing backwards, one character at a time.
while (length > 0)
{
--strpos;
// Lookup from static char array, to cover hex values too
builder[strpos] = Digits[uintVal % baseVal];
uintVal /= baseVal;
--length;
}
return builder;
}
/// <summary>
/// Convert a given unsigned integer value to a string and concatenate
/// onto the StringBuilder. Assume no padding and base ten.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder, uint uintVal)
{
builder.Concat(uintVal, 0, DefaultPadChar, 10);
return builder;
}
/// <summary>
/// Convert a given unsigned integer value to a string and concatenate
/// onto the StringBuilder. Assume base ten.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder, uint uintVal, uint padAmount)
{
builder.Concat(uintVal, padAmount, DefaultPadChar, 10);
return builder;
}
/// <summary>
/// Convert a given unsigned integer value to a string and concatenate
/// onto the StringBuilder. Assume base ten.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder,
uint uintVal,
uint padAmount,
char padChar)
{
builder.Concat(uintVal, padAmount, padChar, 10);
return builder;
}
/// <summary>
/// Convert a given signed integer value to a string and concatenate
/// onto the StringBuilder. Any base value allowed.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder,
int intVal,
uint padAmount,
char padChar,
uint baseVal)
{
Assert.IsTrue(padAmount >= 0);
Assert.IsTrue(baseVal > 0 && baseVal <= 16);
// Deal with negative numbers
if (intVal < 0)
{
builder.Append('-');
// This is to deal with Int32.MinValue
uint uintVal = uint.MaxValue - ((uint)intVal) + 1;
builder.Concat(uintVal, padAmount, padChar, baseVal);
}
else
{
builder.Concat((uint)intVal, padAmount, padChar, baseVal);
}
return builder;
}
/// <summary>
/// Convert a given signed integer value to a string and concatenate
/// onto the StringBuilder. Assume no padding and base ten.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder, int intVal)
{
builder.Concat(intVal, 0, DefaultPadChar, 10);
return builder;
}
/// <summary>
/// Convert a given signed integer value to a string and concatenate
/// onto the StringBuilder. Assume base ten.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder, int intVal, uint padAmount)
{
builder.Concat(intVal, padAmount, DefaultPadChar, 10);
return builder;
}
/// <summary>
/// Convert a given signed integer value to a string and concatenate
/// onto the StringBuilder. Assume base ten.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder,
int intVal,
uint padAmount,
char padChar)
{
builder.Concat(intVal, padAmount, padChar, 10);
return builder;
}
/// <summary>
/// Convert a given float value to a string and concatenate onto the
/// StringBuilder
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder,
float floatVal,
uint decimalPlaces,
uint padAmount,
char padChar)
{
Assert.IsTrue(padAmount >= 0);
if (decimalPlaces == 0)
{
// No decimal places, just round up and print it as an int
// Agh, Math.Floor() just works on doubles/decimals. Don't want
// to cast! Let's do this the old-fashioned way.
int intVal;
if (floatVal >= 0.0f)
{
// Round up
intVal = (int)(floatVal + 0.5f);
}
else
{
// Round down for negative numbers
intVal = (int)(floatVal - 0.5f);
}
builder.Concat(intVal, padAmount, padChar, 10);
}
else
{
int intPart = (int)floatVal;
// First part is easy, just cast to an integer
builder.Concat(intPart, padAmount, padChar, 10);
// Decimal point
builder.Append('.');
// Work out remainder we need to print after the d.p.
float remainder = Mathf.Abs(floatVal - intPart);
// Multiply up to become an int that we can print
do
{
remainder *= 10;
--decimalPlaces;
} while (decimalPlaces > 0);
// Round up. It's guaranteed to be a positive number, so no
// extra work required here.
remainder += 0.5f;
// All done, print that as an int!
builder.Concat((uint)remainder, 0, '0', 10);
}
return builder;
}
/// <summary>
/// Convert a given float value to a string and concatenate onto the
/// StringBuilder. Assumes five decimal places, and no padding.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder, float floatVal)
{
builder.Concat(
floatVal, DefaultDecimalPlaces, 0, DefaultPadChar);
return builder;
}
/// <summary>
/// Convert a given float value to a string and concatenate onto the
/// StringBuilder. Assumes no padding.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder,
float floatVal,
uint decimalPlaces)
{
builder.Concat(floatVal, decimalPlaces, 0, DefaultPadChar);
return builder;
}
/// <summary>
/// Convert a given float value to a string and concatenate onto the
/// StringBuilder.
/// </summary>
public static StringBuilder Concat(
this StringBuilder builder,
float floatVal,
uint decimalPlaces,
uint padAmount)
{
builder.Concat(floatVal, decimalPlaces, padAmount, DefaultPadChar);
return builder;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 055773ae5da5440584d6eda463e00cb6
timeCreated: 1679041183

View File

@@ -0,0 +1,139 @@
using UnityEngine;
namespace YIUIFramework
{
public static class TransformExtensions
{
public static void SetPosition(
this Transform transform, float x, float y, float z)
{
transform.position = new Vector3(x, y, z);
}
public static void SetLocalPosition(
this Transform transform, float x, float y, float z)
{
transform.localPosition = new Vector3(x, y, z);
}
public static void SetLocalScale(
this Transform transform, float x, float y, float z)
{
transform.localScale = new Vector3(x, y, z);
}
public static Transform FindHard(this Transform trans, string path)
{
if (path == string.Empty)
{
return trans;
}
var target = trans;
var names = path.Split('/');
foreach (var name in names)
{
bool find = false;
foreach (Transform child in target)
{
if (child.name == name)
{
target = child;
find = true;
break;
}
}
if (!find)
{
target = null;
break;
}
}
return target;
}
public static Transform FindByName(
this Transform trans, string name1, string name2)
{
if (trans.name.Equals(name1) || trans.name.Equals(name2))
{
return trans;
}
for (int i = 0; i < trans.childCount; ++i)
{
var child = trans.GetChild(i);
var result = FindByName(child, name1, name2);
if (result != null)
{
return result;
}
}
return null;
}
public static Transform FindByName(
this Transform trans,
string name1,
string name2,
string name3)
{
if (trans.name.Equals(name1) ||
trans.name.Equals(name2) ||
trans.name.Equals(name3))
{
return trans;
}
for (int i = 0; i < trans.childCount; ++i)
{
var child = trans.GetChild(i);
var result = FindByName(child, name1, name2, name3);
if (result != null)
{
return result;
}
}
return null;
}
public static Transform FindByName(this Transform trans, string name)
{
if (trans.name == name)
{
return trans;
}
for (int i = 0; i < trans.childCount; ++i)
{
var child = trans.GetChild(i);
var result = FindByName(child, name);
if (result != null)
{
return result;
}
}
return null;
}
public static Transform FindChildByName(this Transform trans, string childName)
{
var childT = trans.Find(childName);
if (childT != null) return childT;
for (int i = 0; i < trans.childCount; i++)
{
childT = FindChildByName(trans.GetChild(i), childName);
if (childT != null) return childT;
}
return null;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d17bb502fad1478db8bf90f7271e1623
timeCreated: 1679041183

View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace YIUIFramework
{
/// <summary>
/// <see cref="System.Type"/>.
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Check whether the type if a struct
/// </summary>
public static bool IsStruct(this Type type)
{
return type.IsValueType && !type.IsPrimitive && !type.IsEnum;
}
/// <summary>
/// Gets <see cref="FieldInfo"/> including base classes.
/// </summary>
public static FieldInfo[] GetFieldInfosIncludingBaseClasses(
this Type type, BindingFlags bindingFlags)
{
var fieldInfos = type.GetFields(bindingFlags);
if (type.BaseType == typeof(object))
{
return fieldInfos;
}
var fieldInfoList = new List<FieldInfo>(fieldInfos);
while (type.BaseType != typeof(object))
{
type = type.BaseType;
fieldInfos = type.GetFields(bindingFlags);
// Look for fields we do not have listed yet and merge them
// into the main list
foreach (var fieldInfo in fieldInfos)
{
bool found = false;
foreach (var recordField in fieldInfoList)
{
if (recordField.Name == fieldInfo.Name &&
recordField.DeclaringType == fieldInfo.DeclaringType)
{
found = true;
break;
}
}
if (!found)
{
fieldInfoList.Add(fieldInfo);
}
}
}
return fieldInfoList.ToArray();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ba003288a4b040d2a12e1e2a7864e230
timeCreated: 1679041183

View File

@@ -0,0 +1,22 @@
using UnityEngine;
namespace YIUIFramework
{
public static class UnityObjectExtensions
{
public static void SafeDestroySelf(
this Object obj)
{
if (obj == null) return;
#if UNITY_EDITOR
if (!Application.isPlaying)
Object.DestroyImmediate(obj);
else
Object.Destroy(obj);
#else
Object.Destroy(obj);
#endif
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: def020af107b4215a45d6a16deae0dda
timeCreated: 1679041183

View File

@@ -0,0 +1,53 @@
using System;
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// <see cref="UnityEngine.Vector2"/>.
/// </summary>
public static class Vector2Extensions
{
/// <summary>
/// Parse Vector2 from a string.
/// </summary>
public static Vector2 Parse(string text)
{
Vector2 value;
if (!TryParse(text, out value))
{
var msg = string.Format(
"The string {0} can not convert to Rect.", text);
throw new FormatException(msg);
}
return value;
}
/// <summary>
/// Try to parse Vector2 from a string.
/// </summary>
public static bool TryParse(string text, out Vector2 v)
{
if (text.Length < 2 ||
text[0] != '(' ||
text[text.Length - 1] != ')')
{
v = Vector2.zero;
return false;
}
var elements = text.Substring(1, text.Length - 2).Split(',');
if (elements.Length != 2)
{
v = Vector2.zero;
return false;
}
float x = float.Parse(elements[0]);
float y = float.Parse(elements[1]);
v = new Vector2(x, y);
return true;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0eb3073cc14c4083a8dd642774507315
timeCreated: 1679041183

View File

@@ -0,0 +1,54 @@
using System;
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// <see cref="UnityEngine.Vector3"/>.
/// </summary>
public static class Vector3Extensions
{
/// <summary>
/// Parse Vector3 from a string.
/// </summary>
public static Vector3 Parse(string text)
{
Vector3 value;
if (!TryParse(text, out value))
{
var msg = string.Format(
"The string {0} can not convert to Rect.", text);
throw new FormatException(msg);
}
return value;
}
/// <summary>
/// Try to parse Vector3 from a string.
/// </summary>
public static bool TryParse(string text, out Vector3 v)
{
if (text.Length < 2 ||
text[0] != '(' ||
text[text.Length - 1] != ')')
{
v = Vector3.zero;
return false;
}
var elements = text.Substring(1, text.Length - 2).Split(',');
if (elements.Length != 3)
{
v = Vector3.zero;
return false;
}
float x = float.Parse(elements[0]);
float y = float.Parse(elements[1]);
float z = float.Parse(elements[2]);
v = new Vector3(x, y, z);
return true;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cd59ae2e96fb4d30a4c52e771688bcf1
timeCreated: 1679041183

View File

@@ -0,0 +1,55 @@
using System;
using UnityEngine;
namespace YIUIFramework
{
/// <summary>
/// <see cref="UnityEngine.Vector4"/>.
/// </summary>
public static class Vector4Extensions
{
/// <summary>
/// Parse Vector4 from a string.
/// </summary>
public static Vector4 Parse(string text)
{
Vector4 value;
if (!TryParse(text, out value))
{
var msg = string.Format(
"The string {0} can not convert to Rect.", text);
throw new FormatException(msg);
}
return value;
}
/// <summary>
/// Try to parse Vector4 from a string.
/// </summary>
public static bool TryParse(string text, out Vector4 v)
{
if (text.Length < 2 ||
text[0] != '(' ||
text[text.Length - 1] != ')')
{
v = Vector4.zero;
return false;
}
var elements = text.Substring(1, text.Length - 2).Split(',');
if (elements.Length != 4)
{
v = Vector4.zero;
return false;
}
float x = float.Parse(elements[0]);
float y = float.Parse(elements[1]);
float z = float.Parse(elements[2]);
float w = float.Parse(elements[3]);
v = new Vector4(x, y, z, w);
return true;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5d17403b8e9e4fcfab6d272865eb9937
timeCreated: 1679041183