📅  最后修改于: 2021-01-11 13:43:01             🧑  作者: Mango
数据类型对各种类型的数据进行分类,例如,字符串,整数,布尔值,浮点数以及该数据类型的可接受值的类型,可以对该数据类型执行的操作,该类型数据的含义可以被存储。
int:此数据类型用于存储32位整数。此数据类型存储正整数或负整数。
float: float数据类型用于存储浮点数。浮点数据类型是Unity中数字的默认类型。
例如:
float points = 72.24;
double: double数据类型还存储浮点数,但是它可以容纳比float大的大小数。它不是Unity中的默认数字类型。
例如:
double amount = 134434.23;
bool: bool数据类型是布尔的缩写形式。此数据类型存储true或false值。
例如:
bool chance = false;
char:此数据类型存储单个字符,例如数字,字母,空格或特殊字符。一个char值应该用单引号引起来。
例如:
char press = 'a';
字符串:此数据类型用于以单词或句子的形式存储字母,数字和其他特殊字符。字符串值应写在双引号内。
例如:
string CharacterName = "Ken Fighter";
让我们看一个简单的例子:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DataType: MonoBehaviour
{
int playerHealth = 100;
float playerVelocity = 14.74f;
string msg = "Welcome to the Game";
bool isGameOver = false;
void Start()
{
Debug.Log("Initial Health : " + playerHealth);
Debug.Log("Player's Volecity : " + playerVelocity + " km/h");
Debug.Log(msg);
Debug.Log("Game Over: " + isGameOver);
}
// Update is called once per frame
void Update()
{
}
}
输出: