29606人加入学习
(126人评价)
Unity API常用方法和类详细讲解(基于Unity5.6)
价格 免费
承诺服务

基本用法

public class V2CS : MonoBehaviour {

	void Start () {
        Vector2 a = new Vector2(2, 2);
		Vector2 b = new Vector2(3, 4);
		Vector2 c = new Vector2(3, 0);

		//求a,b之间的夹角,既
		print(Vector2.Angle(a, b));
		print(Vector2.Angle(a, c));//为45°
		//保持方向不变,把向量c的长度限定在2
		print(Vector2.ClampMagnitude(c, 2));//(2,0)
		//b,c之间的距离
		print(Vector2.Distance(b, c));//4

		//从a到b进行插值,既((2+3)/2,(2+4)/2)
		print(Vector2.Lerp(a, b, 0.5f));//(2.5,3)
		print(Vector2.LerpUnclamped(a, b, 0.5f));//(2.5,3)

		//Lerp(a,b,t)t默认0<=t<=1,当t小于等于0,返回a,t大于等于1,返回b
		print(Vector2.Lerp(a, b, 2f));//因为2f大于1,所以返回b(3,4)
		//没有限制,所以在a,b距离的基础上再增加一个等价距离
		print(Vector2.LerpUnclamped(a, b, 2f));//(4,6)

		print(Vector2.Max(a, b));//(3,4)
		print(Vector2.Min(a, b));//(2,2)

	}
}

MoveTowards:做匀速运动

public class V2CS : MonoBehaviour {
	public Vector2 a = new Vector2(2, 2);
	public Vector2 b = new Vector2(10, 3);

	void Update () {
		a = Vector2.MoveTowards(a, b, Time.deltaTime);//a向b以一秒一米的速度靠近
	}
}
[展开全文]

Vector2.MoveTowards(startMarker.position,endMarker.position, Time.deltaTime);

[展开全文]

测试Vector2 的额静态方法使用。向量是从0,0开始的。

Vector2 a = new Vector2(2,2);

b,  (3,4)

c,(3,0)

Vecotr2.Angle();求两个向量之间的夹角度数

Vector2.ClampMagnitude(c,2);限定最大长度。Vector2.DIstance(b,c);  两个向量之间的距离。

Vector2.Lerp(a,b,比例);  

Vector2.LefpUnClamped  不设限的插值

Vector2.Max  长度大的

Vector2.Min   长度小的

Vector2.MoveTowards(a,b,距离);

Vector2.PingPong(当前数值,阈值); 也是相当于在两个点之间进行来回移动。

[展开全文]

Angle(a,b):a和b向量的夹角;

ClampMagnitude(a,x):把a向量的长度限定在x内;

Distance(a,b):计算两个向量之间的距离;

MoveTowards(a,b,Time.deltaTime)

使向量a向向量b做匀速运动;

[展开全文]

1.创建游戏物体f的三种方法
new GameObject();括号里可以添加想要创建的物体类型例如
new GameObject("Cube");创建出一个正方体
GameObject.Instantiate();
实例化Prefabs或实例化游戏物体
GameObject.CreatePrimitive();
创建原始图形如:.GameObject.CreatePrimitive(PrimitiveType.Cube);
2.通过代码给游戏物体添加组件
 private GameObject object;
object.AddComponent<Rigidbody>();
通过代码给游戏物体j添加脚本
object.AddComponent<脚本名>();
3.GameObject游戏物体的常用类
GameObject.activeInHierarchy 物体是否处于激活状态(包括自身及子物体)
4.UnityEngine下Object的静态方法
Object.Destroy:
Destroy(gameobject,time);
Object.DontDestroyOnLoad:
DontDestroyOnLoad(transform.gameObject);(在进行场景之间转换时不会被销毁,会被带到下一个场景中)
Object.FindObjectOfType:
Object   a =FindObjectOfType(typeof(组件名))
Rigidbody  a=FindObjectOfType<Rigidbody>();
只返回第一个被检测到含有此组件的物体。不查找未激活的游戏物体
Object.FindObjectsOfType:
Object   a =FindObjectsOfType(typeof(组件名))
Rigidbody a=FindObjectsOfType<Rigidbody>();
将检测到的所有的含有此组件的物体都存在数组中。不查找未激活的游戏物体
5.查找方法
Find(比较耗费性能)
FindGameObjectsWithTag返回所有是次标签的物体返回数组
FindWithTag只返回第一个检测到的物体。
6.游戏物体间消息的发送和接收
GameObject.BroadcastMessage();包括其自身及其子物体
GameObject.SendMessage(); 不包括子物体
GameObject.SendMessageUpwards();包括其自身及其父物体
7.得到组件的各种方法函数

GetComponent;只会得到在游戏物体身上的第一个检测到的组件

GetComponent;会得到物体上所有的组件

