mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] Extension
[+] Extension
This commit is contained in:
59
Assets/TEngine/Runtime/Extension/StringExtension.cs
Normal file
59
Assets/TEngine/Runtime/Extension/StringExtension.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
/// <summary>
|
||||
/// 对 string 的扩展方法。
|
||||
/// </summary>
|
||||
public static class StringExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 从指定字符串中的指定位置处开始读取一行。
|
||||
/// </summary>
|
||||
/// <param name="rawString">指定的字符串。</param>
|
||||
/// <param name="position">从指定位置处开始读取一行,读取后将返回下一行开始的位置。</param>
|
||||
/// <returns>读取的一行字符串。</returns>
|
||||
public static string ReadLine(this string rawString, ref int position)
|
||||
{
|
||||
if (position < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int length = rawString.Length;
|
||||
int offset = position;
|
||||
while (offset < length)
|
||||
{
|
||||
char ch = rawString[offset];
|
||||
switch (ch)
|
||||
{
|
||||
case '\r':
|
||||
case '\n':
|
||||
if (offset > position)
|
||||
{
|
||||
string line = rawString.Substring(position, offset - position);
|
||||
position = offset + 1;
|
||||
if ((ch == '\r') && (position < length) && (rawString[position] == '\n'))
|
||||
{
|
||||
position++;
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
offset++;
|
||||
position++;
|
||||
break;
|
||||
|
||||
default:
|
||||
offset++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (offset > position)
|
||||
{
|
||||
string line = rawString.Substring(position, offset - position);
|
||||
position = offset;
|
||||
return line;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/Extension/StringExtension.cs.meta
Normal file
11
Assets/TEngine/Runtime/Extension/StringExtension.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cf3c6e399e5abde4e8b867e4dc5ca9ee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
340
Assets/TEngine/Runtime/Extension/UnityExtension.cs
Normal file
340
Assets/TEngine/Runtime/Extension/UnityExtension.cs
Normal file
@@ -0,0 +1,340 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 对 Unity 的扩展方法。
|
||||
/// </summary>
|
||||
public static class UnityExtension
|
||||
{
|
||||
private static readonly List<Transform> s_CachedTransforms = new List<Transform>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取或增加组件。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要获取或增加的组件。</typeparam>
|
||||
/// <param name="gameObject">目标对象。</param>
|
||||
/// <returns>获取或增加的组件。</returns>
|
||||
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
|
||||
{
|
||||
T component = gameObject.GetComponent<T>();
|
||||
if (component == null)
|
||||
{
|
||||
component = gameObject.AddComponent<T>();
|
||||
}
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或增加组件。
|
||||
/// </summary>
|
||||
/// <param name="gameObject">目标对象。</param>
|
||||
/// <param name="type">要获取或增加的组件类型。</param>
|
||||
/// <returns>获取或增加的组件。</returns>
|
||||
public static Component GetOrAddComponent(this GameObject gameObject, Type type)
|
||||
{
|
||||
Component component = gameObject.GetComponent(type);
|
||||
if (component == null)
|
||||
{
|
||||
component = gameObject.AddComponent(type);
|
||||
}
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 GameObject 是否在场景中。
|
||||
/// </summary>
|
||||
/// <param name="gameObject">目标对象。</param>
|
||||
/// <returns>GameObject 是否在场景中。</returns>
|
||||
/// <remarks>若返回 true,表明此 GameObject 是一个场景中的实例对象;若返回 false,表明此 GameObject 是一个 Prefab。</remarks>
|
||||
public static bool InScene(this GameObject gameObject)
|
||||
{
|
||||
return gameObject.scene.name != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 递归设置游戏对象的层次。
|
||||
/// </summary>
|
||||
/// <param name="gameObject"><see cref="GameObject" /> 对象。</param>
|
||||
/// <param name="layer">目标层次的编号。</param>
|
||||
public static void SetLayerRecursively(this GameObject gameObject, int layer)
|
||||
{
|
||||
gameObject.GetComponentsInChildren(true, s_CachedTransforms);
|
||||
for (int i = 0; i < s_CachedTransforms.Count; i++)
|
||||
{
|
||||
s_CachedTransforms[i].gameObject.layer = layer;
|
||||
}
|
||||
|
||||
s_CachedTransforms.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取 <see cref="Vector3" /> 的 (x, y, z) 转换为 <see cref="Vector2" /> 的 (x, z)。
|
||||
/// </summary>
|
||||
/// <param name="vector3">要转换的 Vector3。</param>
|
||||
/// <returns>转换后的 Vector2。</returns>
|
||||
public static Vector2 ToVector2(this Vector3 vector3)
|
||||
{
|
||||
return new Vector2(vector3.x, vector3.z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取 <see cref="Vector2" /> 的 (x, y) 转换为 <see cref="Vector3" /> 的 (x, 0, y)。
|
||||
/// </summary>
|
||||
/// <param name="vector2">要转换的 Vector2。</param>
|
||||
/// <returns>转换后的 Vector3。</returns>
|
||||
public static Vector3 ToVector3(this Vector2 vector2)
|
||||
{
|
||||
return new Vector3(vector2.x, 0f, vector2.y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取 <see cref="Vector2" /> 的 (x, y) 和给定参数 y 转换为 <see cref="Vector3" /> 的 (x, 参数 y, y)。
|
||||
/// </summary>
|
||||
/// <param name="vector2">要转换的 Vector2。</param>
|
||||
/// <param name="y">Vector3 的 y 值。</param>
|
||||
/// <returns>转换后的 Vector3。</returns>
|
||||
public static Vector3 ToVector3(this Vector2 vector2, float y)
|
||||
{
|
||||
return new Vector3(vector2.x, y, vector2.y);
|
||||
}
|
||||
|
||||
#region Transform
|
||||
|
||||
/// <summary>
|
||||
/// 设置绝对位置的 x 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="newValue">x 坐标值。</param>
|
||||
public static void SetPositionX(this Transform transform, float newValue)
|
||||
{
|
||||
Vector3 v = transform.position;
|
||||
v.x = newValue;
|
||||
transform.position = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置绝对位置的 y 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="newValue">y 坐标值。</param>
|
||||
public static void SetPositionY(this Transform transform, float newValue)
|
||||
{
|
||||
Vector3 v = transform.position;
|
||||
v.y = newValue;
|
||||
transform.position = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置绝对位置的 z 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="newValue">z 坐标值。</param>
|
||||
public static void SetPositionZ(this Transform transform, float newValue)
|
||||
{
|
||||
Vector3 v = transform.position;
|
||||
v.z = newValue;
|
||||
transform.position = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加绝对位置的 x 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="deltaValue">x 坐标值增量。</param>
|
||||
public static void AddPositionX(this Transform transform, float deltaValue)
|
||||
{
|
||||
Vector3 v = transform.position;
|
||||
v.x += deltaValue;
|
||||
transform.position = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加绝对位置的 y 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="deltaValue">y 坐标值增量。</param>
|
||||
public static void AddPositionY(this Transform transform, float deltaValue)
|
||||
{
|
||||
Vector3 v = transform.position;
|
||||
v.y += deltaValue;
|
||||
transform.position = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加绝对位置的 z 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="deltaValue">z 坐标值增量。</param>
|
||||
public static void AddPositionZ(this Transform transform, float deltaValue)
|
||||
{
|
||||
Vector3 v = transform.position;
|
||||
v.z += deltaValue;
|
||||
transform.position = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置相对位置的 x 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="newValue">x 坐标值。</param>
|
||||
public static void SetLocalPositionX(this Transform transform, float newValue)
|
||||
{
|
||||
Vector3 v = transform.localPosition;
|
||||
v.x = newValue;
|
||||
transform.localPosition = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置相对位置的 y 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="newValue">y 坐标值。</param>
|
||||
public static void SetLocalPositionY(this Transform transform, float newValue)
|
||||
{
|
||||
Vector3 v = transform.localPosition;
|
||||
v.y = newValue;
|
||||
transform.localPosition = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置相对位置的 z 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="newValue">z 坐标值。</param>
|
||||
public static void SetLocalPositionZ(this Transform transform, float newValue)
|
||||
{
|
||||
Vector3 v = transform.localPosition;
|
||||
v.z = newValue;
|
||||
transform.localPosition = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加相对位置的 x 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="deltaValue">x 坐标值。</param>
|
||||
public static void AddLocalPositionX(this Transform transform, float deltaValue)
|
||||
{
|
||||
Vector3 v = transform.localPosition;
|
||||
v.x += deltaValue;
|
||||
transform.localPosition = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加相对位置的 y 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="deltaValue">y 坐标值。</param>
|
||||
public static void AddLocalPositionY(this Transform transform, float deltaValue)
|
||||
{
|
||||
Vector3 v = transform.localPosition;
|
||||
v.y += deltaValue;
|
||||
transform.localPosition = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加相对位置的 z 坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="deltaValue">z 坐标值。</param>
|
||||
public static void AddLocalPositionZ(this Transform transform, float deltaValue)
|
||||
{
|
||||
Vector3 v = transform.localPosition;
|
||||
v.z += deltaValue;
|
||||
transform.localPosition = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置相对尺寸的 x 分量。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="newValue">x 分量值。</param>
|
||||
public static void SetLocalScaleX(this Transform transform, float newValue)
|
||||
{
|
||||
Vector3 v = transform.localScale;
|
||||
v.x = newValue;
|
||||
transform.localScale = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置相对尺寸的 y 分量。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="newValue">y 分量值。</param>
|
||||
public static void SetLocalScaleY(this Transform transform, float newValue)
|
||||
{
|
||||
Vector3 v = transform.localScale;
|
||||
v.y = newValue;
|
||||
transform.localScale = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置相对尺寸的 z 分量。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="newValue">z 分量值。</param>
|
||||
public static void SetLocalScaleZ(this Transform transform, float newValue)
|
||||
{
|
||||
Vector3 v = transform.localScale;
|
||||
v.z = newValue;
|
||||
transform.localScale = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加相对尺寸的 x 分量。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="deltaValue">x 分量增量。</param>
|
||||
public static void AddLocalScaleX(this Transform transform, float deltaValue)
|
||||
{
|
||||
Vector3 v = transform.localScale;
|
||||
v.x += deltaValue;
|
||||
transform.localScale = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加相对尺寸的 y 分量。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="deltaValue">y 分量增量。</param>
|
||||
public static void AddLocalScaleY(this Transform transform, float deltaValue)
|
||||
{
|
||||
Vector3 v = transform.localScale;
|
||||
v.y += deltaValue;
|
||||
transform.localScale = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加相对尺寸的 z 分量。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="deltaValue">z 分量增量。</param>
|
||||
public static void AddLocalScaleZ(this Transform transform, float deltaValue)
|
||||
{
|
||||
Vector3 v = transform.localScale;
|
||||
v.z += deltaValue;
|
||||
transform.localScale = v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 二维空间下使 <see cref="Transform" /> 指向指向目标点的算法,使用世界坐标。
|
||||
/// </summary>
|
||||
/// <param name="transform"><see cref="Transform" /> 对象。</param>
|
||||
/// <param name="lookAtPoint2D">要朝向的二维坐标点。</param>
|
||||
/// <remarks>假定其 forward 向量为 <see cref="Vector3.up" />。</remarks>
|
||||
public static void LookAt2D(this Transform transform, Vector2 lookAtPoint2D)
|
||||
{
|
||||
Vector3 vector = lookAtPoint2D.ToVector3() - transform.position;
|
||||
vector.y = 0f;
|
||||
|
||||
if (vector.magnitude > 0f)
|
||||
{
|
||||
transform.rotation = Quaternion.LookRotation(vector.normalized, Vector3.up);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Transform
|
||||
}
|
11
Assets/TEngine/Runtime/Extension/UnityExtension.cs.meta
Normal file
11
Assets/TEngine/Runtime/Extension/UnityExtension.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a1a3443e3fec16542b5f5c354803bcd4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user