zoukankan      html  css  js  c++  java
  • Extension Methods

    Oftentimes you’ll find yourself using classes you can’t modify. Whether they’re basic data types or part of an existing framework, you’re stuck with the functions that are provided. That being said, C# provides a nifty trick to appending functions to classes! These are known as Extension Methods.

    Extension methods are fairly simple to create and are frequently used as syntactic sugar. A practical example can be seen with Unity’s Transform class. Let’s say you want to set only the x variable of Transform.position.

    using UnityEngine;
    using System.Collections;
     
    public class Player : MonoBehaviour
    {
        void Update ()
        {
            //Set new x position to 5
            transform.position = new Vector3(5f, transform.position.y, transform.position.z);
        }
    }

    In this case Transform.position gives you an error if you only try to assign its x member variable, so you have to assign the entire Vector3. An extension method such as SetPositionX() could be appended to the Transform class and help make this code more readable.

    In order to create extension methods you have to create a static class. In addition, an extension method declaration must be declared static and have the first parameter be of the type that you’re writing the method for, preceded by the this keyword.

    using UnityEngine;
    using System.Collections;
     
    //Must be in a static class
    public static class Extensions
    {
        //Function must be static
        //First parameter has "this" in front of type
        public static void SetPositionX(this Transform t, float newX)
        {
            t.position = new Vector3(newX, t.position.y, t.position.z);
        }
    }

    Now you can go back to your other script and replace our old code with the new extension method.

    using UnityEngine;
    using System.Collections;
     
    public class Player : MonoBehaviour
    {
        void Update ()
        {
            //Set new x position to 5
            transform.SetPositionX(5f);
        }
    }
    NOTE: Extension methods can only be called on an instance of a class, not on the class itself.

    Here are a few more extension methods to get you started, as well as an example script that utilizes a few of them.

    Extensions:

    using UnityEngine;
    using System.Collections;
     
    public static class Extensions
    {
        public static void SetPositionX(this Transform t, float newX)
        {
            t.position = new Vector3(newX, t.position.y, t.position.z);
        }
     
        public static void SetPositionY(this Transform t, float newY)
        {
            t.position = new Vector3(t.position.x, newY, t.position.z);
        }
     
        public static void SetPositionZ(this Transform t, float newZ)
        {
            t.position = new Vector3(t.position.x, t.position.y, newZ);
        }
     
        public static float GetPositionX(this Transform t)
        {
            return t.position.x;
        }
     
        public static float GetPositionY(this Transform t)
        {
            return t.position.y;
        }
     
        public static float GetPositionZ(this Transform t)
        {
            return t.position.z;
        }
     
        public static bool HasRigidbody(this GameObject gobj)
        {
            return (gobj.rigidbody != null);
        }
     
        public static bool HasAnimation(this GameObject gobj)
        {
            return (gobj.animation != null);
        }
     
        public static void SetSpeed(this Animation anim, float newSpeed)
        {
            anim[anim.clip.name].speed = newSpeed;
        }
    }

    Example Script:

    using UnityEngine;
    using System.Collections;
     
    public class Player : MonoBehaviour
    {
        void Update ()
        {
            //move x position 5 units
            float currentX = transform.GetPositionX();
            transform.SetPositionX(currentX + 5f);
     
            if(gameObject.HasRigidbody())
            {
                //Do something with physics!
            }
     
            if(gameObject.HasAnimation())
            {
                //Double the animation speed!
                gameObject.animation.SetSpeed(2f);
            }
        }
    }
    NOTE: Static classes do NOT extend MonoBehaviour.
    ANOTHER NOTE: If you define your extension methods inside of a namespace, you have to declare the use of that namespace in order to bring them into scope.
     
  • 相关阅读:
    mysql update与select结合修改表数据
    编译与安装 OpenSSL
    MQTT的学习之Mosquitto安装&使用(1)
    Spring Cloud构建微服务架构(三)消息总线
    Spring Cloud构建微服务架构(一)服务注册与发现
    Spring Cloud构建微服务架构(二)分布式配置中心
    【行为型模式】《大话设计模式》——读后感 (16)加薪非要老板批?——职责链模式
    【行为型模式】《大话设计模式》——读后感 (15)烤羊肉串引来的思考?——命令模式
    【结构型模式】《大话设计模式》——读后感 (14)分公司=一部门?——组合模式
    【结构型模式】《大话设计模式》——读后感 (13)手机软件何时能统一?——桥接模式
  • 原文地址:https://www.cnblogs.com/chongxin/p/4025397.html
Copyright © 2011-2022 走看看