Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

该课程来源于Unity Learn Premium,教程分步与摘要如下。

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

C# Scripting in Unity: Beyond the Basics目录

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

1. Project Overview

主要介绍了命名空间(using xxxxx);

命名规则(首字母大写小写等);

区域(#region #endregion)。

 

新概念:#region 后可跟描写该区域的词句,区域缩起后显示region里的描述语句。例:#region API 则缩起后显示API

 

2.Understanding Types

介绍了类型(type,value type值类型和reference type引用类型);

var关键词的使用(下方新概念中说明);

枚举的使用(enumerations,为值类型)(下方新概念中说明);

泛型的使用(Generic Type)和方法重载(method overloading)(下方新概念中说明);

 

新概念:

① var关键词的使用

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

②enum关键词的使用

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

③泛型<T>的使用和方法重载的说明

 

C#是强类型语言(Strongly-Typed Language)

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

而泛型则可以在使用前才定义参数类型和值,优势在于代码多次使用,替代方法重载。下面的方法重载我们在Unity里定义的各类方法里常常使用(不同的参数)。

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

用泛型代替则是

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

控制台输出

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

3.Working with Groups of Types

主要介绍了数组(Array,比如Public string[] shapes = {"circle", "triangle"}; 或 Public string[] shapes = new string[4];);

List泛型(List<T>);

队列和栈(Queue和Stack)

 

新概念:

①List与数组的区别

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

②字典的使用

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

③队列和栈的使用

队列采用先进先出,栈采用先进后出

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

4.Coroutines

即协程

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

yield后有以下几种形式

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

要注意的是WaitForSeconds()和WaitForSecondsRealtime()的区别

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

上图内Time.timeScale = 0,关于timeScale的说明参考来源

timescale是时间流逝速度的缩放比例,0.5时会降到一半,一般用于慢动作或快动作。

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

所以上面的程序执行一次后就停止了,如果换成WaitForSecondsRealtime(2)则可以继续正常进行。

 

如果不是在Update等方法内每帧执行,想要两个程序同时执行的方式是两个程序都为coroutine。

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

停止协程有StopAllCoroutine()和StopCoroutine(xxx),前者停止该Monobehaviour下的所有携程,后者仅停止该携程,StopCoroutine()的使用方法有点复杂,可以参考unity api。

 

5.Working with Classes

主要介绍了面向对象的编程(Object Oriented Programming)(method为面向对象的语言的叫法,function为面向过程的语言的叫法);

制作一个简单的打飞机游戏说明类的继承(inheritance),多样性(Polymorphism)和接口(interface)。

 

新概念:

①类的继承

之前就在2D Roguelike里学习过。脚本里的打头 public class xxx: Monobehaviour  中的Monobehaviour就是基类,xxx就是mono的派生类。roguelike里也出现过不继承mono基类继承其他基类的情况。基类中的public对基类和其他所有类可见,private仅对自己可见,protected对子类可见对其他类不可见(即保护于其他类)。详细了解类继承可参考

 

②多形性

通常使用类继承是为了减少冗余代码(比如某部分代码在a脚本和b脚本内共同使用,则把该代码写到x脚本,ab继承x),这就涉及到对父类内的方法(method)变量的改写重写(添加功能或者直接重写),这种功能叫多型性。

父类里的方法必须使用virtual或abstract关键词才能改写。

比如 public virtual void Player(){xxx}; 和public abstract void Enemy();。

如果父类的方法使用了关键词abstract则表示子类必须重写该方法({}内不能为空),父类里的abstract则不必写实现(仅有声明,不能加{}和里面的内容,就是抽象)。如果父类方法使用了virtual则必须在父类内写该方法的实现(哪怕仅有一对花括号,即????类)。

在子类内重写父类方法,该方法必须同名,必须添加override 关键词,比如public override void Player(){xxx};详细了解多形性

 

③接口

继承只能一个类继承自一个类,比如A→B→C,C继承了AB,但想要继承一个不在链条上的类,就要使用接口。接口像一个合同,用固定形式声明X接口,并使A类内的方法可直接使用X内的内容(方法method,属性properties,事件events,索引器indexers)

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

Beginner Programming: Unity Game Dev Course(1)- Beyond the Basics

 

在之前brakers的装备系统学习时已经学习使用过例如IPointEnter之类的接口使用方式。