Unity script basic learning (unfinished todo)

1. MonoBehaviour class and its life cycle

1.1 Create script

After creating a new script file, there will be a default code in the file:

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

public class test : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }
}

It derives from the base class MonoBehaviour to ensure that this script will run in the game loop, with the added ability to react to certain events.

1.2 Life cycle

  • When the script is running, unity will call the following methods in sequence according to the predetermined order to complete the life cycle:
    [The order of all scripts is parallel]

insert image description here
insert image description here
insert image description here

  • Initialization:
    Awake() : Executed only once, called once when the game object is created (regardless of script activation), often used to initialize
    OnEnable() : Can be called multiple times, when the component sets enable = true, the game object sets active = true or Call
    Start() when the script is activated for the first time : Execute only once, when the script is activated and initialized (before the first Update), it is related to the component
  • Run:
    FixedUpdate() : fixed time update, default 50fps, the default time is set by Fixed Timestep in the Edit->Project Settings->Time panel (updates related to the physics engine are here) OnTrigger...(): trigger,
    and collision Mutual exclusion, called when Collider or Rigidbody touches "Enter means that it is executed once at the moment of contact; Stay means that the object is always in contact and continues to execute; Exit means that the object is separated and executed once." OnCollision...(): Physical collision, mutually exclusive with trigger, Collider
    or Call
    Update() when the Rigidbody touches : When the game is running and the script is enabled, it is triggered once per frame (the time is not fixed, updates that have nothing to do with the physics engine are here) [ Because the frame rate of different devices is different, such as character movement, etc. need to be multiplied by Time .deltaTime----the time increment of each frame]
    LateUpdate() : Execute every frame, after other updates are executed. (Generally used for command script execution, such as camera following after the object moves position)
  • End: OnApplicationQuit(): OnDisable()
    is called when the application exits : Called at the same time when OnApplicationQuit is called, and the behavior is disabled enable = false, the object is destroyed when active = false (from activation to disable, always disabled) call, load Call OnEnable OnDestroy() after completion : Called when OnApplicationQuit is called, and is called when Destroy game object

1.3 The difference between MonoBehaviour class and ordinary class

  1. The MonoBehaviour class does not need to create an instance or new, and unity will automatically complete it. But it can be replaced by adding components.
  2. Invoke, Coroutine, print, and lifecycle functions can only be used if MonoBehaviour is inherited
  3. Only inherited MonoBehaviour can be seen on the panel and hung on the object

2. Panel expansion

2.1 Inspector panel parameters

Inspector panel parameters are similar to shader:

  1. [Header(" ")]----General title
  2. [Tooltip(" ")]----comment the next line next to it, which appears when the mouse is placed on the variable of the next line in unity
  3. [HideInInspector]----public variables that do not want to appear on the panel
  4. [SerializeField]----private variables that can appear on the panel
  5. [Range( , )]----Indicates the range of the slider for the next row of variables
  6. [Serializable]----indicates that the declared class or structure is displayed in a folded manner on the unity panel
  7. [System.Serializable]----custom data types in unity cannot be displayed in the inspectior panel, you need to add [System.Serializable] to display custom data or structures

2.2 EditorWindows drop-down menu

[MenuItem("Tools/菜单选项")]
public static void ToolsTest()
{
    
    
    类名 window = (类名)EditorWindow.GetWindow(typeof(类名));
    window.Show();
}
void OnGUI()
{
    
    
}

Add list elements via AddItem()

2.3 Assets folder right click creat menu

// 在 Assets 下添加菜单
// fileName 生成名为 Bullet的脚本
// menuName 菜单按钮名New Bullet
// order    按钮显示顺序
[CreateAssetMenu(fileName = "Bullet", menuName = "New Bullet", order = 1)]
public class Bullet: ScriptableObject {
    
    
}

1,Unity’s documentation on MonoBehaviours

Guess you like

Origin blog.csdn.net/yx314636922/article/details/126872839