using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System; using System.Linq; using System.Collections; using System.Collections.Generic; using Random = UnityEngine.Random; public enum LocatePos { Left, Right, Center, Up, Down, Middle, } public static class Extension { #region List public static T Last(this List list, int index) { return list[list.Count - 1 - index]; } public static T Prev(this List list, int index) { return list[(index + list.Count - 1)%list.Count]; } public static T Next(this List list, int index) { return list[(index + 1)%list.Count]; } public static T Random(this List list, bool remove = false) { if (list.Count == 0) { Debug.Log("Count is 0"); } int index = UnityEngine.Random.Range(0, list.Count); if (remove) { T result = list[index]; list.RemoveAt(index); return result; } else { return list[index]; } } public static bool Valid(this List list) { if (list == null || list.Count == 0) { return false; } else { return true; } } public static bool UniqueAdd(this List list, T obj) { if (list.Contains(obj) == false) { list.Add(obj); return true; } else { return false; } } #endregion #region UGUI public static void Resize(this Image image, float ratioX, float ratioY) { Vector2 newSize = image.sprite.rect.size; newSize.x *= ratioX; newSize.y *= ratioY; image.rectTransform.sizeDelta = newSize; } #endregion #region Event public static void SetButtonEvent(this Button button, UnityAction onClick) { button.onClick = new Button.ButtonClickedEvent(); button.onClick.AddListener(onClick); } public static void AddButtonEvent(this Button button, UnityAction onClick) { button.onClick.AddListener(onClick); } public static void PushButtonEvent(this Button button, UnityAction onClick) { Button.ButtonClickedEvent click = button.onClick; button.onClick = new Button.ButtonClickedEvent(); button.onClick.AddListener(onClick); button.onClick.AddListener(click.Invoke); } public static void AddButtonEventOnetime(this Button button, UnityAction onClick) { onClick += () => { button.onClick.RemoveListener(onClick); }; button.onClick.AddListener(onClick); } public static void PushButtonEventOnetime(this Button button, UnityAction onClick) { onClick += () => { button.onClick.RemoveListener(onClick); }; Button.ButtonClickedEvent click = button.onClick; button.onClick = new Button.ButtonClickedEvent(); button.onClick.AddListener(onClick); button.onClick.AddListener(click.Invoke); } #endregion #region Equal public static bool Equal(this int i1, int i2, int accuracy = 0) { if (Math.Abs(i1 - i2) < accuracy) { return true; } else { return false; } } public static bool Equal(this float f1, float f2, float accuracy = 0.0005f) { if (Math.Abs(f1 - f2) < accuracy) { return true; } else { return false; } } public static bool Equal(this Color c1, Color c2, float accuracy = 0.0005f) { if (Math.Abs(c1.r - c2.r) < accuracy && Math.Abs(c1.g - c2.g) < accuracy && Math.Abs(c1.b - c2.b) < accuracy && Math.Abs(c1.a - c2.a) < accuracy) { return true; } else { return false; } } public static bool Equal(this Vector2 v1, Vector2 v2, float accuracy = 0.0005f) { if (Math.Abs(v1.x - v2.x) < accuracy && Math.Abs(v1.y - v2.y) < accuracy) { return true; } else { return false; } } public static bool Equal(this Vector3 v1, Vector3 v2, float accuracy = 0.0005f) { if (Math.Abs(v1.x - v2.x) < accuracy && Math.Abs(v1.y - v2.y) < accuracy && Math.Abs(v1.z - v2.z) < accuracy) { return true; } else { return false; } } #endregion #region Move public static Shake Shake(this Component comp, float duration, int repeat, Vector3 strength, Curve curve) { return ManaAnim.Shake(comp.transform, duration, repeat, strength, curve); } public static Move2D Move2D(this Component comp, Vector3 destination, float duration, bool local, Curve curve) { return ManaAnim.Move2D(comp.transform, destination, duration, local, curve); } public static Move3D Move3D(this Component comp, Vector3 destination, float duration, bool local, Curve curve) { return ManaAnim.Move3D(comp.transform, destination, duration, local, curve); } public static Move2D MoveOffset2D(this Component comp, Vector3 offset, float duration, bool local, Curve curve) { return ManaAnim.MoveOffset2D(comp.transform, offset, duration, local, curve); } public static Move3D MoveOffset3D(this Component comp, Vector3 offset, float duration, bool local, Curve curve) { return ManaAnim.MoveOffset3D(comp.transform, offset, duration, local, curve); } public static Zoom2D Zoom2D(this Transform target, float origin, float destination, float duration, float stay, Transform targetZoom, Curve curve) { return ManaAnim.Zoom2D(target, origin, destination, duration, stay, targetZoom, curve); } public static Zoom2D Zoom2D(this Transform target, float destination, float duration, float stay, Transform targetZoom, Curve curve) { return ManaAnim.Zoom2D(target, destination, duration, stay, targetZoom, curve); } public static Shake GetShake(this Component comp) { return ManaAnim.GetShake(comp.transform); } public static Move2D GetMove2D(this Component comp) { return ManaAnim.GetMove2D(comp.transform); } public static Move3D GetMove3D(this Component comp) { return ManaAnim.GetMove3D(comp.transform); } public static Zoom2D GetZoom2D(this Component comp) { return ManaAnim.GetZoom2D(comp.transform); } public static Shake CreateShake(this Component comp) { return ManaAnim.CreateShake(comp.transform); } public static Move2D CreateMove2D(this Component comp) { return ManaAnim.CreateMove2D(comp.transform); } public static Move3D CreateMove3D(this Component comp) { return ManaAnim.CreateMove3D(comp.transform); } public static Zoom2D CreateZoom2D(this Component comp) { return ManaAnim.CreateZoom2D(comp.transform); } #endregion #region Alpha public static void SetAlpha(this Graphic graphic, float alpha) { graphic.color = new Color(graphic.color.r, graphic.color.g, graphic.color.b, alpha); } public static void SetAlpha(this TextMesh textMesh, float alpha) { textMesh.color = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, alpha); } public static void SetAlpha(this SpriteRenderer sR, float alpha) { sR.color = new Color(sR.color.r, sR.color.g, sR.color.b, alpha); } #endregion #region String public static T ToEnum(this string str) { return (T)Enum.Parse(typeof(T), str); } #endregion #region Sprite public static Sprite GetSprite(this Component comp) { return comp.GetComponent().sprite; } #endregion #region String public static string Remove(this string str, int startIndex, int endIndex, bool empty) { if (startIndex > endIndex) { throw new Exception(); } return str.Remove(startIndex, endIndex - startIndex + 1); } public static string Replace(this string str, int startIndex, int endIndex, string newStr) { str = str.Remove(startIndex, endIndex - startIndex + 1); str = str.Insert(startIndex, newStr); return str; } public static string Between(this string str, int startIndex, int endIndex) { if (startIndex > endIndex) { return ""; } else if (startIndex == endIndex) { return str[startIndex].ToString(); } else { return str.Substring(startIndex, endIndex - startIndex + 1); } } #endregion #region Regist public static T AddScript(this Component comp) where T : Component { return AddScript(comp.gameObject); } public static T AddScript(this GameObject go) where T : Component { Component comp = go.AddComponent(typeof(T)); if (comp is Regist) { Regist regist = (Regist) comp; regist.enabled = false; regist.RegistImmed(); Initializer.RegistList.Add(regist); return (T) comp; } else { throw new Exception(); } } #endregion #region Active public static void SetActive(this Component comp, bool active) { comp.gameObject.SetActive(active); } #endregion #region Tween public static TweenSr TweenForSr(this Component comp) { return ManaAnim.TweenForSr(comp.transform); } public static TweenCG TweenForCG(this Component comp) { return ManaAnim.TweenForCG(comp.transform); } public static TweenVec TweenForVec(this Component comp) { return ManaAnim.TweenForVec(comp.transform); } public static TweenGra TweenForGra(this Component comp) { return ManaAnim.TweenForGra(comp.transform); } public static TweenFont TweenForFont(this Component comp) { return ManaAnim.TweenForFont(comp.transform); } public static TweenRect TweenForRect(this Component comp) { return ManaAnim.TweenForRect(comp.transform); } public static TweenScale TweenForScale(this Component comp) { return ManaAnim.TweenForScale(comp.transform); } public static TweenAudio TweenForAudio(this Component comp) { return ManaAnim.TweenForAudio(comp.transform); } public static TweenAudio TweenForAudio(this AudioSource audioSource) { return ManaAnim.TweenForAudio(audioSource); } public static TweenOutline TweenForOutline(this Component comp) { return ManaAnim.TweenForOutline(comp.transform); } public static TweenNumber TweenForNumber(this Component comp) { return ManaAnim.TweenForNumber(comp.transform); } public static TweenSr TweenBacSr(this Component comp) { return ManaAnim.TweenBacSr(comp.transform); } public static TweenCG TweenBacCG(this Component comp) { return ManaAnim.TweenBacCG(comp.transform); } public static TweenVec TweenBacVec(this Component comp) { return ManaAnim.TweenBacVec(comp.transform); } public static TweenGra TweenBacGra(this Component comp) { return ManaAnim.TweenBacGra(comp.transform); } public static TweenFont TweenBacFont(this Component comp) { return ManaAnim.TweenBacFont(comp.transform); } public static TweenRect TweenBacRect(this Component comp) { return ManaAnim.TweenBacRect(comp.transform); } public static TweenScale TweenBacScale(this Component comp) { return ManaAnim.TweenBacScale(comp.transform); } public static TweenAudio TweenBacAudio(this Component comp) { return ManaAnim.TweenBacAudio(comp.transform); } public static TweenAudio TweenBacAudio(this AudioSource audioSource) { return ManaAnim.TweenBacAudio(audioSource); } public static TweenOutline TweenBacOutline(this Component comp) { return ManaAnim.TweenBacOutline(comp.transform); } public static TweenNumber TweenBacNumber(this Component comp) { return ManaAnim.TweenBacNumber(comp.transform); } public static TweenSr TweenReForSr(this Component comp) { return ManaAnim.TweenReForSr(comp.transform); } public static TweenCG TweenReForCG(this Component comp) { return ManaAnim.TweenReForCG(comp.transform); } public static TweenVec TweenReForVec(this Component comp) { return ManaAnim.TweenReForVec(comp.transform); } public static TweenGra TweenReForGra(this Component comp) { return ManaAnim.TweenReForGra(comp.transform); } public static TweenFont TweenReForFont(this Component comp) { return ManaAnim.TweenReForFont(comp.transform); } public static TweenRect TweenReForRect(this Component comp) { return ManaAnim.TweenReForRect(comp.transform); } public static TweenScale TweenReForScale(this Component comp) { return ManaAnim.TweenReForScale(comp.transform); } public static TweenAudio TweenReForAudio(this Component comp) { return ManaAnim.TweenReForAudio(comp.transform); } public static TweenAudio TweenReForAudio(this AudioSource audioSource) { return ManaAnim.TweenReForAudio(audioSource); } public static TweenOutline TweenReForOutline(this Component comp) { return ManaAnim.TweenReForOutline(comp.transform); } public static TweenNumber TweenReForNumber(this Component comp) { return ManaAnim.TweenReForNumber(comp.transform); } public static TweenSr TweenReBacSr(this Component comp) { return ManaAnim.TweenReBacSr(comp.transform); } public static TweenCG TweenReBacCG(this Component comp) { return ManaAnim.TweenReBacCG(comp.transform); } public static TweenVec TweenReBacVec(this Component comp) { return ManaAnim.TweenReBacVec(comp.transform); } public static TweenGra TweenReBacGra(this Component comp) { return ManaAnim.TweenReBacGra(comp.transform); } public static TweenFont TweenReBacFont(this Component comp) { return ManaAnim.TweenReBacFont(comp.transform); } public static TweenRect TweenReBacRect(this Component comp) { return ManaAnim.TweenReBacRect(comp.transform); } public static TweenScale TweenReBacScale(this Component comp) { return ManaAnim.TweenReBacScale(comp.transform); } public static TweenAudio TweenReBacAudio(this Component comp) { return ManaAnim.TweenReBacAudio(comp.transform); } public static TweenAudio TweenReBacAudio(this AudioSource audioSource) { return ManaAnim.TweenReBacAudio(audioSource); } public static TweenOutline TweenReBacOutline(this Component comp) { return ManaAnim.TweenReBacOutline(comp.transform); } public static TweenNumber TweenReBacNumber(this Component comp) { return ManaAnim.TweenReBacNumber(comp.transform); } public static TweenSr GetTweenSr(this Component comp) { return ManaAnim.GetTweenSr(comp.transform); } public static TweenCG GetTweenCG(this Component comp) { return ManaAnim.GetTweenCG(comp.transform); } public static TweenVec GetTweenVec(this Component comp) { return ManaAnim.GetTweenVec(comp.transform); } public static TweenGra GetTweenGra(this Component comp) { return ManaAnim.GetTweenGra(comp.transform); } public static TweenFont GetTweenFont(this Component comp) { return ManaAnim.GetTweenFont(comp.transform); } public static TweenRect GetTweenRect(this Component comp) { return ManaAnim.GetTweenRect(comp.transform); } public static TweenScale GetTweenScale(this Component comp) { return ManaAnim.GetTweenScale(comp.transform); } public static TweenAudio GetTweenAudio(this Component comp) { return ManaAnim.GetTweenAudio(comp.transform); } public static TweenAudio GetTweenAudio(this AudioSource audioSource) { return ManaAnim.GetTweenAudio(audioSource); } public static TweenOutline GetTweenOutline(this Component comp) { return ManaAnim.GetTweenOutline(comp.transform); } public static TweenNumber GetTweenNumber(this Component comp) { return ManaAnim.GetTweenNumber(comp.transform); } public static TweenSr CreateTweenSr(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false) { return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, originActive, destActive, curve, cg, group); } public static TweenSr CreateTweenSr(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false) { return ManaAnim.CreateTweenSr(comp.transform, destination, duration, originActive, destActive, curve, cg, group); } public static TweenSr CreateTweenSr(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false) { return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, originActive, destActive, curve, cg, group); } public static TweenSr CreateTweenSr(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false) { return ManaAnim.CreateTweenSr(comp.transform, destination, duration, originActive, destActive, curve, cg, group); } public static TweenCG CreateTweenCG(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenCG(comp.transform, origin, destination, duration, originActive, destActive, curve); } public static TweenCG CreateTweenCG(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenCG(comp.transform, destination, duration, originActive, destActive, curve); } public static TweenGra CreateTweenGra(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenGra CreateTweenGra(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve, cg); } public static TweenGra CreateTweenGra(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenGra CreateTweenGra(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve, cg); } public static TweenVec CreateTweenVec2D(this Component comp, Vector3 origin, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenVec2D(comp.transform, origin, destination, duration, local, originActive, destActive, curve, cg); } public static TweenVec CreateTweenVec2D(this Component comp, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenVec2D(comp.transform, destination, duration, local, originActive, destActive, curve, cg); } public static TweenVec CreateTweenVec3D(this Component comp, Vector3 origin, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenVec3D(comp.transform, origin, destination, duration, local, originActive, destActive, curve, cg); } public static TweenVec CreateTweenVec3D(this Component comp, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenVec3D(comp.transform, destination, duration, local, originActive, destActive, curve, cg); } public static TweenVec CreateTweenVecOffset2D(this Component comp, Vector3 offset, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenVecOffset2D(comp.transform, offset, duration, local, originActive, destActive, curve, cg); } public static TweenVec CreateTweenVecOffset3D(this Component comp, Vector3 offset, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenVecOffset3D(comp.transform, offset, duration, local, originActive, destActive, curve, cg); } public static TweenFont CreateTweenFont(this Component comp, int origin, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenFont(comp.transform, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenFont CreateTweenFont(this Component comp, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenFont(comp.transform, destination, duration, originActive, destActive, curve, cg); } public static TweenRect CreateTweenRect(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenRect(comp.transform, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenRect CreateTweenRect(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenRect(comp.transform, destination, duration, originActive, destActive, curve, cg); } public static TweenScale CreateTweenScale(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenScale CreateTweenScale(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve, cg); } public static TweenScale CreateTweenScale(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenScale CreateTweenScale(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve, cg); } public static TweenAudio CreateTweenAudio(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenAudio(comp.transform, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenAudio CreateTweenAudio(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenAudio(comp.transform, destination, duration, originActive, destActive, curve, cg); } public static TweenAudio CreateTweenAudio(this AudioSource audioSource, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenAudio(audioSource, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenAudio CreateTweenAudio(this AudioSource audioSource, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenAudio(audioSource, destination, duration, originActive, destActive, curve, cg); } public static TweenOutline CreateTweenOutline(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenOutline(comp.transform, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenOutline CreateTweenOutline(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenOutline(comp.transform, destination, duration, originActive, destActive, curve, cg); } public static TweenOutline CreateTweenOutline(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenOutline(comp.transform, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenOutline CreateTweenOutline(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenOutline(comp.transform, destination, duration, originActive, destActive, curve, cg); } public static TweenNumber CreateTweenNumber(this Component comp, int origin, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenNumber(comp.transform, origin, destination, duration, originActive, destActive, curve, cg); } public static TweenNumber CreateTweenNumber(this Component comp, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) { return ManaAnim.CreateTweenNumber(comp.transform, destination, duration, originActive, destActive, curve, cg); } #endregion #region Stream public static StreamScale StreamForScale(this Component comp) { return ManaAnim.StreamForScale(comp.transform); } public static StreamScale GetStreamScale(this Component comp) { return ManaAnim.GetStreamScale(comp.transform); } public static StreamScale CreateStreamScale(this Component comp, List delayList, List durationList, List destKvList, bool originActive, bool destActive, Curve curve, bool cg = false, List actionList = null) { return ManaAnim.CreateStreamScale(comp.transform, delayList, durationList, destKvList, originActive, destActive, curve, cg, actionList); } public static StreamScale CreateStreamScale(this Component comp, List delayList, List durationList, List destList, bool originActive, bool destActive, Curve curve, bool cg = false, List actionList = null) { return ManaAnim.CreateStreamScale(comp.transform, delayList, durationList, destList, originActive, destActive, curve, cg, actionList); } #endregion #region Collider public static void SetCollider(this Component comp, bool active) { BoxCollider2D collider = comp.GetComponent(); if (collider) { collider.enabled = active; } } public static void SetCollider(this GameObject gameObject, bool active) { gameObject.GetComponent().enabled = active; } #endregion #region Material public static void SetAlpha(this Material material, float alpha, string propertyName) { Color color = material.GetColor(propertyName); color.a = alpha; material.SetColor(propertyName, color); } public static float GetAlpha(this Material material, string propertyName) { Color color = material.GetColor(propertyName); return color.a; } #endregion #region GetChild public static Transform GetChild(this Component comp, int index) { return comp.transform.GetChild(index); } public static Transform GetChild(this GameObject gameObject, int index) { return gameObject.transform.GetChild(index); } #endregion #region SetParent public static void SetParent(this Component comp, Transform par) { comp.transform.SetParent(par); } public static void SetParent(this GameObject go, Transform par) { go.transform.SetParent(par); } #endregion #region ScrollRect public static Move Locate(this ScrollRect scrollRect, int index, float duration, Curve curve, LocatePos locatePos) { LayoutGroup layoutGroup = scrollRect.content.GetComponent(); RectTransform tra1 = scrollRect.GetComponent(); Rect rect1 = tra1.rect; RectTransform tra2 = scrollRect.content.GetChild(index).GetComponent(); Rect rect2 = tra2.rect; if (locatePos == LocatePos.Up) { Vector3 itemPos = tra2.position + new Vector3(0, rect2.yMax, 0); Vector3 targetPos = tra1.position + new Vector3(0, rect1.yMax - layoutGroup.padding.top, 0); Vector3 offset = targetPos - itemPos; offset.x = 0; return scrollRect.content.MoveOffset2D(offset, duration, false, curve); } if (locatePos == LocatePos.Down) { Vector3 itemPos = tra2.position + new Vector3(0, rect2.yMin, 0); Vector3 targetPos = tra1.position + new Vector3(0, rect1.yMin + layoutGroup.padding.bottom, 0); Vector3 offset = targetPos - itemPos; offset.x = 0; return scrollRect.content.MoveOffset2D(offset, duration, false, curve); } else if (locatePos == LocatePos.Middle) { Vector3 itemPos = tra2.position + new Vector3(rect2.center.x, rect2.center.y, 0); Vector3 targetPos = tra1.position + new Vector3(rect1.center.x, rect1.center.y, 0); Vector3 offset = targetPos - itemPos; offset.x = 0; return scrollRect.content.MoveOffset2D(offset, duration, false, curve); } if (locatePos == LocatePos.Left) { Vector3 itemPos = tra2.position + new Vector3(rect2.xMin, 0, 0); Vector3 targetPos = tra1.position + new Vector3(rect1.xMin + layoutGroup.padding.left, 0, 0); Vector3 offset = targetPos - itemPos; offset.y = 0; return scrollRect.content.MoveOffset2D(offset, duration, false, curve); } if (locatePos == LocatePos.Right) { Vector3 itemPos = tra2.position + new Vector3(rect2.xMax, 0, 0); Vector3 targetPos = tra1.position + new Vector3(rect1.xMax - layoutGroup.padding.right, 0, 0); Vector3 offset = targetPos - itemPos; offset.y = 0; return scrollRect.content.MoveOffset2D(offset, duration, false, curve); } if (locatePos == LocatePos.Center) { Vector3 itemPos = tra2.position + new Vector3(rect2.center.x, rect2.center.y, 0); Vector3 targetPos = tra1.position + new Vector3(rect1.center.x, rect1.center.y, 0); Vector3 offset = targetPos - itemPos; offset.y = 0; return scrollRect.content.MoveOffset2D(offset, duration, false, curve); } throw new Exception(); } public static Move Locate(this ScrollRect scrollRect, Transform item, float duration, Curve curve, LocatePos locatePos) { return scrollRect.Locate(item.GetSiblingIndex(), duration, curve, locatePos); } #endregion #region Transform public static void SetX(this Transform tra, float x) { tra.position = new Vector3(x, tra.position.y, tra.position.z); } public static void SetY(this Transform tra, float y) { tra.position = new Vector3(tra.position.x, y, tra.position.z); } public static void SetZ(this Transform tra, float z) { tra.position = new Vector3(tra.position.x, tra.position.y, z); } public static void SetLX(this Transform tra, float x) { tra.localPosition = new Vector3(x, tra.localPosition.y, tra.localPosition.z); } public static void SetLY(this Transform tra, float y) { tra.localPosition = new Vector3(tra.localPosition.x, y, tra.localPosition.z); } public static void SetLZ(this Transform tra, float z) { tra.localPosition = new Vector3(tra.localPosition.x, tra.localPosition.y, z); } public static void SetEX(this Transform tra, float x) { tra.eulerAngles = new Vector3(x, tra.eulerAngles.y, tra.eulerAngles.z); } public static void SetEY(this Transform tra, float y) { tra.eulerAngles = new Vector3(tra.eulerAngles.x, y, tra.eulerAngles.z); } public static void SetEZ(this Transform tra, float z) { tra.eulerAngles = new Vector3(tra.eulerAngles.x, tra.eulerAngles.y, z); } public static Vector3 GetScale(this Transform tra) { Vector3 scale = tra.localScale; while (tra.parent != null) { float x = scale.x * tra.parent.localScale.x; float y = scale.y * tra.parent.localScale.y; float z = scale.z * tra.parent.localScale.z; scale = new Vector3(x, y, z); tra = tra.parent; } return scale; } #endregion #region Dictionary public static T2 Random(this Dictionary dic) { return dic.Values.ToList().Random(); } public static bool UniqueAdd(this Dictionary dic, T1 t1, T2 t2) { if (dic.ContainsKey(t1)) { return false; } else { dic.Add(t1, t2); return true; } } #endregion #region UnityAction public static void SafeInvoke(this UnityAction action) { if (action != null) { action.Invoke(); } } public static void SafeInvoke(this UnityAction action, T t) { if (action != null) { action.Invoke(t); } } #endregion #region AddComponent public static T AddComponent(this Component comp) where T : Component { return comp.transform.gameObject.AddComponent(); } #endregion }