Unity学习(三)

吐槽

今天是暑假学校的第一天唉,,,学习方面效率蛮低的,专心程度不够,要好好训练自己这方面的内容了。

协程

协程方法—调用协程方法时候,不会等协程方法完才结束
自身可以暂停的
协同程序的特点
1、协程在中断指令(YieldInstruction)产生时暂停执行
2、协程一暂停执行便立即返回 //中断协程后返回主函数,暂停结束后继续执行协程剩余的函数。
3、中断指令完成后从中断指令的下一行继续执行
4、同一时刻、一个脚本实例中可以有多个暂停的协程,但只有一个运行着的协程
5、函数体全部执行完后,协程结束
6、协程可以很好的控制跨越一定帧数后执行的行为
7、协程在性能上、相比于一般函数几乎没有更多的开销
Unity协程长的有点像线程,但却不是线程.因为协程仍然是在主线程中执行,且在使用时不用考虑同步与锁的问题.协程只是控制代码等到特定的时机后再执行后续步骤.1
1创建一个协程函数

 IEnumerator methodName(Object parameter1,Object parameter2,...){
        // to do something
        yield return YieldInstruction/other/null;
        // to do something else
    }

注意:
协同函数的返回值的类型必须是Coroutine,Coroutine继承与Yieldinstruction。
所以协同程序的返回类型就只能是null,等待的时间,等待的帧数。。由此可见WWW 也是实现了Coroutine的~

2启动协程:
/形式一
StartCoroutine(CustomCorutineFn());
StartCoroutine(CustomCorutineFn(7));//向方法中传递参数
//形式二
StartCoroutine(“CustomCorutineFn”);
StartCoroutine(“CustomCorutineFn”,7);//向方法中传递参数
1、StartCoroutine(IEnumerator routine);
优点:灵活,性能开销小。
缺点:无法单独的停止这个协程,如果需要停止这个协程只能等待协同程序运行完毕或则使用StopAllCoroutine();方法。
2、StartCoroutine (methodName:string, value : object = null);
优点:可以直接通过传入协同程序的方法名来停止这个协程:StopCoroutine(string methodName);
缺点:性能的开销较大,只能传递一个参数。

3停止协同程序
1、StopCoroutine(string methodName);
2、StopAllCoroutine();
3、设置gameobject的active为false时可以终止协同程序,但是再次设置为true后协程不会再启动。

eg

调用的时候:StartCoroutine(changeColor());

//协程方法Coroutines
    //1返回值IEnumerator
    //2 返回参数是yield return null;
    //3 调用StartCoroutinr
    IEnumerator changeColor()
    {
        print("hhhhhhh3");
        cube.GetComponent<MeshRenderer>().material.color = Color.red;

        print("hhhhhhh4");
        yield return null;
    }

使用Coroutine实现颜色动画渐变

yield return new WaitForSeconds(3);//暂停三秒

跟鼠标相关事件函数OnMouseXXX讲解

这里写图片描述

onmouseover和onmouseout 鼠标指针移动到或离开元素时触发脚本
onmousedown和onmouseup 鼠标按钮被按下或松开时触发脚本
onclick和ondbclick 鼠标单击和双击时触发脚本
onmousemover 鼠标指针移动时触发脚本

Mathf里面的静态常量

A collection of common math functions.
1Deg2Rad 度数变为弧度的
Degrees-to-radians conversion constant (Read Only).
2Rad2Deg 弧度变为度数的
Radians-to-degrees conversion constant (Read Only).
3Epsilon 很小的小数
A tiny floating point value (Read Only).
4Infinity 无限大的数
A representation of positive infinity (Read Only).
5NegativeInfinity
A representation of negative infinity (Read Only).

Mathf的主要的方法

Mathf.Abs绝对值
计算并返回指定参数 f 绝对值

Mathf.Clamp限制
static function Clamp (value : float, min :float, max : float) : float
限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
static function Clamp (value : int, min :int, max : int) : int
限制value的值在min和max之间,并返回value。

Mathf.DeltaAngle增量角 最小夹角
static function DeltaAngle (current :float, target : float) : float
计算给定的两个角之间最短的差异。
// Prints 90
Debug.Log(Mathf.DeltaAngle(1080,90));

Mathf.Exp指数
static function Exp (power : float) : float
返回 e 的 power 次方的值。

Mathf.Lerp插值
static function Lerp (from : float, to :float, t : float) : float
基于浮点数t返回a到b之间的插值,t限制在0~1之间。
当t = 0返回from,当t = 1 返回to。当t = 0.5 返回from和to的平均值。

Mathf.MoveTowards移向
static function MoveTowards (current :float, target : float, maxDelta : float) : float
改变一个当前值向目标值靠近。
这实际上和 Mathf.Lerp相同,而是该函数将确保我们的速度不会超过maxDelta。maxDelta为负值将目标从推离。

Mathf.PingPong乒乓
static function PingPong (t : float, length: float) : float
0到length之间往返。t值永远不会大于length的值,也永远不会小于0。
The returned value will move back and forthbetween 0 and length.
返回值将在0和length之间来回移动。

差值运算

Lerp 可以看成是一种进度
Lerp(a,b,c);
这里,并不一定需要
Lerp(min,max ,c);
并不一定需要 从小到大,
可以将 a 看成是起点,
b 看成是终点,
c 看成是一个进度的 百分比,
100% 就是 1, 50% 就是 0.5 , 30% 就是 0.3 。
可以看成是 坐火车,
从 北京坐车 到 山东,
也可以从 山东 坐车到 北京。
假如,从北京 坐车到 山东 一共是花 100分钟,
那么,可能 北京发车后,经过 20分钟,到达了天津,
那么,从山东坐车,发车后,80分钟,很可能也是到达 天津。
就像每天 攒钱 10块钱,
十天 就攒 100块,
如果有100块, 每天花 10块,
那么,也是十天 花完,
Mathf Lerp 的含义就是,从 某一个值,到某一个值的过渡,根据这个百分比,
我们就可以得到一个 进度值。
可以是 逐渐增大的,也可以是 逐渐 减小的。

Input类输入类(按键、按键、触摸相关检测)

1Input里面的GetKeyXXX的使用
GetKey :Returns true while the user holds down the key identified by name.
GetKeyDown :Returns true during the frame the user starts pressing down the key identified by name.
GetKeyUp :Returns true during the frame the user releases the key identified by name.
GetKey:用户长按按键有效;
GetKeyDown:用户按下按键时有效;
GetKeyUp:用户抬起按键时有效;

2anyKey,anyKeyDown
anyKey用于监听用户的任意按键输入或鼠标点击,事件触发后返回true。anyKeyDown:当任意按键松开时返回true。

3GetButton(string buttonName) //虚拟的按键
GetButton用于监听Button的输入,参数为自己指定的button的名字,在Unity中可以使用管理器添加或修改Input参数。unity已经为用户预设了多种输入情况,在button中预设了Fire、Jump等输入情况。用户也可以自行修改,打开Edit–>Project Settings–>Input可以看到预设

鼠标的输入
mousePosition
Input.mousePosition返回当前鼠标的位置,这里指的是距离原点的像素位置,说明一下,Unity中的原点(0,0)位置为左下角,上位y轴正方向,右为x轴正方向。返回值类型为Vector3。
GetMouseButton ()里面是0.,1,2
GetMouseButton 对应的键处于按下状态时返回true
GetMouseButtonDown 对应的键被按下时返回true
GetMouseButtonUp 对应的键弹起时返回true
参数为int型,含义为:0左键,1右键,2中键。

猜你喜欢

转载自blog.csdn.net/sakurakider/article/details/81072568