扩展类

作者: APP4x | 来源:发表于2020-01-02 22:55 被阅读0次

扩展类是一种静态的一种类的调用方法,通过实例化进行调用。
利用this进行指正该类,有参数的时候直接在后面追加参数。

在游戏内可以预定义一些常用的扩展类:
1.Color

public static class ColorExtension
{
    public static Color HtmlStringToColor(this string htmlString)
    {
        Color retColor;
        var parseSucceed = ColorUtility.TryParseHtmlString(htmlString, out retColor);
        return parseSucceed ? retColor : Color.black;
    }
}

2.GameObject

public static class GameObjectExtension
    {
        public static GameObject SetActiveGracefully(this GameObject selfObj, bool value)
        {
            if (selfObj.activeSelf != value)
                selfObj.SetActive(value);
            return selfObj;
        }
        public static T SetActiveGracefully<T>(this T selfComponent, bool value) where T : Component
        {
            if (selfComponent.gameObject.activeSelf != value)
                selfComponent.gameObject.SetActive(value);
            return selfComponent;
        }
        public static void DestroyGameObj<T>(this T selfBehaviour) where T : Component
        {
            if (selfBehaviour.gameObject)
                Object.Destroy(selfBehaviour.gameObject);
        }
        public static T DestroyGameObjDelay<T>(this T selfBehaviour, float delay) where T : Component
        {
            if (selfBehaviour.gameObject)
                Object.Destroy(selfBehaviour.gameObject, delay);
            return selfBehaviour;
        }
        public static T EnsureComponent<T>(this GameObject selfObj) where T : Component
        {
            var comp = selfObj.GetComponent<T>();
            return comp ? comp : selfObj.AddComponent<T>();
        }
        public static Component EnsureComponent(this GameObject selfObj, Type type)
        {
            var comp = selfObj.GetComponent(type);
            return comp ? comp : selfObj.AddComponent(type);
        }
    }

3.UI

public static class UIExtension
{
    public static T ColorAlpha<T>(this T selfGraphic, float alpha) where T : Graphic
    {
        var color = selfGraphic.color;
        color.a = alpha;
        selfGraphic.color = color;
        return selfGraphic;
    }
    public static Image FillAmount(this Image selfImage, float fillamount)
    {
        selfImage.fillAmount = fillamount;
        return selfImage;
    }
    public static void AddOnValueChangedEvent(this Toggle selfToggle, UnityAction<bool> onValueChangedEvent)
    {
        selfToggle.onValueChanged.AddListener(onValueChangedEvent);
    }
    public static void AddButtonEvent(this Button btn, UnityAction onClicked)
    {
        if (btn == null)
        {
            btn = btn.gameObject.GetComponent<Button>();
        }
        btn.onClick.AddListener(() => { onClicked(); });
    }
    public static void AddButtonEvent(this Button btn, UnityAction<GameObject> onClicked)
    {
        btn.onClick.AddListener(() => { onClicked(btn.gameObject); });
    }
}

4.Transform

