• 凉鞋的 Unity 笔记 203. 变量的常用类型


    203. 变量的常用类型

    在上一篇,我们对变量进行了概述和简介,知识地图如下:

    image-20231003164039775

    我们已经接触了变量的字符串类型,以及一些功能。

    在这一篇,我们尝试多接触一些变量的类型。

    首先是整数类型

    整数类型

    整数类型一般是 int 类型,我们来看下如何使用,代码如下:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class FirstGameObject : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            var textToPrint = 5;
            
            print(textToPrint); 
            print(textToPrint); 
            print(textToPrint); 
            print(textToPrint); 
            print(textToPrint);
    
            textToPrint = 0;
            
            print(textToPrint); 
            print(textToPrint); 
            print(textToPrint); 
            print(textToPrint); 
            print(textToPrint); 
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    运行之后,结果如下:

    image-20231005132807363

    我们当然也可以指定整个变量为 int 类型,代码如下:

    int textToPrint = 5;
    
    • 1

    整数类型(一般叫整型),在游戏开发中使用的频率非常高,比如角色的等级、经验值、金币都是使用整型进行记录和存储。

    实数类型

    实数类型就是带有小数点的类型,比如 0.1,0.0,5.5 都是实数类型。

    在 C# 有两种类型,一种是比较常用的 float,另一种是精度比较高的 double。

    我们写一些代码测试一下,如下:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class FirstGameObject : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            var textToPrint = 0.1f; // float 类型
            
            print(textToPrint); 
            
            textToPrint = 0; // 可以接收 int 类型
            
            print(textToPrint);
    
            var textToPrint2 = 0.1; // double 类型
            
            print(textToPrint2);
    
            textToPrint2 = 1; // 同样可以接收 int 类型
            
            print(textToPrint2);
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    运行之后,结果如下:

    image-20231005133610007

    float 类型在游戏开发中使用的频率也非常高,比如角色的血量条、一些数值、比如暴击率,都是 float 类型的。

    float/double 同样可以指定类型,代码如下:

    float textToPrint = 0.1f;
    double textToPrint2 = 0.1;
    
    • 1
    • 2

    逻辑真假类型

    逻辑真假类型叫做布尔类型(bool 类型或 boolean 类型)。

    布尔类型只有两个值,true 或 false,即真和假。

    我们先写代码测试下:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class FirstGameObject : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            bool textToPrint = true;
            
            print(textToPrint); 
            
            textToPrint = false;
            
            print(textToPrint);
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    运行之后结果如下:

    image-20231005134054457

    虽然 bool 变量只能存储 true 和 false,但是 bool 变量的使用频率不输 int 和 float。

    这是因为 bool 变量常常与程序语言中的条件判断结合使用。

    自定义类型

    我们还剩下自定义类型,自定义类型其实有点复杂。

    自定义类型有枚举、类、结构体、委托等,每一个都可以花一整个篇幅介绍,所以这部分后续再介绍。

    好了到此变量的三种使用频率非常高的类型就介绍完了。

    这一篇的内容就这些,我们下一篇再见,拜拜。

    知识地图

    image-20231005134432053

    更多内容

    更新期间半价,保持 60% 的内容免费更新到此平台
    版权所有 GamePix 独立游戏学院
    转载请注明凉鞋的笔记

  • 相关阅读:
    经典算法-----农夫过河问题(深度优先搜索)
    Mac下通过nvm管理node
    [附源码]java毕业设计st音乐网站论文
    软件设计不是CRUD(15):低耦合模块设计理论——行为抽象与设计模式(中)
    端口被占用怎么解决
    Springboot导出mysql数据到Excel表
    sentinel介绍和使用
    QTableView通过setColumnWidth设置了列宽无效的问题
    基于ssm流浪动物救助管理系统
    记录一些Linux的指令(持续更新)
  • 原文地址:https://blog.csdn.net/u010125551/article/details/133952804