Good Practice

  1. 普通的Public变量可以在inspect里显示,变量是可以在inspect里赋值并realtime反映在被attach到的GameObject上的。注意经典public property的写法,在inspect里不会显示
  2. You can't new MonoBehaviour, or override MonoBehabiour基类,不可以override Start和Update各种方法. Only public no argument constructor work, ignore fancy constructor.
  3. 一般Unity下的初始值由Start()方法提供,而不是通过Constructor,如果非要写Constructor只可以用不带参数的而且不可以重载带不同参数的constructor(默认会变为不带参数的)。
  4. Corotine是关于如何suspend 一个方法 in the game(防止该程序块执行时间过长拖慢整个game loop或者为了Asyn), 以及访问WWW的技巧

MonoBehaviour Lifecycle function

  • Awake():

    • 当一个脚本实例被loaded into scene时Awake被调用,程序Dynamic添加或者手动Drag到GameObject上的script一旦load到scene里就开始Awake()。
    • Awake用于在游戏开始之前初始化变量或游戏状态。Awake在所有对象被初始化之后调用,所以你可以安全的与其他对象对话或用诸如GameObject.FindWithTag这样的函数搜索它们。
    • Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth.
    • private GameObject target;
      void Awake() {
      target = GameObject.FindWithTag("Player");
      }
    • Awake总是在Start之前执行。这允许你协调初始化顺序。
    • Awake不能用作co-routine.
    • •Will be called once after all prefab and GameObject is been instantiated
      •If a GameObject is in-active during start up Awake is not called until it is made active, or a function in any script attached to it is called.
      •Awake is called first even if the script component is not enabled and is best used for setting up any resources between scripts and initialization.
  • Start():

    • Start在behaviour的生命周期中只被调用一次,Start is only called if the script instance is enabled,调整Script启动与否可以控制初始化的时间。
    • 这是Initialize初始参数代替Constructor的地方。可以包含Coroutine方法.
  • Awake VS Start

  • FixedUpdate():

    • 处理RigidBody相关的计算放这里,Call on fixed time interval(使用的原因是如果放到Update()里called once per farme,很多geometry被load的话主程序会Hang)。
    • 每一个game loop的时间长短可能不一样,取决于渲染每次loop的速度,所以一个fame里会被call几次game loop,FixedUpdate也就决定于这个次数,可能一个frame被call10次,也可能被call5次。在每个frame的FixedDetalTime等长的情况下比如50帧的画面即0.02/sec,有的渲染快的frame被call了5次FixedUpdate,有的渲染慢的frame被call了10次FixedUpdate
    • • is called before the first frame update only if the script instance is enabled.Start is called after Awake,immediately before the first Update,but only if the script component is enabled.
      •This means that you can use Start for anything you need to occur when the script component is enabled.This allows you to delay any part of your initialization code until it's really needed.
    • •Called more frequently than Update() , Used for physics GameObject
      •Fixed Update is called on a regular timeline and will have same time between calls
      •Used for regular updates such as adjusting physics (Rigibody) objects
  • Update():

    • Been called every frame
    • •Most commonly used function in unity, Used for all Non-Physics GameObject
      •Called Every frame
      •Update is not called on a regular timeline. If one frame takes longer to process then the time between update calls will be different.
  • Awake VS Start
  • LateUpdate():Call after all Update() function has been called,这可用于调整脚本执行顺序。比如:For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
  • OnEvent functions:called as part of reactions or event to different situations,见unity主页

Gameloop

Game Loop ex:点击Play, call Awake(),然后是Start()

  每个frame可以有多次的gameloop,每次gameloop里先后执行

    • Any of On event(OnDisable OnEnable) -> Update() -> Coroutines() -> LateUpdate() -> GUI render, Camera Render (Continue the cycle)
    • 上面GameLoop里没有引入FixedUpdate(),因为其在固定的frame下call
    • WaitForFixedUpdate, WaitForEndOfFrame, and then WaitForSeconds是在Coroutines里被call的,不是每个loop都call,看其设定。
    • 所有的Coroutines都是建立在MonoBehivour下的,如果MonoBehivour destroy了后所有的Coroutines也将消失作。

