写更少代码的需求

当我们重复写一些繁杂的代码,或C#的一些方法,我们就想能不能有更便捷的方法呢?当然在unity中,我们对它进行扩展。

对unity的类或C#的类进行扩展有以下两点要注意:

1、这个类必须声明为static,扩展的方法也必须要声明为static

2、在使用时,就可以直接调用扩展的方法

扩展Unity的属性

Demo

using UnityEngine;
using System.Collections; //It is common to create a class to contain all of your
//extension methods. This class must be static.
public static class ExtensionMethods
{
//Even though they are used like normal methods, extension
//methods must be declared static. Notice that the first
//parameter has the 'this' keyword followed by a Transform
//variable. This variable denotes which class the extension
//method becomes a part of.
public static void ResetTransformation(this Transform trans)
{
trans.position = Vector3.zero;
trans.localRotation = Quaternion.identity;
trans.localScale = new Vector3(1, 1, 1);
}
}

使用方法

using UnityEngine;
using System.Collections; public class SomeClass : MonoBehaviour
{
void Start () {
//Notice how you pass no parameter into this
//extension method even though you had one in the
//method declaration. The transform object that
//this method is called from automatically gets
//passed in as the first parameter.
transform.ResetTransformation();
}
}

扩展C#的方法

为C#的集合扩展一个方法,当在调用时,就可以直接调用CFirstOrDefault

public static T CFirstOrDefault<T>(this IEnumerable<T> source)
{
if (source != null)
{
foreach (T item in source)
{
return item;
}
} return default(T);
}

文档资料

文档:http://unity3d.com/learn/tutorials/modules/intermediate/scripting/extension-methods

扩展Unity的方法的更多相关文章

  1. PHP打开PDO_MySQL扩展的配置方法

    PHP中的PDO其实是一个很好用的扩展,在一些PHPCMS系统中,开发者大多都有用到,那么如何开启PDO和PDO_MySQL扩展呢?方法同样很简单: 打开php.ini配置文件,找到extension ...

  2. 谈谈CListCtrl 扩展风格设置方法-SetExtendedStyle和ModifyStyleEx 比較

    谈谈CListCtrl 扩展风格设置方法 --------------------------------------SetExtendedStyle和ModifyStyleEx 比較 对于刚開始学习 ...

  3. python获取文件扩展名的方法(转)

    主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧.具体实现方法如下: 1 2 3 4 import os.path def file_extension(path ...

  4. C#开发Unity游戏教程之Unity中方法的参数

    C#开发Unity游戏教程之Unity中方法的参数 Unity的方法的参数 出现在脚本中的方法,无论是在定义的时候,还是使用的时候,后面都跟着一对括号“( )”,有意义吗?看起来最多也就是起个快速识别 ...

  5. python获取文件扩展名的方法

    主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧 import os.path def file_extension(path): ] print file_ex ...

  6. C#获取并修改文件扩展名的方法

    本文实例讲述了C#获取并修改文件扩展名的方法.分享给大家供大家参考.具体分析如下: 这里使用C#编程的方法改变文件扩展名的文件,必须使用Path类. Path类用来解析文件系统路径的各个部分.静态方法 ...

  7. Kotlin(2): 优雅地扩展类的方法和属性

    欢迎Follow我的GitHub, 关注我的CSDN. 个人博客: http://www.wangchenlong.org/, 最新内容. Kotlin由JetBrains公司推出, 是兼容Java的 ...

  8. 无需重新编译php加入ftp扩展的解决方法

    无需重新编译php加入ftp扩展的解决方法   本文为大家介绍无需重新编译php加入ftp扩展的方法,有需要的朋友可以参考下   首先,进入源码目录cd php-5.2.13/ext/ftp #运行p ...

  9. Ubuntu 16.04 安装opencv的各种方法(含opencv contrib扩展包安装方法)

    Ubuntu 16.04 安装opencv的各种方法(含opencv contrib扩展包安装方法) https://blog.csdn.net/ksws0292756/article/details ...

随机推荐

  1. 简单两句话解释下prototype和__proto__

    先上两句代码: var Person = function () {}; var p = new Person(); 把new的过程拆分成以下三步: <1> var p={}; 也就是说, ...

  2. sass高级语法

    github地址:https://github.com/lily1010/sass/tree/master/course03 用到的sass语法是: sass --watch test.scss:te ...

  3. ArcGIS Server For Linux 10.2.2安装

    1.#  yum install Xvfb#  yum groupinstall "X Window System"# yum install gettext 2./usr/sbi ...

  4. 关于SharePoint 2013 UserProfile跨场的几点注意

    1.跨场中需要以下几个Service实例,没有这几个会遇到各种问题 2.发布场和消费场同时需要创建Host站点,否则消费场的SiteFeed无法使用. 3.跨场关注的问题请参考:http://www. ...

  5. 如何处理 android 方法总数超过 65536 . the number of method references in a .dex file exceed 64k

    一:问题描述:     应用中的Dex 文件方法数超过了最大值65536的上限,简单来说,应用爆棚了. 二.解决方案:      方案1:使用插件化框架  比如: https://github.com ...

  6. IntentService源码分析

    和HandlerThread一样,IntentService也是Android替我们封装的一个Helper类,用来简化开发流程的.接下来分析源码的时候 你就明白是怎么回事了.IntentService ...

  7. Sql server profiler抓出的语句不可信

      Sql profiler抓出的语句不可信      

  8. 生成uuid

    function guid(){ if (function_exists('com_create_guid')){ return com_create_guid(); }else{ mt_srand( ...

  9. IrfanView 4.36 中文版发布了

    IrfanView 4.36 简体中文便携版 小而快的图片浏览器 仅仅不到2M的小软件,功能却能与体积大到几十M的ACDSee相媲美!这个软件就是IrfanView.图片.音频.视频浏览,图片批量格式 ...

  10. post请求报文

    POST /02_WEB_HTTP/index.html HTTP/1.1 Accept: application/x-ms-application, image/jpeg, application/ ...