声明数组 类型[] 数组名 = {数组值};
可以通过索引访问数组内部数据 数组名[索引数],其中的索引
声明数组 类型[] 数组名 = {数组值};
可以通过索引访问数组内部数据 数组名[索引数],其中的索引
if(判断语句){是否执行内部语句}else{不和if}
< > >= <= == !=
会常用到bool来判断结果并可以运行时输出TRUE或者FALSE
+= -=
++自增运算符
两个不同的数据类型进行运算时结果的数据类型会
bool类型表示 是或者否(取值只有true\false可以用于判断是否死亡)
char n = 'c';
string name = "";
整型 long(-2^64~2^64-1)sbyte(-128~127)int(-21亿~21亿)short(-32758~32767)byte(0~255)ushort(0~2^16-1)uint(0~2^32-1)ulong(0~2^64-1)
第一次赋值也是初始化的过程。语法错误非常严重,会影响后续开发过程。
在unity中双击错误会直接在VS当中移动到错误位置
声明变量后,初始化的变量类型无法更改。变量赋值前后类型一定要
int声明变量,变量名随机定义(不能是数字开头,字母数字下划线)
变量声明在方法内(void start / void update)只能用于该方法内,在其他地方需要重新声明
Debug.Log("");和print("");是两种输出方式
Debug.LogWarning("");Debug.LogError("");分别输出警告和错误
文件名必须和public class
数组:存储一组同类型的数据。
用法:类型[] 数组名 = {数组值}
数组引用:数组名[索引] 索引从0开始
transform.Find 查找子物体
GameObject.find 根据名字查找
FindWithTag
返回值
return res;
int res =Add(10,67);
print()
使用Vector3 pos创建一个位置参数
通过参数控制方法里变得东西 创建不同位置的敌人
CreateEnemy(new Vector3(1,1,1));
CreateEnemy(new Vector3(3,3,4));
枚举类型
int roleType=0;//0代表魔法师 1战士 2 坦克....
enum RoleType{
Mag,
Solidier,
Wizard,
}
RoleType rt = RoleType.Mag;
rt =RoleType.Solidier;
返回值 方法名 (参数){方法体}
创建方法然后调用方法
数组的的三种表达方式
运算符
数学运算符:+-*/%
赋值运算符:= += -= *= /= %=
a+=b ——> a=a+b;运算+赋值
比较运算符(bool)
> >= < <= == =!
&&逻辑与操作
|| 逻辑或操作
! 去反赋值
bool isDead=false;
print(!true);
print(!false);
print(!isDead);
if(isDead){
}
print(true&&true);
print(false&&true);
print(true&&false);
print(false&&false);
int player1hp=0
int player2hp=0
if (player1hp<=0&&player2hp<=0){
print("gameover")}
脚本中变量的定义
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player2 : MonoBehaviour {
//类里面生命的public变量都可以在控制面板上反映出来 变量值以第一次价值的脚本值为准,当脚本中的变量值与控制面板值不一致时,脚本输出以控制面板值为准
public int hp;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
`
Unity中脚本的基本结构
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
命名空间,灰色表示没有使用
使用的类必须在相应的命名空间,使用类之前必须要启用相应的命名空间。
//继承自MonoBehaviour类 void Start 和Void Update 是方法
public class C#Project : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
命名空间控制着类的使用,没有命名空间无法使用类
命名空间 类 方法 变量 对象 运算和调用