Rule of write code with C# in Unity3d的更多相关文章

  1. Unity3D Shader官方教程翻译(十九)----Shader语法,编写表面着色器

    Writing Surface Shaders Writing shaders that interact with lighting is complex. There are different ...

  2. JS框架avalon简单例子 行编辑 添加 修改 删除 验证

    为什么要写这个例子:做表单的时候,表单包含主子表,对于子表的编辑,使用的是easyui datagrid的行编辑功能,由于业务比较复杂,实现起来比较麻烦,代码写的也很多,因为插件的封装,无法操作原始的 ...

  3. rem和em,px的使用

    rem是CSS3中新增加的一个单位值,他和em单位一样,都是一个相对单位.不同的是em是相对于元素的父元素的font-size进行计算:rem是相对于根元素html的font-size进行计算.这样一 ...

  4. [原] XAF 如何基于业务规则禁用属性

    How to: Disable Property Editors Based on a Business Rule // Developer Express Code Central Example: ...

  5. 电信SDK Pay函数里面System.out.print 无输出消息

    private void Pay(HashMap<String, String> payParams){ System.out.print("----------Pay Dian ...

  6. 编写高质量js代码

    原文链接:http://code.tutsplus.com/tutorials/24-javascript-best-practices-for-beginners--net-5399 jquery代 ...

  7. CodeForces 918D MADMAX(博弈+记忆化搜索)

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  8. [LeetCode] Tag Validator 标签验证器

    Given a string representing a code snippet, you need to implement a tag validator to parse the code ...

  9. [Swift]LeetCode591. 标签验证器 | Tag Validator

    Given a string representing a code snippet, you need to implement a tag validator to parse the code ...

随机推荐

  1. 【解题报告】[动态规划] CodingTrip - 携程编程大赛 (预赛第一场)- 聪明的猴子

    原题: 聪明的猴子 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Problem D ...

  2. ORACLE执行计划 explain说明

    ORACLE SQL优化工具系列之--EXPLAIN PLAN 对于oracle数据库来说,sql语句的优化可能是对性能提升最为明显的,当然对于DBA来说,也是挑战性比较大的.为了优化一个复杂的SQL ...

  3. 《Unix网络编程》卷2 读书笔记 第2章- Posix IPC

    1. 概述 Posix IPC 包括:Posix消息队列.Posix信号量.Posix共享内存区 Posix IPC在访问它们的函数和描述它们的信息上有一些类似点. 本章讲述所有这些共同属性:用于标识 ...

  4. 【转】linux设备驱动程序中的阻塞机制

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275272.html 阻塞与非阻塞是设备访问的两种方式.在写阻塞与非阻塞的驱动程序时,经 ...

  5. 配置apache以fastcgi运行php

    apache默认是用自带的mod_php模块运行php,现在我们介绍使用fastcgi来执行php脚本.先说下fastcgi的优点: Fastcgi的优点: 从稳定性上看, fastcgi是以独立的进 ...

  6. [教程] Windows Server 2008 R2架设SMTP服务器发送邮件教程

    Windows Server 2008 R2 架设SMTP服务器实现邮件发送 目的:架设SMTP服务器实现邮件发送. 一.域名设置 添加“邮件交换记录(MX)”: Newjs.cn           ...

  7. Oracle锁表(转载)

    锁定类型               行级锁               表级锁行级锁         ---- 行被排他锁定         ----在某行的锁被释放之前,其他用户不能修改此行    ...

  8. oracle 定时任务(DBMS_JOB)

    示例如下: -- 每隔一分钟执行存储过程p1 create or replace procedure p2 as job_num number; begin dbms_job.submit(job_n ...

  9. python中函数的总结之三

    1. 可变长参数 在函数中可变长参数分为两种:一种是非关键字参数,表示为元组:一种是关键字参数,表示为字典. 具体看下面的例子代码,相当于单元测试: #!/usr/bin/env python #'t ...

  10. 数字图像去噪典型算法及matlab实现

    原文地址http://jncumter.blog.51cto.com/812546/243961   图像去噪是数字图像处理中的重要环节和步骤.去噪效果的好坏直接影响到后续的图像处理工作如图像分割.边 ...