脚本中
播放模式下的游戏状态不可保存,修改了之后不会被保存
Update():帧率每更新一次就调用一次,里面的函数也是
Debug.Log("Time = " + Time.time); //获取从 (游戏启动到现在经过了多长时间)
Debug.Log("deleTime = " + Time.deltaTime); //获取每一帧的时间
GameObject obj = this.gameObject; //获取所绑定物体
Vector3 pos = this.transform.localPosition; //获取位置
Vector3 angles = this.transform.localEulerAngles; //获取角度
this.transform.Translate(0, 0, 0, Space.Self); //修改位置
this.transform.Rotate(0,0,0,Space.Self); //修改旋转角度
transform.parent //找到父物体的transform
公转————创建父子关系物体来转动
理解:每一个节点(物体)都是一个实例对象,节点下的挂的每一个组件也都是实例对象。
相当于物体是一个对象,所有组件也都是物体中的对象属性。脚本也是一个对象属性(可以获取其他对象属性(组件))
this 表示的 是 该脚本所绑定物体
消息函数
1.Awake() :脚本被禁用也可以被调用, 但start()不可以 ,两者都只会执行一次
2.OnEnable: 脚本启用时调用
OnDisable : 脚本禁用时调用
函数执行顺序
先执行每一个脚本的Awake(),再执行每一个脚本的Start(),再执行update()。
主控脚本
创建空节点挂上主控脚本(可以提高优先级),用来做一些全局化的设置,例如设置帧率啥的
脚本参数
值类型:
在函数中用public 定义(定义出的是默认值),可以在unity 检查器中修改
注意:参数变量名:第一个单词小写第二个大写
注意: 参数先是默认值——
注解提示:
引用类型
节点,
组件,
资源,
数组类型
运行时调试
调试时 右键 脚本,“Copy component” ,结束调试后 “Paste component Values”
键鼠操作
移动代码
Vector3 direction = this.transform.localPosition;
// 水平偏移量
float InputX = Input.GetAxis("Horizontal");
// 垂直偏移量
float InputZ = Input.GetAxis("Vertical");
// 计算移动距离并修改物体位置
this.transform.Translate(InputX*Time.deltaTime*speed,0, InputZ * Time.deltaTime * speed, Space.Self);
// 键盘
Input.GetKey(KeyCode.W) // 按住 W 键持续返回 true, 松开后返回 false
Input.GetKeyDown(KeyCode.W) // 按下 W 键返回 true,按住也只会返回 false
Input.GetKeyUp(KeyCode.W) // 抬起 W 键返回 true
// 注:这里的 Jump 在 Input Manager 里默认是空格键
Input.GetButton("Jump") // 按住自定义的键持续返回 true, 松开后返回 false
Input.GetButtonDown("Jump") // 按下自定义的键返回 true,按住也只会返回 false
Input.GetButtonUp("Jump") // 抬起自定义的键返回 true
Input.GetAxis("Horizontal") // AD 或左右方向键的偏移量 (从 0 到 1 变化,或从 0 到 -1 变化)
Input.GetAxis("Vertical") // WS 或上下方向键的偏移量 (从 0 到 1 变化,或从 0 到 -1 变化)
Input.GetAxisRaw("Horizontal") // AD 或左右方向键的偏移量 (-1 0 1)
Input.GetAxisRaw("Vertical") // WS 或上下方向键的偏移量 (-1 0 1)
// 鼠标 0: 左键 1: 右键 2: 中键
Input.GetMouseButton(0) // 按住鼠标左键
Input.GetMouseButtonDown(1) // 按下鼠标右键
Input.GetMouseButtonUp(2) // 抬起鼠标中键
Input.GetAxis("Mouse X") // 鼠标水平方向移动的偏移量
Input.GetAxis("Mouse Y") // 鼠标垂直方向移动的偏移量
audioSource = this.GetComponent<AudioSource>(); //获取音频组件
audioSource.Play(); //播放
示例:
if (audioSource.isPlaying)
{
Debug.Log("停止播放");
audioSource.Pause(); //暂停播放
return;
}
if (!audioSource.isPlaying)
{
Debug.Log("开始播放");
audioSource.Play(); //开始播放
return;
}
组件中的参数
先获取组件,再调用组件的属性
audioSource = this.GetComponent<AudioSource>(); //获取组件
audioSource.loop = true; //设置组件属性
获取其他物体下的组件
一个方法:在脚本中创建一个引用物体变量,用检查器导入
GameObject audioObj = GameObject.FindWithTag("music"); //通过标签获取物体节点
AudioSource audio = audioObj.GetComponent<AudioSource>(); //获取到了音频组件
获取父transform ,父节点
注意:获取子节点,父节点,基本用transform,,Gameobject 存在感太低
获取父节点
parent = this.transform.parent; //获取到的父transform
parentNode = this.transform.parent.gameObject; //通过父transform获取到的节点
获取子节点
1.遍历获取
foreach (Transform child in transform) //便利获取
{
Debug.Log(child.name);
}
2.索引获取
Transform child0 = this.transform.GetChild(0); //参数为索引
Debug.Log("孩子0:"+child0.name);
3.名字获取
注意:若是二级路径,则需要添加路径
Transform child0 = this.transform.Find("Plane/bb");
设置父节点
GameObject gameObject = GameObject.Find("Capsule"); //寻找需要设置为父物体的节点
this.transform.SetParent(gameObject.transform); //将Capsule设置为父物体,!!参数为transform
this.transform.SetParent(null); //设置为一级节点
设置物体隐藏/显示
Transform Planetf = this.transform.Find("Plane"); //寻找子节点
if (Planetf.gameObject.activeSelf) //判断是否是激活状态
{
Debug.Log("隐藏");
Planetf.gameObject.SetActive(false); //设置为隐藏状态
return;
}
else
{
Debug.Log("显示");
Planetf.gameObject.SetActive(true); //设置为激活状态
return;
}
资源的使用
资源数组:可以在脚本中定义一个AudioClip 变量数组,可播放多种不同的音频
注意:是在检查器中 将资源放在脚本中的属性中,而不是直接放在组件的属性中!!!!
public AudioClip[] Audioclip; //定义音频变量数组
AudioSource audioSource = this.GetComponent<AudioSource>();
audioSource.PlayOneShot(Audiocli[0]); //播放索引为0的音频
ps:随机数
index = Random.Range(min,max); //不含max
UnityEngine.Random.value; 随机获得一个0~1之间的数
延时方法
注意:函数名是字符串
Invoke("Play",2); //延时两秒再调用Play()
InvokeRepeating("Play",2,1); //等待2s后首次调用Play(),之后每一秒执行一次Play()
IsInvoking(func) //判断func是否在Invoke调用队列
Cancellnvoke(func) 取消func的Invoke 调用
Cancellnvoke() //取消脚本的所有Invoke调用
向量
注意:前面是Z ,上面是Y,右边是X
向量可以标准化 : v.nomalized
向量长度:v.magnitude
向量之间可以直接相加
点击监听
ReturnButton.onClick.AddListener(ReturnGame);
附加点:
三元运算符: ?<值1>:<值2> 若bool为真,则选值1,若bool为假,择选值2
数学函数:
Mathf = math float类型
1.Mathf.Lerp(float a,float b,float t)
线性插值 t=0时候取a,t=1时候取b,其他的时候按照比例取a,b之间的数字| a和b之间,百分之t的位置
注意:三维空间中的是Vector3.Lerp,可以制作非线性动画
2.Mathf.Min(float a,float b);
取最小
3.Mathf.Clamp(float value,float min,float max)
限制 value的值在min,max之间,如果value大于max,
则返回max,如果value小于min,则返回min,否者返回value;