扩展Unity的方法
写更少代码的需求
当我们重复写一些繁杂的代码,或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的方法的更多相关文章
- PHP打开PDO_MySQL扩展的配置方法
PHP中的PDO其实是一个很好用的扩展,在一些PHPCMS系统中,开发者大多都有用到,那么如何开启PDO和PDO_MySQL扩展呢?方法同样很简单: 打开php.ini配置文件,找到extension ...
- 谈谈CListCtrl 扩展风格设置方法-SetExtendedStyle和ModifyStyleEx 比較
谈谈CListCtrl 扩展风格设置方法 --------------------------------------SetExtendedStyle和ModifyStyleEx 比較 对于刚開始学习 ...
- python获取文件扩展名的方法(转)
主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧.具体实现方法如下: 1 2 3 4 import os.path def file_extension(path ...
- C#开发Unity游戏教程之Unity中方法的参数
C#开发Unity游戏教程之Unity中方法的参数 Unity的方法的参数 出现在脚本中的方法,无论是在定义的时候,还是使用的时候,后面都跟着一对括号“( )”,有意义吗?看起来最多也就是起个快速识别 ...
- python获取文件扩展名的方法
主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧 import os.path def file_extension(path): ] print file_ex ...
- C#获取并修改文件扩展名的方法
本文实例讲述了C#获取并修改文件扩展名的方法.分享给大家供大家参考.具体分析如下: 这里使用C#编程的方法改变文件扩展名的文件,必须使用Path类. Path类用来解析文件系统路径的各个部分.静态方法 ...
- Kotlin(2): 优雅地扩展类的方法和属性
欢迎Follow我的GitHub, 关注我的CSDN. 个人博客: http://www.wangchenlong.org/, 最新内容. Kotlin由JetBrains公司推出, 是兼容Java的 ...
- 无需重新编译php加入ftp扩展的解决方法
无需重新编译php加入ftp扩展的解决方法 本文为大家介绍无需重新编译php加入ftp扩展的方法,有需要的朋友可以参考下 首先,进入源码目录cd php-5.2.13/ext/ftp #运行p ...
- Ubuntu 16.04 安装opencv的各种方法(含opencv contrib扩展包安装方法)
Ubuntu 16.04 安装opencv的各种方法(含opencv contrib扩展包安装方法) https://blog.csdn.net/ksws0292756/article/details ...
随机推荐
- 1:时间戳转换成年月日函数,2:url截取参数方法,3:弹窗自定义方法 4:点击按钮加入购物车
最近一直在使用vue.js来构建项目,先分享一下一些简单可复用的函数. 1:时间戳转换Date.prototype.format = function(fmt){ //author: yumeiqia ...
- 从客户端(?)中检测到有潜在危险的 Request.Path 值 的解决方案
public ActionResult A(string title) { return Redirect("B"+((String.IsNullOrEmpty(title))?& ...
- Wix安装程序中判断是否安装的.net framwork 4.5
<PropertyRef Id="NETFRAMEWORK40FULL"/> <PropertyRef Id="NETFRAMEWORK45" ...
- Android下创建一个SQLite数据库
数据库:SQLite(轻量级,嵌入式的数据库) 大量的相似结构的数据的储存,快速的查询.特殊的文件(按照一定的格式生成) 数据库的创建 创建文件 1.声明文件对象,文件是不会被创建出来的. File ...
- 利用hexo搭建博客
利用Hexo搭建博客 以前用Octopress搭过博客,折腾了好久才弄出来,当时看到那巨难看的默认主题,繁琐的操作,一点写东西的欲望都没了. 一次逛微博,看见了Hexo.尝试了一下,真的很好用哦. 下 ...
- mac PHP配置
apache默认路径配置方法 apache的配置 apache已经自带了,只需如下三个命令就可以了. 开启apache服务 sudo apachectl start 停止apache服务 sudo ...
- 禁止uiscrollview垂直方向滚动,只允许水平方向滚动;或只允许垂直方向滚动
禁止UIScrollView垂直方向滚动,只允许水平方向滚动 scrollview.contentSize = CGSizeMake(你要的长度, 0); 禁止UIScrollView水平方向滚动, ...
- python 获取星期几
In [17]: now.strftime(%a),now.strftime(%w) Out[17]: ('Mon', '1') Directive Meaning %a Weekday name. ...
- asp.net mvc 应用Bundle(捆绑和微小)压缩技术 启用 BundleConfig 配置web.config
从MVC4开始,我们就发现,项目中对Global.asax进行了优化,将原来在MVC3中使用的代码移到了 [App_Start]文件夹下,而Global.asax只负责初始化.其中的BundleCon ...
- visual studio生成后调试启动又提示部分项目需要生成问题总结
长久以来若干个项目都遇到过类似的情形,已经成功生成的项目启动调试或者再生成依然认为部分项目需要生成而不是跳过.总结以往的经验,记录下来,以便大家遇到时处理. 若有多个项目提示需要重新生成,优先检查被依 ...