using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log("唤醒");
    }
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("start");
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("update");
    }

    private void OnEnable()  //**的时候被调用
    {
        Debug.Log("对象**");
    }
    private void OnDisable()
    {
        Debug.Log("对象休眠");
    }
    private void OnDestroy()
    {
        Debug.Log("对象销毁");
    }
}

可以看到输出如下:
Unity c# 脚本的生命周期

awake

相当于是构造函数,gameobject在构造的时候就会调用。

onEnable onDisable

对象在**的时候和休眠的时候被调用
onEnable在awake之后,但是在第一帧之前被调用

start

在对象**之后调用

update

不停的进行循环

onDestroy

对象销毁的时候进行调用

相关文章:

  • 2021-08-31
  • 2021-10-16
  • 2021-10-17
  • 2021-11-08
  • 2021-05-11
  • 2022-12-23
  • 2021-06-03
猜你喜欢
  • 2021-09-22
  • 2021-05-28
  • 2021-12-27
  • 2022-12-23
相关资源
相似解决方案