[Unity3D]引擎学习之注意事项(持续更新中)

原理相关

  • 在3D世界中,通过triangles来呈现shape的原因:We use triangles because they provide a fast way for a computer to represent surfaces, they’re pretty simple structures, and we’ve been using them for quite a while, so computer hardware is optimized for them. Now let’s take a look at the simplest kind of meshes...

调试相关

  • 如果是想在触发粒子系统效果的时候播放声音(比如爆炸的特殊发生时也播放声音),则需要将爆炸效果的粒子系统保持为Prefab后,添加Audio Source组件,在组件中添加声音文件并且确保play on awake是开启的:
    [Unity3D]引擎学习之注意事项(持续更新中)_第1张图片
  • LoadScene之后GI光影发生变化,并且没有重新烘焙。方法为在Lighting面板将最下方的Auto选项取消勾选,并且点一下旁边的build。该错误只在编辑器发生,发布后的产品不会有该问题
    194303-20160927210631672-481733742.png
  • GameObject对象基本不会通过FindGameObjectWithTag来获取,还可以通过静态变量或者建立一个静态的单例资源管理器来声明Transform变量。其他脚本可以无需搜索直接获取主角Transform
  • 获取子弹对象,在速度不快的时候可以通过比较Tag获取:
    ```
    void OnTriggerEnter(Collider other)
    {

      if (other.tag.CompareTo("EnemyRocket") == 0) {

    ```
    如果速度快,就通过射线获取

  • SmoothDamp
    [Unity3D]引擎学习之注意事项(持续更新中)_第2张图片

Unity协程处理

  • MonoBehaviour.StartCoroutine: 协程作业
    Unity不支持多线程,但是可以通过协程作业,开启一个下载任务,但不会block主线程的继续执行,但它不是多线程,还是遵循于Mono的生命周期,可以用于跨帧的操作
    One common misconception cleared up: Coroutines do not execute in parallel to your code. They run in the same thread as everything else in your scripts, so editing the same values in Coroutines and Update is safe.
  • 如果有需求如下:想要在主角挂了之后,延迟几秒后重新加载Scnen, 我们不能通过Thread.Sleep(time), 因为是单线程,所以这样会导致整个画面都会卡住time时间,所以我们只能通过协程来完成这个需求

GameObject和Component的区别: 参考链接

  • GameObject可以理解为一个容器,里面可以包括一个或者多个的component,比如transform就是用来标识3D位置的component。好比GameObject就是一辆车的基本底盘或者框架,轮胎 车门等等就是它的component,这些component都是属于这个GameObject的,component不可能脱离GameObject而独立存在。
  • 所以说transform.gameObject是获取transform这个组件所在的容器对象,而gameObject.transform是获取容量里面的transform组件。
  • GameObject只有非常少的属性和方法,具体的效果还是需要操作容量里面的各个component。若添加了一些自定义的component,我们可以通过GetComponent()获取。
  • 注意,若一个GameObject中多个同类型的的组件(比如一辆车有多个车轮),则GetComponent()会返回第一个组件,GetComponents()会返回组件队列。
  • SetActive是GameObject的启用禁用,enable是GameObject下组件的启用禁用
  • 获取对象的方法解析, 不过这个文档里面说Transform和GameObject有点问题,GameObject应该是Transform组件的容器

  • 获取隐藏的GameObject对象方法
    ```
    GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));
    foreach (GameObject pObject in pAllObjects)
    {
    if (pObject.transform.parent != null)
    {
    continue;
    }

          if (pObject.hideFlags == HideFlags.NotEditable || pObject.hideFlags == HideFlags.HideAndDontSave)
           {
               continue;
           }
    
          if (Application.isEditor)
           {
               string sAssetPath = AssetDatabase.GetAssetPath(pObject.transform.root.gameObject);
               if (!string.IsNullOrEmpty(sAssetPath))
               {
                   continue;
               }
           }
    
          Debug.Log(pObject.name);
       }

    ```

    推荐关注的技术

  • Unity的UI系统,UGUI和NGUI
  • 更多C#知识:数组(List),委托,代理。
  • Vector3下的相关函数,Mathf下的相关函数。
  • 各类Unity范例教程

转载于:https://www.cnblogs.com/xiuj/p/5914280.html

你可能感兴趣的:([Unity3D]引擎学习之注意事项(持续更新中))