GetComponentInChildren;会得到其自身及其子物体身上第一个检测到的相应的组件

GetComponentInParent;会得到其自身及其父物体身上第一个检测到的相应的组件

GetComponentsInChildren;会得到其自身及其子物体身上所有检测到的相应的组件

GetComponentsInParent;会得到其自身及其父物体身上所有检测到的相应的组件

8.Public Functions

Invoke;

Public void Invoke(string methodName,float time );可延时调用方法

CancelInvoke;取消所有调用的方法(仅适用于当前脚本,不影响其他脚本中相同方法的调用)

InvokeRepeating;重复调用方法

IsInvoking;判断方法是否正在被调用

协程:

1.返回值是IEnumerator

2.返回参数时使用yield return null/0。

3协程方法的调用:使用StartCoroutine(需要调用的协程方法的名字);

4暂停yield return new WaitForSeconds(需要暂停的时间);

例如:

IEnumerator Method(){

内容;

yied return null;

}

StartCoroutine();开启协程

StopCoroutine();关闭协程

StopAllCoroutine();

9.与鼠标相关事件函数OnMouseXXX

OnMouseDown();当鼠标按下时触发

OnMouseUp();当鼠标抬起时触发

OnMouseDrag();当鼠标已经按下但没有抬起时触发

OnMouseEnter();当鼠标在游戏物体身上但没有按下鼠标时触发

ONMouseExit();当鼠标离开游戏物体时触发

OnMouseOver();当鼠标在游戏物体身上时无论是鼠标按下或没有按下只要鼠标在游戏物体身上就触发。

10.Mathf里面的方法

Mathf.PI 圆周率

Mathf.Deg2Rad 将度数转变为弧度

Mathf.Rad2Deg 将弧度转成度数

Mathf.Epsilon 无限小的数

Mathf.Infinity 无限大的数

Mathf.Abs 绝对值

Mathf.Ceil 向上取整

如Mathf.Ceil(10);结果为10.

Mathf.Ceil(10.2f);结果为11.

Mathf.Ceil(-10);结果为-10.

Mathf.Ceil(-10.2f);结果为-10.

Mathf.Floor();向下取整。

Mathf.Clamp(float value,float min,flaot max); valur为自己定义的数值。如果value小于min则将value改为min,如果value大于min且小于max则value为自身值,如果value大于max则将value改为max。

Mathf.ClosesPowerOfTwo(int value);取离value最近的2的多次方

如Mathf.ClosesPowerOfTwo(3);返回4

Mathf.ClosesPowerOfTwo(7);返回8

Mathf.DeltaAngle();返回角度之间的最小夹角。如721°-360°=361°,其返回1°。

Mathf.Exp(float power);代表e的power次方

Mathf.Max(float a,float b);比较两个数中的最大值(float可换为int)

Mathf.Max(float[] value);比较数组中的最大值(float可换为int)

Mathf.Min(float a,float b);比较两个数中的最小值(float可换为int)

Mathf.Min(float[] value)比较数组中的最小值(float可换为int

Mathf.Pow(float a,float b);a的b次方。

Mathf.Sqrt();开平方根。

Mathf.Lerp(float a,float b,float t);插值运算 

t为a到b之间的距离的比率,如t=0.5时,返回值为(a+b)*0.5,t为0时,返回值为a,t为1时返回值为b;

Mathf.LerpAngle();角度的差值运算

 Mathf.MoveToWards(float a,float b,float t);

a初始 值,b目标值,t从a到b每帧所移动的距离。

Mathf.PingPong(float a,float b);返回值在0到b之间0为最小值,b为最大值,返回值随着a的增大在0到b之间往返。

11.Input

GetTouch()屏幕触摸

Input.anyKeyDown();按下任何键都会触发,包括鼠标。

Input.mousePosition;鼠标在键盘上的实时坐标。

12.Vector2

magnitude;返回向量的长度。根号下X的平方加Y的平方。

normalized;将长度不为一的向量在方向不变的情况下,将长度取一。不改变向量本身。 

sqrMagnitude;X的平方加Y的平方不开根号,一般用于较为节省性能的比较两个向量的长度

向量是结构体,如要修改,需整体修改。如

transform.position=new vector3(3,3,3);

如想要只修改物体的X坐标,直接调用transform.position.x是错误的。需:

Vector3 pos=transform.position;

pos.x=10;

transform.position=pos;

即可。

Vector2.Equals;判断两个向量是否相等。相等返回true,不相等返回flase.

Vector2.Normalize();将向量单位化,改变向量。

Vector2.Angle();用来取得两个向量的夹角。

Vector2.Distance(Vector2 a,Vector2 b);返回a,b两个点的距离。

Vector2.Dot()点乘。

Vector2.MoveTowards(Vector2 a,Vector2 b,float c);物体从a移动到b,c是速度。

[展开全文]