Unity 脚本

在 Project-Assets 中,右击-创建-脚本。或为物体添加一个自定义名称的组件(就是脚本)。

Visual Studio

  • 将VS的快捷键改成同VSCode:找到工具-->选项-->键盘-->映射方案 --> vscode
  • 先在VS中点击运行,再在Unity中点击运行,即可进行断点调试等。

生命周期

Awake

当前脚本实例创建时调用,类似构造函数
此时已经可以执行GameObject.FindGetComponent等操作

Start

启用脚本实例后,第一次帧更新之前,调用该方法
因此,在Update中通过脚本创建物体时,该物体的Start方法不会在当前帧调用

Update

每一帧都会调用。受Application.targetFrameRate影响,不受Time.timeScale影响。

LateUpdate

会在所有物体的Update都完成后触发。通常用于移动相机跟随角色逻辑。

FixedUpdate
  • Unity会尝试以固定帧率(Edit->ProjectSetting->time)调用该函数。当某次FixedUpdate被阻塞而错过调用时,Unity会在之后额外调用以弥补该次。因此在 FixedUpdate 内应用运动计算时,无需将值乘以 Time.deltaTime(可乘以固定值Time.fixedDeltaTime)。
  • 两次Update间可能会存在多次FixedUpdate,也可能一次都不存在
  • 通常在该回调中进行物理模拟相关逻辑。
OnEnable

当前脚本所属的GameObject每次被激活时调用(gameObject.SetActive(true)

OnDisable

当前脚本所属的GameObject每次失活时调用(gameObject.SetActive(false)

OnDestroy

对象被销毁时被调用(依附的GameObject对象被删除时)


获取内容

获取GameObject

在脚本中声明public变量并在Script组件中手动对其赋值为目标GameObject,或通过方法查找对象:

public GameObject ball;//手动赋值
GameObject.Find("KimBall");
GameObject.FindGameObjectWithTag("KimBall");

注意,该方法无法查到被隐藏的对象(即在Hierarchy中为灰色的,isActive=false的对象)。此时可通过其父元素的transform.Find进行查找:

ParentGameObject.transform.Find("Son Name").gameObject;
获取组件

在脚本中声明public变量并在UnityEditor中对其赋值为目标组件,或通过方法/属性获取组件:

public Transform ballTransform;//手动赋值
ball.transform;
ball.GetComponent<Transform>();//应在Start而非Update中使用,防止性能浪费
ball.GetComponent("Transform");

//对于自身组件
transform;
GetComponent<Transform>();
GetComponent("Transform");

其中GetComponent会返回第一个符合条件的组件,如果存在多个同类型组件,可以使用GetComponents以返回组件列表。

引用其他脚本内容
  • 通过GetComponent获取对应脚本实例
ScoreController scoreController = GameObject.Find("Text").GetComponent<ScoreController>();
scoreController.DoSomething();
  • 或使用单例模式
//在脚本UIHealthBarController中
public class UIHealthBarController: MonoBehaviour
{
  public static UIHealthBarController instance { get; private set; }
  private void Awake()
  {
    instance = this;
  }
}

//在其他脚本中进行引用
UIHealthBar.instance


物体控制

控制物体显隐
  • 创建物体
    通常不直接通过脚本创建物体,而是通过复制预制体的方式实现。
    也可创建一个空物体,充当新物体的父级:
GameObject newParent = new GameObject("newParent");
GameObject newObj = Instantiate(bulletPrefab, newParent.transform);
  • 复制物体
    注意:预制体也属于 GameObject。
Instantiate(Object original);
Instantiate(Object original, Transform parent);
Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
Instantiate(Object original, Vector3 position, Quaternion rotation);
Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
  • 销毁物体
Destroy(gameObject);
  • 物体显隐
gameObject.SetActive(true);
gameObject.SetActive(false);
控制物体移动

物体的坐标是一个三维向量

Vector3 position = new Vector3(0f, 0f, 10f); 
  • 直接控制物体的transform组件
    • 缺点:刚体碰撞时,该位移和系统计算的刚体碰撞受力位移可能会冲突,造成物体碰撞时抖动
transform.position = position;//绝对坐标
transform.localPosition = position;//相对于父物体的坐标
  • 控制刚体组件移动(MovePosition
    如在其他地方调用,则会等到下一次FixedUpdate才执行。且连续调用多次只有最后一个生效。因此建议在FixedUpdate中调用。
    • 缺点:速度较快时,可能会直接越过刚体边界,造成穿模。此时可修改主动运动方的刚体组件Collision Detection属性为Continuous解决,但会影响性能。
      此外该方法必须同时设置x、y两个值,容易和其他物理系统冲突。
private Rigidbody2D rigidbody2D;
rigidbody2D = GetComponent<Rigidbody2D>();
rigidbody2D.MovePosition(position);
  • 控制刚体组件受力
public Rigidbody rd;
Vector3 dir = new Vector3(h, j*10, v);
rd.AddForce(dir);
  • 控制刚体组件速度
GetComponent<Rigidbody>().velocity = transform.forward * 100000f;
控制物体旋转角度

脚本中,Transform 和 Rigidbody 的rotation均不是Vector3(欧拉角),而是Quaternion(四元数),以避免万向锁问题。
以下以 Transform 的旋转举例,Rigidbody 的旋转同理:

  • 四元数转欧拉角:
    eulerAngles为Quaternion类的一个Get方法返回值
Vector3 r = transform.rotation.eulerAngles;
  • 欧拉角转四元数:
Quaternion rotation = Quaternion.Euler(new Vector3(0f, 30f, 0f));
  • transform.rotation
    transform.localRotation
    直接获取或设置物体的四元数角
Vector3 r = transform.rotation.eulerAngles;
Quaternion rotation = Quaternion.Euler(r);
transform.rotation = rotation;
  • transform.eulerAngles
    transform.localEulerAngles
    直接获取或设置物体的欧拉角

  • transform.Rotate()
    设置角度在当前基础上的偏移值
    当xyz轴均有值时,按z、y、x顺序依次发生旋转。
    默认围绕自己坐标系旋转,可改为Space.World围绕绝对坐标系旋转。

public void Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self);
public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);
  • Quaternion.identity 默认的无旋转状态四元数

计时器

通过时间自行实现

每隔一秒做某件事:

private float delayTime = 1;
private void Update()
{
    if(Time.time >= delayTime)
    {
        //你要做的事......
        delayTime += Time.time;
    }
}
private float timer = 0;
private float delayTime = 1;
private void Update()
{
    timer += Time.deltaTime;
    if(timer >= delayTime)
    {
        //你要做的事......
        timer = 0;
    }
}

Invoke(string methodName,float time)

等待time秒后执行方法。其中函数名可以通过nameof(函数)获取。

InvokeRepeating (string methodName, float time, float repeatRate);

等待time秒后执行方法,之后每间隔 repeatRate 再次执行。

CancelInvoke(string methodName)

若传入参数,则停止指定函数名在当前脚本中的所有Invoke和InvokeRepeating方法。否则停止当前脚本中所有Invoke和InvokeRepeating方法。


其他方法

  • magnitude Vector3 向量的长度
transform.position.magnitude 

射线检测

创建一条射线,并判断射线是否与指定layer有碰撞,并找到碰撞物

void Update()
{
  // 参数依次为起点、方向、长度、layer
  RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up,   lookDirection, 1.5f, LayerMask.GetMask("NPC"))
  if (hit.collider != null){
    hit.collider.gameObject
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容