public static class TransformExtension
    {
        static Vector3 localPos;
        private static Vector3 mScale;
        private static Vector3 mPos;

        public static T Parent<T>(this T selfComponent, Component parentComponent) where T : Component
        {
            selfComponent.transform.SetParent(parentComponent == null ? null : parentComponent.transform);
            return selfComponent;
        }
        public static T AsRootTransform<T>(this T selfComponent) where T : Component
        {
            selfComponent.transform.SetParent(null);
            return selfComponent;
        }
        public static T Identity<T>(this T selfComponent) where T : Component
        {
            selfComponent.transform.position = Vector3.zero;
            selfComponent.transform.rotation = Quaternion.identity;
            selfComponent.transform.localScale = Vector3.one;
            return selfComponent;
        }
        public static T LocalIdentity<T>(this T selfComponent) where T : Component
        {
            selfComponent.transform.localPosition = Vector3.zero;
            selfComponent.transform.localRotation = Quaternion.identity;
            selfComponent.transform.localScale = Vector3.one;
            return selfComponent;
        }
        public static T LocalPosition<T>(this T selfComponent, Vector3 localPos) where T : Component
        {
            selfComponent.transform.localPosition = localPos;
            return selfComponent;
        }
        public static T LocalPosition<T>(this T selfComponent, float x, float y, float z) where T : Component
        {
            selfComponent.transform.localPosition = new Vector3(x, y, z);
            return selfComponent;
        }
        public static T LocalPosition<T>(this T selfComponent, float x, float y) where T : Component
        {
            localPos = selfComponent.transform.localPosition;
            localPos.x = x;
            localPos.y = y;
            selfComponent.transform.localPosition = localPos;
            return selfComponent;
        }
        public static T LocalPositionX<T>(this T selfComponent, float x) where T : Component
        {
            localPos = selfComponent.transform.localPosition;
            localPos.x = x;
            selfComponent.transform.localPosition = localPos;
            return selfComponent;
        }
        public static T LocalPositionY<T>(this T selfComponent, float y) where T : Component
        {
            localPos = selfComponent.transform.localPosition;
            localPos.y = y;
            selfComponent.transform.localPosition = localPos;
            return selfComponent;
        }
        public static T LocalPositionZ<T>(this T selfComponent, float z) where T : Component
        {
            localPos = selfComponent.transform.localPosition;
            localPos.z = z;
            selfComponent.transform.localPosition = localPos;
            return selfComponent;
        }
        public static T LocalPositionIdentity<T>(this T selfComponent) where T : Component
        {
            selfComponent.transform.localPosition = Vector3.zero;
            return selfComponent;
        }
        public static Vector3 GetLocalPosition<T>(this T selfComponent) where T : Component
        {
            return selfComponent.transform.localPosition;
        }
        public static T LocalRotation<T>(this T selfComponent, Quaternion localRotation) where T : Component
        {
            selfComponent.transform.localRotation = localRotation;
            return selfComponent;
        }
        public static T LocalRotationIdentity<T>(this T selfComponent) where T : Component
        {
            selfComponent.transform.localRotation = Quaternion.identity;
            return selfComponent;
        }
        public static Quaternion GetLocalRotation<T>(this T selfComponent) where T : Component
        {
            return selfComponent.transform.localRotation;
        }
        public static T LocalScale<T>(this T selfComponent, Vector3 scale) where T : Component
        {
            selfComponent.transform.localScale = scale;
            return selfComponent;
        }
        public static T LocalScale<T>(this T selfComponent, float xyz) where T : Component
        {
            selfComponent.transform.localScale = Vector3.one * xyz;
            return selfComponent;
        }
        public static T LocalScale<T>(this T selfComponent, float x, float y, float z) where T : Component
        {
            mScale = selfComponent.transform.localScale;
            mScale.x = x;
            mScale.y = y;
            mScale.z = z;
            selfComponent.transform.localScale = mScale;
            return selfComponent;
        }
        public static T LocalScale<T>(this T selfComponent, float x, float y) where T : Component
        {
            mScale = selfComponent.transform.localScale;
            mScale.x = x;
            mScale.y = y;
            selfComponent.transform.localScale = mScale;
            return selfComponent;
        }
        public static T LocalScaleX<T>(this T selfComponent, float x) where T : Component
        {
            mScale = selfComponent.transform.localScale;
            mScale.x = x;
            selfComponent.transform.localScale = mScale;
            return selfComponent;
        }
        public static T LocalScaleY<T>(this T selfComponent, float y) where T : Component
        {
            mScale = selfComponent.transform.localScale;
            mScale.y = y;
            selfComponent.transform.localScale = mScale;
            return selfComponent;
        }
        public static T LocalScaleZ<T>(this T selfComponent, float z) where T : Component
        {
            mScale = selfComponent.transform.localScale;
            mScale.z = z;
            selfComponent.transform.localScale = mScale;
            return selfComponent;
        }
        public static T LocalScaleIdentity<T>(this T selfComponent) where T : Component
        {
            selfComponent.transform.localScale = Vector3.one;
            return selfComponent;
        }
        public static Vector3 GetLocalScale<T>(this T selfComponent) where T : Component
        {
            return selfComponent.transform.localScale;
        }
        public static T Position<T>(this T selfComponent, Vector3 position) where T : Component
        {
            selfComponent.transform.position = position;
            return selfComponent;
        }
        public static T Position<T>(this T selfComponent, float x, float y, float z) where T : Component
        {
            selfComponent.transform.position = new Vector3(x, y, z);
            return selfComponent;
        }
        public static T Position<T>(this T selfComponent, float x, float y) where T : Component
        {
            mPos = selfComponent.transform.position;
            mPos.x = x;
            mPos.y = y;
            selfComponent.transform.position = mPos;
            return selfComponent;
        }
        public static T PositionX<T>(this T selfComponent, float x) where T : Component
        {
            mPos = selfComponent.transform.position;
            mPos.x = x;
            selfComponent.transform.position = mPos;
            return selfComponent;
        }
        public static T PositionX<T>(this T selfComponent, Func<float, float> xSetter) where T : Component
        {
            mPos = selfComponent.transform.position;
            mPos.x = xSetter(mPos.x);
            selfComponent.transform.position = mPos;
            return selfComponent;
        }
        public static T PositionY<T>(this T selfComponent, float y) where T : Component
        {
            mPos = selfComponent.transform.position;
            mPos.y = y;
            selfComponent.transform.position = mPos;
            return selfComponent;
        }
        public static T PositionY<T>(this T selfComponent, Func<float, float> ySetter) where T : Component
        {
            mPos = selfComponent.transform.position;
            mPos.y = ySetter(mPos.y);
            selfComponent.transform.position = mPos;
            return selfComponent;
        }
        public static T PositionZ<T>(this T selfComponent, float z) where T : Component
        {
            mPos = selfComponent.transform.position;
            mPos.z = z;
            selfComponent.transform.position = mPos;
            return selfComponent;
        }
        public static T PositionZ<T>(this T selfComponent, Func<float, float> zSetter) where T : Component
        {
            mPos = selfComponent.transform.position;
            mPos.z = zSetter(mPos.z);
            selfComponent.transform.position = mPos;
            return selfComponent;
        }
        public static T PositionIdentity<T>(this T selfComponent) where T : Component
        {
            selfComponent.transform.position = Vector3.zero;
            return selfComponent;
        }
        public static Vector3 GetPosition<T>(this T selfComponent) where T : Component
        {
            return selfComponent.transform.position;
        }
        public static T Rotation<T>(this T selfComponent, Quaternion rotation) where T : Component
        {
            selfComponent.transform.rotation = rotation;
            return selfComponent;
        }
        public static T RotationIdentity<T>(this T selfComponent) where T : Component
        {
            selfComponent.transform.rotation = Quaternion.identity;
            return selfComponent;
        }
        public static Quaternion GetRotation<T>(this T selfComponent) where T : Component
        {
            return selfComponent.transform.rotation;
        }
        public static Vector3 GetGlobalScale<T>(this T selfComponent) where T : Component
        {
            return selfComponent.transform.lossyScale;
        }
        public static Vector3 GetScale<T>(this T selfComponent) where T : Component
        {
            return selfComponent.transform.lossyScale;
        }
        public static Vector3 GetWorldScale<T>(this T selfComponent) where T : Component
        {
            return selfComponent.transform.lossyScale;
        }
        public static Vector3 GetLossyScale<T>(this T selfComponent) where T : Component
        {
            return selfComponent.transform.lossyScale;
        }
        public static T AsLastSibling<T>(this T selfComponent) where T : Component
        {
            selfComponent.transform.SetAsLastSibling();
            return selfComponent;
        }
        public static T AsFirstSibling<T>(this T selfComponent) where T : Component
        {
            selfComponent.transform.SetAsFirstSibling();
            return selfComponent;
        }
        public static T SiblingIndex<T>(this T selfComponent, int index) where T : Component
        {
            selfComponent.transform.SetSiblingIndex(index);
            return selfComponent;
        }
        public static Transform FindChildRecursion(this Transform tfParent, string name, StringComparison stringComparison = StringComparison.Ordinal)
        {
            if (tfParent.name.Equals(name, stringComparison))
            {
                return tfParent;
            }
            foreach (Transform tfChild in tfParent)
            {
                Transform tfFinal = null;
                tfFinal = tfChild.FindChildRecursion(name, stringComparison);
                if (tfFinal)
                {
                    return tfFinal;
                }
            }
            return null;
        }
        public static Transform FindChildRecursion(this Transform tfParent, Func<Transform, bool> predicate)
        {
            if (predicate(tfParent))
            {
                return tfParent;
            }
            foreach (Transform tfChild in tfParent)
            {
                Transform tfFinal = null;
                tfFinal = tfChild.FindChildRecursion(predicate);
                if (tfFinal)
                {
                    return tfFinal;
                }
            }
            return null;
        }
        public static string GetPath(this Transform transform)
        {
            var sb = new System.Text.StringBuilder();
            var t = transform;
            while (true)
            {
                sb.Insert(0, t.name);
                t = t.parent;
                if (t)
                {
                    sb.Insert(0, "/");
                }
                else
                {
                    return sb.ToString();
                }
            }
        }
    }

