概念:变量是存储内容的别名,通过变量可以访问到内容。
为什么要使用变量?
通过变量可以映射计算机内存地址,进而获取解析相应的内存块,最后返回想要访问的数据。
变量的赋值格式:
<data type><variable_name>=value;
例: int a = 10;
char和string:
char表示一个字符,string是一个char类型的数组。
// 匈牙利命名法(驼峰式) int hpValue = 1000; int iHpValue = 2000;// i表示int // 可单行赋值多个相同数据类型的变量 int c = 30, d = 40; // string str string strMonster = '树人'; // char ch char chUnicode = 'a'; // short sh short shID = 100; // long l long lMpValue = 1000; // bool true : false b/is bool bOpen = true;
// 输出格式化,范例如下
Debug.LogFormat('名字{0},血量{1},等级{2},经验值{3}', strName, iHpValue, iLevel, IExp);
数组,是有序的相同数据类型元素组成的有限集合,内存是线性连续的。
一维数组:数组中每个元素都只带有一个下标;
二维数组:数组中每个元素带有两个下标;
………………
N维数组:数组中每个元素带有N个下标。
<data_type>[] variable_name;
int[] array; // 数组的声明,并没有分配内存 int[] array1 = new int[5]; // 数组的声明并分配内存 int iNumber = 10; // 第一种数组初始化 int[] array3 = new int[5]; for(int i = 0; i < array3.Length; i++) { array3[i] = iNumber; iNumber += 10; } Debug.Log('访问第4个元素(第三个下标)代表的数据:' + array3[3]); array3[3] = 400; Debug.Log('访问第4个元素(修正)代表的数据:' + array3[3]); // 第二种数组初始化 int[] array4 = new int[5] { 100, 200, 300, 400, 500 };// 容量显式声明 int[] array5 = new int[] { 100, 200, 300, 400, 500 };// 容量隐式声明 // 第三种数组初始化 int[] array6 = { 100, 200, 300, 400, 500 }; Debug.Log('获取array6的数组长度(容量)= ' + array6.Length); // 二维数组 int[,] array7 = new int[2, 2]; array7[0, 0] = 100;// 1行1列 array7[0, 1] = 200;// 1行2列 array7[1, 0] = 300;// 2行1列 array7[1, 1] = 400;// 2行2列 Debug.Log('打印第2行第1列数据:' + array7[1, 0]); int[,] array8 = new int[2, 2] { { 1000, 2000 }, { 3000, 4000 }, }; int[,] array9; array9 = new int[2, 3] { { 1000, 2000, 9 }, { 3000, 4000, 8 }, }; Debug.Log('array9总长度(容量)' + array9.Length); Debug.Log('array9行数' + array9.GetLength(0)); Debug.Log('array9列数' + array9.GetLength(1)); // 三维数组 int[,,] array10 = new int[3, 4, 5] { {{1, 2, 3, 4, 5 }, {6, 7, 8, 9, 10 }, {1, 2, 3, 4, 5 }, {6, 7, 8, 9, 10 } }, {{1, 2, 3, 4, 5 }, {6, 7, 8, 9, 10 }, {1, 2, 3, 4, 5 }, {6, 7, 8, 9, 10 } }, {{1, 2, 3, 4, 5 }, {6, 7, 8, 9, 10 }, {1, 2, 3, 4, 5 }, {6, 7, 8, 9, 10 } } }; Debug.Log('array10总容量:' + array10.Length); Debug.Log('array10行数:' + array10.GetLength(0)); Debug.Log('array10列数:' + array10.GetLength(1)); Debug.Log('array10层数:' + array10.GetLength(2)); Debug.Log('arry10[2, 2, 2]:' + array10[2, 2, 2]); int[,,] array11 = new int[2, 3, 4]; for(int i = 0; i < array11.GetLength(0); i++) { for(int j = 0; j < array11.GetLength(1); j++) { for(int k = 0; k < array11.GetLength(2); k++) { array11[i, j, k] = 10; } } } Debug.Log(array11[1, 1, 1]); // 数组的访问 // 1.下标的方式 // 2.循环遍历的方式
函数,即方法(接口),可以直接被另一段程序调用的程序块。
一个较大的程序一般分为若干个程序块,每一个模块用来实现一个特定的功能。
无返回值函数的定义
有返回值函数的定义
函数的调用
// 1.函数返回值的分类 // (1)无返回值 void + function_name + (args) + { } // (2)有返回值 function_type + function_name + (args) + { return function_type; } // 2.函数调用 // 注意: // 函数调用时,传入的参数的数据类型,必须和被调用的函数参数的数据类型保持一致 // 3.如何调试程序 // 断点如何添加: // (1)选中某一行,通过鼠标单击左侧竖栏,再单击一下取消 // (2)选中某一行,按下F9标记断点,再按一下取消 // 如何调试 // (1)附加到Unity,同时启动Unity // (2)F11进入函数体内,F11逐语句执行,F10逐过程执行 // (3)shift+F5退出调试 public class function : MonoBehaviour { // Start is called before the first frame update void Start() { ShowContxt(); ShowName('老王'); ShowName2('老张', 26); int result = GetSum(1, 2); Debug.Log('计算之和:' + result); } void ShowContxt() { Debug.Log('ShowContxt被调用'); } void ShowName(string strName) { Debug.Log('ShowName输出参数:' + strName); } void ShowName2(string strName, int iAge) { Debug.Log('ShowName输出参数:' + strName + ',年龄:' + iAge); } int GetSum(int a, int b) { int sum = 0; sum = a + b; return sum; } }
运算符,是一种告知编译器执行特定的数学或逻辑操作的符号。
赋值运算符:
关系运算符:
逻辑运算符:
三目运算符:
a > b ? a : b
若a>b,则返回a;反之返回b
枚举类型使用enum关键字声明,包含了一些易读的标识符。
// Enum <--> int // Enum <--> string public class EnumTest : MonoBehaviour { /* * 枚举内容: * 1. 起床 2. 洗漱 3. 吃饭 4. 玩 */ public enum emAction { /*None, // 占位符 GetUp, Wash, Eat, Play,*/ None = 0, GetUp, // 0 + 1 = 1 Wash, // 1 + 1 = 2 Eat, // 2 + 1 = 3 Play // 3 + 1 = 4 } // 初始化 public emAction mAction = emAction.Play; void Start() { Func1(); } void Func1() { Debug.Log(mAction); Debug.Log(mAction.ToString()); // Enum <--> string // mAction.ToString() Enum 转化为string // mAction = (emAction)Enum.Parse(typeof(emAction), 'Wash'); string 转化为 Enum mAction = (emAction)Enum.Parse(typeof(emAction), 'Wash'); Debug.Log('string --> Enum: ' + mAction); // Enum <--> int // int iPlay = (int)mAction; Enum 转化为 int // int iConverValue = Convert.ToInt32(mAction); Enum 转化为 int // mAction = (emAction)3; int 转化为 Enum // Enum.ToObject(typeof(emAction), 1); int 转化为 Enum int iPlay = (int)mAction; Debug.Log('Enum --> int: ' + iPlay); int iConverValue = Convert.ToInt32(mAction); Debug.Log(iConverValue); mAction = (emAction)3; Debug.Log('int --> Enum: ' + mAction); mAction = (emAction)Enum.ToObject(typeof(emAction), 1); // Enum在switch-case中的应用 // 增加了代码的可读性 switch( mAction ) { case emAction.GetUp: Debug.Log('起床'); break; case emAction.Wash: Debug.Log('洗漱'); break; case emAction.Eat: Debug.Log('吃饭'); break; case emAction.Play: Debug.Log('玩'); break; default: Debug.Log('none'); break; } } }
if{} else{}
和switch() case 0: break; case 1: break;……
for 语句
// int i = 0 定义变量 // i < length; 检测条件 // i++ 自增自减语句 for (int i = 0; i < 10; i++) { Debug.Log(i); } for (int i = 10; i > 0; i--) { Debug.Log(i); }
for-each语句
foreach是一种语法糖,用来简化对可枚举元素的遍历代码。而被遍历的类通过实现IEnumerable
接口和一个相关的IEnumerator
枚举器来实现遍历功能。
在foreach语句中有两个限制,第一不能修改枚举成员,其次不要对集合进行删除操作。也就是如下两种方式都是错误的。
// Use 'foreach' to loop an arraylist foreach( int i in arrInt ) { i++;//Can't be compiled Debug.WriteLine( i.ToString() ); } // Use 'foreach' to loop an arraylist foreach( int i in arrInt ) { arrInt.Remove( i );//It will generate error in run-time Debug.WriteLine( i.ToString() ); }
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestForeach : MonoBehaviour { void Start() { GoTestForeach(); } private void GoTestForeach() { Debug.Log('start foreach 空数组的遍历'); var array = new List<string>(); //注意区分:空数组是指元素个数为0,数组为空是指数组变量本身为null foreach(var item in array) { //这里数组array变量不为空,只是元素个数为0,在遍历时不会报空。 Debug.Log(item); } //foreach语句中不能修改枚举成员 var array2 = new List<string> { '', '1', '2', '3' }; foreach(var item in array2) { Debug.Log(item); array2.Remove('1'); Debug.Log(item); } } }
while 语句
// while 关键字 // (true) 检测条件 int i = 0; while(i < 10) { // 循环体 Debug.Log(i); i++; }
do-while 语句
int i = 10;
do
{
Debug.Log('i')
}while(i > 10);
Debug.Log('!');
循环语句知识补充:
(1)continue
跳过:跳过当前循环,进入到下个循环;
(2)break
打断:跳出所有循环,到循环体外;
(3)++i
前自增:先进行 i+1,再执行有关 i 操作;
(4)i++
后自增:先进行有关 i 的操作,再执行 i+1.
联系客服