目录
print() 和 Debug.Log()、Debug.Log. Warning() 、Debug.LogErroro()、Debug.Break() 操作 ( 73P)
修改 Transform 和 Rigidbody 组件 中的属性来对游戏对象进行操作 (73P)
使用 GetComponent 方法 获取到游戏对象上的 组件 (75P)
通过 Transform.Find 来 需找 子对象 或 父对象 (76P)
通过 Transform.Find 来 获取 子对象 上的组件 (76P)
使用 GameObject.FindWithTag 和 GameObject.Find 方法 来 通过 游戏物体的名字 或 标签 来 查找对象 (77P)
使用 gameObject.SetActive 方法设置物体是否为 active
使用 GameObject.CreatePrimitive 创建内置几何体
使用 OnTriggerStay 函数 为 刚体 施加力 (77P)
使用 FindObjectOfType 和 FindObjectsOfType 函数 来查找到挂载特定类型组件的游戏对象 (78P)
使用不同的方式 初始化 Vector3 向量类型 ( 78P )
Destroy、DestroyImmediate 、Instantiate 方法 ( 80P)
使用 StartCoroutine 方法 来启动一个协同程序 (80P)
MonoBehaviour 类 中可调用的成员变量 (82P)
使用 transform.Rotate 使一个物体顺时针旋转
使用 GameObject.Instantiate 实例化Prefab
通过 GameObject. FindGameObjectWithTag 通过标签来查找游戏对象
使用 Awake 或 Start 方法初始化
必须使用 Awake 或 Start 方法 来初始化脚本的代码
两者的不同之处在于
- Awake方法是在加载场景时运行;
- Start 方法是在第一次调用 Update 或 FixedUpdate 方法之前被调用;
- Awake 方法运行在所有 Start 方法之前。
Unity脚本中协同程序有不同的语法规则
- Unity脚本中协同程序(Coroutines)必须是 IEnumerator返回类型, 并且 yield 被yield return替代
尽量避免使用构造函数
- 不要在构造函数中初始化任何变量, 要用 Awake 或 Start 方法来实现。
print() 和 Debug.Log()、Debug.Log. Warning() 、Debug.LogErroro()、Debug.Break() 操作 ( 73P)
在Unity中,可以使用 print() 和 Debug.Log() 打印调试信息。
print() 和 Debug.Log() 不同在于?
- print() 只能在 继承了 Monobehaviour 的类中使用
- 但是 Debug.Log() 可以在任何地方使用。所有一般情况下最好使用Debug.Log(),它和print()效果一样。
- 使用 Debug.Log. Warning() 可以收集警告
- 使用 Debug.LogErroro()可以收集错误信息。
- 使用 Debug.Break() 可以设置断点。如果想查看特定情况发生时 对象属性 的变化时, 用断点可以快速地完成。
修改 Transform 和 Rigidbody 组件 中的属性来对游戏对象进行操作 (73P)
使一个游戏物体旋转:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
// Update is called once per frame
void Update()
{
this.transform.Rotate(20, 0, 0); // 让该脚本所在的游戏物体 绕 x 轴旋转 20 度
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
// Update is called once per frame
void Update()
{
this.transform.Translate(0, 0, 1); // 让该脚本所在的游戏物体 每帧 向前移动1个单位长度
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
// Update is called once per frame
void Update()
{
// 第四个参数是可选的,默认情况下是 Space.Self
this.transform.Rotate(5, 0, 0, Space.World); // 该物体 相当于绕世界坐标系进行旋转
this.transform.Translate(5, 0, 0, Space.Self); // 相当于饶该物体的自身坐标系进行旋转
}
}
使用 Time 类 来记录时间 (74P)
Time 类中有一个 名为 deltaTime (只读) 的变量, 该变量指的是 从最近一次调用 Update 或者 FixedUpdate 方法到现在所经过的时间。
如果想均匀地旋转一个物体,不考虑帧速率的情况下, 可以乘以 Time.deltaTime .
public class Test : MonoBehaviour
{
// Update is called once per frame
void Update()
{
this.transform.Rotate(10 * Time.deltaTime, 0, 0); // 绕X 轴 均匀的旋转
}
}
public class Test : MonoBehaviour
{
// Update is called once per frame
void Update()
{
this.transform.Translate(0, 0, 1*Time.deltaTime); // 绕Z 轴 均匀的平移
}
}
public class Test : MonoBehaviour
{
// Update is called once per frame
void Update()
{
// this.transform.Translate(0, 5*Time.deltaTime,0);
Vector3 te = gameObject.transform.position; // 获取游戏对象的位置坐标
te.y += 5 * Time.deltaTime; // 该物体 沿 y 轴 每秒 上升 5 个单位
gameObject.transform.position = te; // 设置游戏对象的位置坐标
}
}
public class Test : MonoBehaviour
{
// Update is called once per frame
void Update()
{
// this.transform.Translate(0, 5*Time.deltaTime,0);
//gameObject.GetComponent: 如果游戏对象有一个附加组件,则返回Type类型的组件,如果没有,则返回null。
Vector3 te = gameObject.GetComponent<Rigidbody>().transform.position;
te.y += 5 * Time.fixedDeltaTime; // 刚体 沿 y 轴 每秒 上升 5 个单位
gameObject.GetComponent<Rigidbody>().transform.position = te; // 设置刚体的位置坐标
}
}
使用 GetComponent 方法 获取到游戏对象上的 组件 (75P)
public class Test : MonoBehaviour
{
// Update is called once per frame
void Update()
{ // 下面的两段代码是等价的,都 沿着 x 轴 移动一个单位
transform.Translate(1, 0, 0); // 这句代码是 通过 Transform 组件的 transform 属性 调用 Translate 向 x 轴移动一个单位
GetComponent<Transform>().Translate(1, 0, 0); // 首先获取到 Transform 组件,然后移动
}
}
通过 Transform.Find 来 需找 子对象 或 父对象 (76P)
public class Test : MonoBehaviour
{
// Update is called once per frame
void Update()
{
transform.Find("Sphere").Translate(0, 0, 1); // 首先找到该子对象,然后 向 Z 轴每帧移动一个单位
transform.parent.Translate(0, 0, 1); // 找到父对象,然后将其 沿z 轴每帧移动一个单位
}
}