经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Unity API学习笔记(1)
来源:cnblogs  作者:BrainK_1400  时间:2019/7/17 10:38:30  对本文有异议

首先创建测试项目:

这里我选择了2D,其实都可以,之后可以在项目中修改。

修改方法:

进入正题。

首先看一下官方提供的>手册和>脚本API文档。

创建C#脚本文件并打开:

默认的脚本文件为以下格式:

  1. 1 using System.Collections;
  2. 2 using System.Collections.Generic;
  3. 3 using UnityEngine;
  4. 4
  5. 5 public class TestScript : MonoBehaviour
  6. 6 {
  7. 7 // Start is called before the first frame update
  8. 8 void Start()
  9. 9 {
  10. 10
  11. 11 }
  12. 12
  13. 13 // Update is called once per frame
  14. 14 void Update()
  15. 15 {
  16. 16
  17. 17 }
  18. 18 }

Start( )、Update( )都是事件函数。去查一下事件函数有哪些。>事件函数的执行顺序(Order of Execution for Event Function)

 这里官方提供了一个叫脚本生命周期流程图(Script lifecycle flowchart)的东西。(PS:好像版本更新导致现在的和以前的不太一样,所以最好跟着官方的文档来。)

那就从事件函数??开始??。

Awake(): 

  1. 1 void Awake() // flowchart上是处于Initialization段也就是初始化阶段执行的事件函数。
  2. 2 {
  3. 3 Debug.Log("Awake"); // 控制台输出。
  4. 4 }

    • Awake: This function is always called before any Start functions and also just after a prefab
         is instantiated. (If a GameObject is inactive during start up Awake is not called until it is made active.)

 这里就是说Awake()最先调用,从图上位置也能看出它的地位。(当一个GameObject在启动的时候不是活动状态,那么Awake()也不会调用,直到它被置为活动状态。)

这里测试一下常用的事件函数以及它们的执行顺序,其它事件函数以后用到再添加进来。

  1. 1 using System.Collections;
  2. 2 using System.Collections.Generic;
  3. 3 using UnityEngine;
  4. 4
  5. 5 public class TestScript : MonoBehaviour
  6. 6 {
  7. 7 void Awake() // flowchart上是处于Initialization段也就是初始化阶段执行的事件函数。
  8. 8 {
  9. 9 Debug.Log("Awake"); // 控制台输出。
  10. 10 }
  11. 11
  12. 12 void OnEnable()
  13. 13 {
  14. 14 Debug.Log("OnEnable");
  15. 15 }
  16. 16
  17. 17 void Reset() // 当附加于一个GameObject或手动Reset时调用。(不能在PlayMode时)
  18. 18 {
  19. 19 Debug.Log("Reset");
  20. 20 }
  21. 21
  22. 22 void Start() // 程序开始时,只调用一次。
  23. 23 {
  24. 24 Debug.Log("Start");
  25. 25 }
  26. 26
  27. 27 void Update() // 字面意思,更新,但是会受实际性能影响。
  28. 28 {
  29. 29 Debug.Log("Update");
  30. 30 }
  31. 31
  32. 32 void FixedUpdate() // 更加适合物理运动的更新。
  33. 33 {
  34. 34 Debug.Log("FixedUpdate");
  35. 35 }
  36. 36
  37. 37 void LateUpdate() // 在Update()后执行的函数。
  38. 38 {
  39. 39 Debug.Log("LateUpdate");
  40. 40 }
  41. 41
  42. 42 void OnDisable() // 和OnEnable()是一对儿哒~
  43. 43 {
  44. 44 Debug.Log("OnDisable");
  45. 45 }
  46. 46
  47. 47 void OnApplicationQuit() // 程序退出时 执行顺序(OnApplication()->OnDisable()->OnDestroy());
  48. 48 {
  49. 49 Debug.Log("OnApplicationQuit");
  50. 50 }
  51. 51 /// <summary>
  52. 52 /// OnDestroy: This function is called after all frame updates for the last frame of the object’s existence
  53. 53 /// (the object might be destroyed in response to Object.Destroy or at the closure of a scene).
  54. 54 /// </summary>
  55. 55 void OnDestroy() // 销毁
  56. 56 {
  57. 57 Debug.Log("OnDestroy");
  58. 58 }
  59. 59 }

运行效果:

 

原文链接:http://www.cnblogs.com/braink-1400/p/11197238.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号