using UnityEngine; using UnityEngine.UI; using System; using System.Collections; using System.Collections.Generic; public static class Extension { #region List public static T Last(this List list, int index) { return list[list.Count - 1 - 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 Scale 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 Move public static void MoveVec(this Component comp, Vector3 destination, float duration, Curve curve) { ManaAnim.MoveVec(comp.transform, destination, duration, curve); } public static MoveVec GetMoveVec(this Component comp) { return ManaAnim.GetMoveVec(comp.transform); } public static MoveVec CreateMoveVec(this Component comp) { return ManaAnim.CreateMoveVec(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 Sprite public static Sprite GetSprite(this Component comp) { return comp.GetComponent().sprite; } #endregion #region Regist public static void AddScript(this Component comp) { AddScript(comp.gameObject); } public static void AddScript(this GameObject go) { Component comp = go.AddComponent(typeof(T)); if (comp is Regist) { Initializer.RegistList.Add((Regist) comp); } } #endregion #region String 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 Active public static void SetActive(this Component comp, bool active) { comp.gameObject.SetActive(active); } #endregion #region Tween public static void TweenForCG(this Component comp) { ManaAnim.TweenForCG(comp.transform); } public static void TweenForVec(this Component comp) { ManaAnim.TweenForVec(comp.transform); } public static void TweenForGra(this Component comp) { ManaAnim.TweenForGra(comp.transform); } public static void TweenForRect(this Component comp) { ManaAnim.TweenForRect(comp.transform); } public static void TweenForScale(this Component comp) { ManaAnim.TweenForScale(comp.transform); } public static void TweenForAudio(this Component comp) { ManaAnim.TweenForAudio(comp.transform); } public static void TweenBacCG(this Component comp) { ManaAnim.TweenBacCG(comp.transform); } public static void TweenBacVec(this Component comp) { ManaAnim.TweenBacVec(comp.transform); } public static void TweenBacGra(this Component comp) { ManaAnim.TweenBacGra(comp.transform); } public static void TweenBacRect(this Component comp) { ManaAnim.TweenBacRect(comp.transform); } public static void TweenBacScale(this Component comp) { ManaAnim.TweenBacScale(comp.transform); } public static void TweenBacAudio(this Component comp) { ManaAnim.TweenBacAudio(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 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 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, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve); } public static TweenGra CreateTweenGra(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve); } public static TweenVec CreateTweenVec(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenVec(comp.transform, origin, destination, duration, originActive, destActive, curve); } public static TweenVec CreateTweenVec(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenVec(comp.transform, destination, duration, originActive, destActive, curve); } public static TweenRect CreateTweenRect(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenRect(comp.transform, origin, destination, duration, originActive, destActive, curve); } public static TweenRect CreateTweenRect(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenRect(comp.transform, destination, duration, originActive, destActive, curve); } public static TweenScale CreateTweenScale(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve); } public static TweenScale CreateTweenScale(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve); } public static TweenAudio CreateTweenAudio(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenAudio(comp.transform, origin, destination, duration, originActive, destActive, curve); } public static TweenAudio CreateTweenAudio(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve) { return ManaAnim.CreateTweenAudio(comp.transform, destination, duration, originActive, destActive, curve); } #endregion #region Vector3 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); } #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 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 AddComponent public static T AddComponent(this Component comp) where T : Component { return comp.transform.gameObject.AddComponent(); } #endregion }