20 Unity 滚动的小球
用一个项目零基础熟悉 Unity 引擎的基本操作。 开发之旅任重道远,这只是个微不足道开头,最终目标是做出自己的项目来。
个人学习记录,非系统学习,仅记录了对自己有用的东西。
参考课程:b 站 siki 学院 视频链接:https://www.bilibili.com/video/BV15N41177JS/
Unity 中的 Update 方法
查看帧率(即一秒执行多少次 Update 函数)
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4
5public class Player : MonoBehaviour
6{
7 // Start is called before the first frame update
8 void Start()
9 {
10 Debug.Log("Start运行了"); // 日志
11 }
12
13 private float updateCount = 0f;
14 private float timer = 0f;
15 private float updateRate = 0f;
16
17 // Update is called once per frame
18 // 每帧执行一次(flush),具体的帧率看电脑性能
19 void Update()
20 {
21 updateCount++;
22 timer += Time.deltaTime;
23
24 if (timer >= 1f)
25 {
26 updateRate = updateCount / timer;
27 updateCount = 0f;
28 timer = 0f;
29 }
30 }
31
32 void OnGUI()
33 {
34 // 每秒调用多少次
35 GUI.Label(new Rect(10, 10, 200, 20), "Update Rate:" + updateRate.ToString("F2") + "updates/s");
36 }
37}
键盘控制
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4
5public class Player : MonoBehaviour
6{
7 public Rigidbody rd; // 定义刚体属性
8
9 // Start is called before the first frame update
10 private void Start()
11 {
12 Debug.Log("Start运行了"); // 日志
13 }
14
15 // Update is called once per frame
16 // 每帧执行一次(flush),具体的帧率看电脑性能
17 private void Update()
18 {
19 float h = Input.GetAxisRaw("Horizontal");
20 float v = Input.GetAxisRaw("Vertical");
21
22 Vector3 dir = new Vector3(h, 0, v);
23 rd.AddForce(dir * 3);
24 }
25}