您的位置:首页 > 移动开发 > Unity3D

Unity3D自己常用代码

2015-09-02 15:04 495 查看
常需要,常查找!

自己记录下!

1.

var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = ray.GetPoint(10.0f);
transform.LookAt(ray.GetPoint(10.0f));


2.
提高效率

GetComponent(), FindObjectByType() and Instantiate()
尽量少使用。

3. InvokeReapting 的内部实现

实现过程
{
找到函数方法名称,存储
等待调用时间,WaitForSeconds(time)
循环知道用户取消
{
Invoke 存储的函数方法
等待时间
}
删除记录保存的方法
}

4. 

Coroutine 和InvokeReapting

 

协同程序,不是多线程。但是他有自己的堆栈,局部变量,指令指针,与其他协同程序共享全局变量等信息。在多处理器下,多线程同时运行,协同程序,在某时刻之有一个在运行。

 

若你有个负责的任务,它非常费帧率,就考虑使用协同或InvokeReapting.

若是简单任务,就嫑在协同或InvokeReapting使用,这不会提高效率的。

但是也不用太死板。尽管可能降低效率,代码会更简单和实用。

 

协同是处理时间序列的一种很好的方法。

它不是线程也不是异步。

协同当脚本未激活或对象为激活时不可用。

协同返回时间的WaitForSeconds依赖于Time.timeScale.

 InvokeReapting

/*
void InvokeRepeating(string methodName, float time, float repeatRate)
{
- Find the method called "methodName" in the calling assembly and store it.
- Wait "time" seconds by yielding on WaitForSeconds(time)
Loop until the user cancels this action
{
- Invoke the stored method
- Wait "repeatTime" seconds by yielding on WaitForSeconds(repeatRate)
}
- Remove the method info record from storage.
}
*/
使用Invoke或InvokeReapting,这样很难调试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity