UniversalViewer/Assets/Scripts/SharedBasic/Extensions.cs

54 lines
1.7 KiB
C#
Raw Normal View History

2024-04-21 22:38:26 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Extensions
{
#region Transform
public static void SetTransform(this Transform current, SerializableTransform target)
{
current.localPosition = target.Position;
current.localEulerAngles = target.Rotation;
current.localScale = target.Scale;
}
public static void LerpTransform(this Transform current, SerializableTransform source, SerializableTransform target, float amount)
{
current.localPosition = Vector3.Lerp(source.Position, target.Position, amount);
current.localEulerAngles = Quaternion.Lerp(Quaternion.Euler(source.Rotation), Quaternion.Euler(target.Rotation), amount).eulerAngles;
current.localScale = Vector3.Lerp(source.Scale, target.Scale, amount);
}
public static void ResetTransform(this Transform trans)
{
trans.localPosition = Vector3.zero;
trans.localEulerAngles = Vector3.zero;
trans.localScale = Vector3.one;
}
#endregion
#region GameObject
public static GameObject CopyTransfrom(this GameObject go, GameObject other)
{
go.transform.position = other.transform.position;
go.transform.rotation = other.transform.rotation;
go.transform.localScale = other.transform.localScale;
return go;
}
public static T Get<T>(this GameObject go)
{
return go.GetComponent<T>();
}
public static T GetChild<T>(this GameObject go)
{
return go.GetComponentInChildren<T>();
}
public static T[] GetChildren<T>(this GameObject go)
{
return go.GetComponentsInChildren<T>();
}
#endregion
}