UNITY之SendMassage,Corouting
来源:互联网 发布:poi的软件 编辑:程序博客网 时间:2024/06/03 22:44
using System.Collections;
public class SengMsg : MonoBehaviour {
public GameObject obj;
// Use this for initialization
void Start () {
// obj.SendMessage ("DisplayInfo");//不支持重载,没错,但只会找一个函数,重载的函数调用不到
// obj.SendMessage ("DisplayInfo",2);//即只会调用obj里的DisplayInfo()函数,DisplayInfo(int n)调用不到
//obj向下广播信息,向子类广播信息
//obj.BroadcastMessage ("DisplayInfo");
//obj向上广播信息,向父类广播信息
obj.SendMessageUpwards ("DisplayInfo");
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class Receive : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void DisplayInfo(){
print (this.gameObject.name+" : Receive diao yong");
}
public void DisplayInfo(int n){
print ("Receive diao yong n:"+n);
}
}
using UnityEngine;
using System.Collections;
public class CoroutingTest : MonoBehaviour {
//协程是指开启一段程序,协同当前程序的执行,不是多线程
// Use this for initialization
void Start () {
print ("1");
//StartCoroutine ("CoroutingDemo1");
StartCoroutine (CoroutingDemo1());//这种调用方式好一点,当方法含有参数时可以直接在括号里写参数
print ("5");
}
// Update is called once per frame
void Update () {
}
IEnumerator CoroutingDemo1(){
print ("2");
yield return new WaitForSeconds (5f);
print ("3");
yield return new WaitForSeconds (2f);
print ("4");
}
void OnDisable(){//当此脚本的enalbe=false时调用此脚本
//topCoroutine ("CoroutingDemo1");//关闭单个开启的协程
StopAllCoroutines ();//关闭所有的协程
}
}
using UnityEngine;
using System.Collections;
public class Cotouting : MonoBehaviour {
public Vector3 targetPosition;
public float moveSpeed;
// Use this for initialization
void Start () {
//StartCoroutine (CountDown("Messgae"));
//StartCoroutine (SaySomeThings());
//StartCoroutine (RepeatMessage(5,1f,"nihao"));
StartCoroutine (MoveToPosition(targetPosition));
}
// Update is called once per frame
void Update () {
}
IEnumerator CountDown(string msg){
for (float i = 3; i >=0; i-=Time.deltaTime) {
yield return 0;
}
print (msg);
}
IEnumerator SaySomeThings(){
print ("started");
//yield return StartCoroutine (Wait(1f));
yield return new WaitForSeconds (1f);
print ("1s");
yield return StartCoroutine (Wait(2.5f));
print ("2.5s");
}
IEnumerator Wait(float duration){
for (float i = 0; i <= duration; i+=Time.deltaTime) {
yield return 0;
}
}
IEnumerator RepeatMessage(int count ,float frequence,string msg){
for (int i = 0; i < count; i++) {
print (msg);
for (float j = 0; j <=frequence; j+=Time.deltaTime) {
yield return 0;
}
}
}
IEnumerator MoveToPosition(Vector3 target){
while(transform.position!=target){
transform.position = Vector3.MoveTowards (transform.position,target,moveSpeed*Time.deltaTime);
yield return 0;
}
}
}
- UNITY之SendMassage,Corouting
- Unity之Unity基本操作
- unity之入门经验
- unity菜鸟之路
- unity之Animation组件
- Unity之局域网通信
- Unity之渲染问题
- Unity开发之Android
- unity学习之公路
- Unity学习之三
- unity之小结
- unity之数组
- unity之枚举
- unity之抽象类
- unity之接口
- unity之接口
- unity之数据库
- unity之集合
- Codeforces Round #277.5 (Div. 2) C. Given Length and Sum of Digits...
- ADF Table新增临时字段计算2个字段的和
- final的作用随着所修饰的类型而不同
- Java AIO总结与示例
- Shell脚本 变量笔记
- UNITY之SendMassage,Corouting
- 动态规划之矩阵链乘法理解
- 用java编写一个函数,统计一个字符串中每个字母出现的次数
- 构建ceph deb 安装包
- 微信支付笔记
- ZCTF Android1-200题解
- 2016.08.16【初中部 NOIP提高组 】模拟赛C
- HDU 5855 Less Time, More profit(最大权闭合图)
- JPA 配置persistence.xml