unity学习——委托delegate的使用
来源:互联网 发布:淘宝怎么做手机上 编辑:程序博客网 时间:2024/06/14 12:54
初次接触c#的委托机制,感觉非常方便使用,下面是一个简单的委托使用的例子。
目的:实现按下键盘上左右键能使三个cube物体同时移动
1.首先创建一个unity场景(非常简单包括一个主相机,三个cube物体cube1,cube2,cube3)
2.建立两个C#脚本,一个名为SmartCube,另一个名为CubeManager
SmartCube的代码如下:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class SmartCube : MonoBehaviour { public float speed = 10; public void MoveHorizontal() { transform.Translate(Time.deltaTime*speed*Input.GetAxis("Horizontal"),0,0); }}
CubeManager代码如下:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class CubeManager : MonoBehaviour { delegate void CubeMoveHDelegate(); CubeMoveHDelegate hDelegate; // Use this for initialization void Start () { SmartCube[] cubes= FindObjectsOfType(typeof(SmartCube))as SmartCube[]; for (int i = 0; i < cubes.Length; i++) { SmartCube c = cubes[i]; hDelegate += c.MoveHorizontal; } } // Update is called once per frame void Update () { if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow)) hDelegate(); }}
3.将SmartCube脚本拖分别拖到三个cube物体上,将CubeManager拖到主相机上,调整好相机角度便于观察cube的移动。
阅读全文
0 0
- unity学习——委托delegate的使用
- Unity学习 - C#委托的介绍(delegate、Action、Func、predicate)
- Unity学习 - C#委托的介绍(delegate、Action、Func、predicate)
- delegate 委托的使用
- unity delegate事件委托
- IOS—delegate(委托)
- C# 委托(delegate) 的使用
- .NET delegate 委托的使用
- C# 委托(delegate)的使用
- 在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信
- unity学习——委托(带参数方法的使用)
- Unity-3D 委托Delegate
- C#学习笔记之——委托(delegate)
- 使用委托——delegate进行参数传递
- Unity中用到的C#补充(六)- Delegate委托
- 委托控件的使用(Delegate Control)
- C# EventHandler and Delegate(委托的使用)
- C# EventHandler and Delegate(委托的使用)
- 图解Java多线程设计模式
- 专访阿里云异构计算负责人:异构计算,GPU、FPGA、ASIC芯片将三分天下
- 第七次面试
- GD库 生成缩略图、文字水印、图片水印的函数封装
- SO_REUSEADDR在Linux和Windows的区别SO_LINGER的作用
- unity学习——委托delegate的使用
- Spring事务管理(详解+实例)
- iOS UIViewController 和 xib 绑定 详解
- Linux学习
- Referenced file contains errors (http://struts.apache.org/dtds/struts-2.0.dtd). For more information
- 操作系统概论(2)
- 服务器架构
- js时间转换 将获取的时间转换标准时间 像12:20:36形式
- javascript随笔