Input&&协程

请添加图片描述
找到
请添加图片描述


一、Input


1)鼠标事件
GetMouseButton(0): 按下鼠标左键不动,程序会一直运行,松开左键程序停止运行。
GetMouseButton(2): 按下鼠标中键不动,程序会一直运行,松开中键程序停止运行。
GetMouseButton(1): 按下鼠标右键不动,程序会一直运行,松开右键程序停止运行。
GetMouseButtonDown(0): 按下鼠标左键时,程序运行一次
GetMouseButtonDown(1): 按下鼠标右键时,程序运行一次
GetMouseButtonUp(2): 按下鼠标中键时,程序不运行,松开中键时,程序运行一次。

if(Input.GetMouseButton(0)){
执行语句;
}

public class ClonBeen : MonoBehaviour {
	public GameObject Sphere;
	void Start () {
	
	}
	
	void Update () {
        if (Input.GetMouseButtonDown(0))
        {
			Instantiate(Sphere, Vector3.zero, Quaternion.identity);//点击鼠标左键克隆一个Sphere
        }
		if (Input.GetMouseButtonDown(1))
		{
			Instantiate(Sphere, Vector3.one, Quaternion.identity);//点击鼠标中键克隆一个Sphere
		}
		if (Input.GetMouseButtonDown(2))
		{
			Instantiate(Sphere, Vector3.one*3, Quaternion.identity);//点击鼠标右键克隆一个Sphere
		}
	}
}


Input.GetButtonDown("Fire1");//点击鼠标左键
Input.GetButtonDown("Fire2");//点击鼠标右键

2)键盘事件
Input.GetAxis(“Horizontal”);//控制游戏对象水平轴方向移动(W/S方向移动)
Input.GetAxis(“Vertical”);控制游戏对象垂直轴方向移动(A/D方向移动)

public class Move : MonoBehaviour {
	float speed = 10f;
	void Start () {
	
	}
	
	void Update () {
		float h = Input.GetAxis("Horizontal")*Time.deltaTime*speed;//水平轴
		float v = Input.GetAxis("Vertical") * Time.deltaTime * speed;
		transform.Translate(h,0,v);

	}
}

另一种键盘控制游戏对象移动:
Input.GetKey(KeyCode.A);
Input.GetKey(KeyCode.D);
Input.GetKey(KeyCode.W);
Input.GetKey(KeyCode.S);

public class Move : MonoBehaviour {
	float speed = 10f;
	void Start () {
	
	}
	
	void Update () {

		float a = Time.deltaTime * speed;
        if (Input.GetKey(KeyCode.A))
        {
			transform.Translate(-a,0,0);
        }
		if (Input.GetKey(KeyCode.D))
		{
			transform.Translate(a, 0, 0);
		}
		if (Input.GetKey(KeyCode.W))
		{
			transform.Translate(0, 0, a);
		}
		if (Input.GetKey(KeyCode.S))
		{
			transform.Translate(0, 0, -a);
		}
	}
}

3)自定义按钮


GetButton : 根据按钮名称返回true当对应的虚拟按钮被按住时
GetButtonDown: 在给定名称的虚拟按钮被按下的那一帧返回true
GetButtonUp: 在用户释放指定名称的虚拟按钮时返回true

二、协程


协程:有点类似于并行系统,多个方法同时处理,开启协同程序就是开启一个线程,协程其实就是一个IEnumerator(迭代器

开启线程: StartCoroutine(CloneEnemy()); 或者StartCoroutine(“CloneEnemy”);
CloneEnemy:为线程名称

Yield语句 :一种特殊的Rerurn语句 ,当函数在下一次被执行时,不是从头开始,而是从Yield语句处开始 (yield return在协程中是连用的 )

运行到 yield return 语句时,会返回一个表达式并保留当前在代码中的位置。 当下次调用迭代器函数时执行从该位置重新启动

public class Enemy : MonoBehaviour {
	
	public GameObject cube;

	void Start () {
		//开启协程
		//StartCoroutine("CloneEnemy");
		StartCoroutine(CloneEnemy());
	}

	void Update () {
	
	}

	IEnumerator CloneEnemy() {
		//IEnumerator:协程(类似于一个方法)
		//IEnumerator CloneEnemy:协程的名字为CloneEnemy

		while (true)
        {
			//yield:确保函数在下一次被执行时,不是从头开始,而是从Yield语句处开始
			yield return new WaitForSeconds(3f);//停顿三秒
			Instantiate(cube);
        }
	
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中,协程是一种特殊的函数,可以在执行过程中暂停并在稍后继续执行。协程通常用于处理需要延迟执行或需要分步执行的任务,例如动画、网络请求等。 要暂停协程的执行,可以使用yield语句。yield语句可以将协程暂停,并在满足某个条件后继续执行。常用的yield语句有以下几种: 1. yield return null:暂停协程一帧的时间,然后继续执行。 2. yield return new WaitForSeconds(time):暂停协程指定的时间(以秒为单位),然后继续执行。 3. yield return new WaitForFixedUpdate():暂停协程直到下一次固定更新(FixedUpdate)时才继续执行。 4. yield return StartCoroutine(otherCoroutine):暂停协程并启动另一个协程,直到另一个协程执行完毕后才继续执行。 以下是一个示例代码,演示了如何使用协程暂停和继续执行: ```csharp using UnityEngine; using System.Collections; public class CoroutineExample : MonoBehaviour { private IEnumerator coroutine; void Start() { coroutine = MyCoroutine(); StartCoroutine(coroutine); } IEnumerator MyCoroutine() { Debug.Log("Coroutine started"); yield return new WaitForSeconds(2f); Debug.Log("Coroutine paused for 2 seconds"); yield return null; Debug.Log("Coroutine resumed"); } void Update() { if (Input.GetKeyDown(KeyCode.Space)) { if (coroutine != null) { StopCoroutine(coroutine); Debug.Log("Coroutine stopped"); } } } } ``` 在上述示例中,协程MyCoroutine()会在开始时打印一条日志,然后暂停2秒钟,再打印一条日志,最后继续执行并打印最后一条日志。按下空格键会停止协程的执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值