相关文章

  • (SPI)3.dubbo spi代码分析

    看三个方法,静态扩展类,adapter扩展类,activate扩展类。先看getExtension 获取实现类对象...

  • JQuery插件

    类扩展: 对象扩展:

  • Kotlin-面向对象-进阶

    扩展 扩展方法 Kotlin支持扩展方法和扩展属性。语法:被扩展的类/接口名.方法名() 父类不能使用子类的扩展方...

  • 类扩展

    类扩展 (Class Extension也有人称为匿名分类) 作用:能为某个类附加额外的属性,成员变量,方法声明一...

  • 类扩展

    Method Swizzling是改变一个selector的实际实现的技术。通过这一技术,我们可以在运行时通过修改...

  • 类扩展

    1.类扩展:为某一个类扩充私有成员变量和方法,写在.m文件中, 英文名Class Estension 格式:@in...

  • 扩展类

    扩展类是一种静态的一种类的调用方法,通过实例化进行调用。利用this进行指正该类,有参数的时候直接在后面追加参数。...

  • 22章:类扩展

    类扩展 .h implemention 涉及实现细节的属性和方法,才在类扩展中声明。类扩展是一组私有的声明。只有类...

  • 每天学一点 Kotlin -- 类的进阶:扩展

    1. 扩展 1.1 类的扩展是给类增加新的方法或属性。 2. 扩展类的方法 1.2 扩展的语法:和定义函数差不多,...

  • 小码哥底层原理笔记:Catgory的本质

    首先我们要明白什么是类扩展class extension?像下面这样就是类扩展 类扩展和分类Catgory一样都是...

网友评论

      本文标题:扩展类

      本文链接:https://www.haomeiwen.com/subject/vxsaoctx.html