GameObject、MonoBehavior类
一、GameObject
I、Static Functions:
1)Destroy(既可以销毁游戏物体,也可以销毁组件,同时也可以指定时间销毁某个游 戏物体)
2)FindObjectOfType(查找某个物体)
FindObjectsOfType(查找多个游戏物体,但不查找未**的游戏物体)
3)CreatePrimitive
4)Find(根据名字查找,会遍历整个场景中所有的游戏物体。当游戏物体多时,耗费性能,慎用)
5)FindGameObjectsWithTag
6)FindWithTag(根据标签查找)
a)GameObject go=GameObject.Find(“Main Camera”);
go.SetActive(false);
b)GameObject[ ] gos=GameObject.FindGameObjectsWithTag(“MainCamera”);
gos[0].SetActive(false);
c)GameObject go=GameObject.FindGameObjectWithTag(“MainCamera”);
go.SetActive(false);
II、
- GameObject.BroadcastMessage(methodName,parameter=null,option)(广播消息,直接传递方法名,不需要知道消息的接收者具体是谁)
针对某物体的所有子物体
2)SendMessage(methodName,value=null,option)
只会针对某一个游戏物体上所有脚本中的方法会被调用
3)SendMessageUpwards()
针对当前物体的父物体,还有父物体的父物体
二、MonoBehavior(Inherited members 继承)
Variables
1)gameObject 获取组件所在的游戏物体
2)transform 获取组件所在游戏物体的transform组件
3)tag 组件所在游戏物体的标签
4)name 获取游戏物体的名字
5)isActiveAndEnabled 判断组件是否为**状态
6)enabled=true/false 设置组件是**或禁用(禁用Update)
Public Functions
1)GetComPonent
GetComPonents
2) GetComPonentInChildren
GetComPonentsInChildren
3) GetComPonentInParent
GetComPonentsInParent
三、MonoBehavior总览
Variables:
MonoBehavior.runInEditMode 只有在编译器情况下运行,还有ExcudeInEditMode
Public Function:
Invoke 调用某个方法
InvokeRepeating 重复调用某种方法
Static Functions:
print 输出日志,Debug的缩写
MonoBehaviour中Invoke的使用
Invoke 调用某个方法
InvokeRepeating 重复调用某种方法
CancelInvoke 取消所有调用
(CancelInvoke、InvokeRepeating两个配合使用)
1)Invoke的使用
Invoke(字符串(方法名),时间)
a、IsInvoking 判断该方法是否被调用
一直输出,Attack在队列里
调用完之后将会移除
b、InvokeRepeating(方法名,开始时间,间隔时间) 重复调用
c、CancelInvoke 取消调用,可指定名字,也可不指定,取消所有的Invoke
Message:
1、OnMouseDown 在按下鼠标的时候
条件:1)如果碰撞器是Trigger,必须设置Edit–Project Settings–Physic—Queries Hit Trigger保证是勾选状态。2)可以保证Collider状态
2、OnMouseUp 鼠标在按下抬起的时候
3、OnMouseDrag 鼠标按下的时候进行拖拽,一直按着鼠标不拖拽也会一直运行
4、OnMouseEnter 鼠标放上去的时候
5、OnMouseExit 鼠标移出的时候
6、OnMouseOver 鼠标在上面的时候,只要鼠标在游戏物体上,就会一直执行
7、OnMouseUpAsButton 按下和抬起在同一个物体上时才会执行
四、总结及收获
本节主要学习了GameObject类中的静态方法:Destroy、FindObjectOfType、FindObjectsOfType、CreatePrimitive、Find、FindGameObjectsWithTag、FindWithTag等;变量:BroadcastMessage、SendMessage、SendMessageUpwards等一些常用的方法及变量。
还有MonoBehavior(Inherited members 继承)中的Variables:gameObject、transform、tag、name、isActiveAndEnabled、enabled=true/false等;Public Functions:GetComPonent、GetComPonents、GetComPonentInChildren、GetComPonentsInChildren、GetComPonentInParent、GetComPonentsInParent等。
MonoBehavior中Variables:runInEditMode、ExcudeInEditMode等;Public Function:Invoke、InvokeRepeating 等;Static Functions:print等。还具体介绍了Invoke、InvokeRepeating、CancelInvoke的使用。Message:OnMouseDown、OnMouseUp、OnMouseDrag、OnMouseEnter、OnMouseExit、OnMouseOver、OnMouseUpAsButton等
本节内容介绍了Unity3D中重要的GameObject类和MonoBehavior类的使用,可以帮助解决很多游戏过程中的实现。