Unity 常用常找的东西存放

本文章由cartzhang编写,转载请注明出处。 所有权利保留。
文章链接:http://blog.csdn.net/cartzhang/article/details/50483316
作者:cartzhang

常需要,常查找!
自己记录下!

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 (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,这样很难调试。

5.常用属性

ContextMenu,在面板功能增加选项。
ContextMenuItemAttribute,对编辑添加右键操作。[ContextMenuItem("Reset", "ResetName")]
HeaderAttribute,在变量前面增加说明:[Header("魔法值")]
MultilineAttribute,[MultilineAttribute]
RangeAttribute,限制输入范围
RequireComponent,依赖组件,[RequireComponent(typeof(Rigidbody))]
[Space(10)],在面板上,增加一个空行。
[Tooltip("This year is 2015!")],鼠标移到对象上,显示提示。

6.MenuItem设置快捷键

[MenuItem("Tools/New Option %#a")]
private static void NewMenuOption()
{
}

菜单快捷键设置,可单独使用,也可以组合使用:


% – CTRL on Windows / CMD on OSX

# – Shift

& – Alt

LEFT/RIGHT/UP/DOWN – Arrow keys

F1…F2 – F keys

HOME, END, PGUP, PGDN

AddForce和velocity有什么区别?

这是力与速度的区别。
施加恒力,对象获取加速度,然后就加速前进。
直接使用Velocity,速度很平稳,移动为恒定值。

力与速度的速度之比较

检查那个按键被按下


    public void DetectPressedKeyOrButton()
    {
        foreach (KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
        {
            if (Input.GetKeyDown(kcode))
            {
                Debug.Log("KeyCode down: " + kcode);
            }
        }
    }

7. 对象是否可见


public static class RendererExtensions
{
    public static bool IsVisibleFrom(this Renderer renderer, Camera camera)
    {
        Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
        return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
    }
}

然后判断:


        if (rend.IsVisibleFrom(Camera.main)) Debug.Log("Visible");
        else Debug.Log("Not visible");

若有问题,请随时联系!
非常感谢你!!

原文地址:https://www.cnblogs.com/qitian1/p/6461927.html