1. create C# file
注意文件名要与类名相同。如果没有相符的话,当脚本在游戏中运行时会出现类定义错误(class definition error)。可以在unity3D中创建C# scripts时直接更名, 也可以后面rename(右键)。
2. error MSB3644: 找不到 .NETFramework,Version=v4.7.1 的參考組件
解决办法:下载指定版本的.NETFramework, 就算下载最新版的4.7.2也不行,必须是报错提示上的
3. 属性修改权限
以position为例, 右键找到peek->Definition,出现以下界面:
有的属性下的list中get
和set
都有,有的仅有get
。 仅有get
表示只读, 有set
的话表示可修改。
3. How Unity3D excutes codes
不同与在其他开发环境以main()
作为起始函数,在U3D中一个场景第一次开始时,将运行Start()
函数。如果我们想在每一帧重复我们的代码,那么我们将使用Update()
方法。
4. megabyte & gigabyte
When you talk about computer storage you use the word megabyte or gigabyte. The word mega refers to how many millions of bytes a component in your computer can store. Giga indicates how every many billions of bytes are being stored.
MLA (Modern Language Assoc.) Okita, Alex. Learning C# Programming with Unity 3D, Second Edition. Vol. Second edition, A K Peters/CRC Press, 2020.
5. double & float
在unity中, Vector3
由三个float类型组成,而不是double。
float floatNum = 1.0f; //32bits
double doubleNum = 1.0; //64bits
Vector3 Myvector = new Vector3(1.0, 1.0, 1.0); //error
Vector3 Myvector = new Vector3(1.0f, 1.0f, 1.0f); //correct
6. converting from a string to a number value
string s = "234834";
int i = (int) s; //ERROR
int i = int.Parse(s); //Bingo
7. #region & #endregion
#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the code editor. In longer code files, it is convenient to be able to collapse or hide one or more regions so that you can focus on the part of the file that you are currently working on. The following example shows how to define a region:
#region MyClass definition
public class MyClass
{
static void Main()
{
}
}
#endregion
A #region block must be terminated with a #endregion directive.
A #region block cannot overlap with a #if block. However, a #region block can be nested in a #if block, and a #if block can be nested in a #region block.
8. out
9. GetMouseButton()
GetMouseButton(0): left mouse button is pressed
GetMouseButton(1): right mouse button is pressed