The basic knowledge of unity scripts and the life cycle of scripts

/*Script is the instruction code attached to the game body to define the behavior of the game object

  • Unity supports three high-level programming languages: c#, Javascript
  • grammar structure
  • using namespace;
  • public class 类名:MonoBehaviour
  • {
  •  void 方法名()
    
  •  {
    
  •  }
    
  • }
  • The file name and the class name must be consistent
  • The written script must be attached to the object to be executed
  • The script class attached to the game object must inherit from the MonoBehaviour class
    * The object is just the object of the class
  • Compilation process:
  • Source code (CLS)->Intermediate language (dll file in unity)->(Mono Runtime)->Machine code
  • development tools
  • MomoDevolop
  • Unity comes with a script compiler to create Mono applications, suitable for integrated development environments for Linux, Mac OS X and windows, and supports c#, BOO and Java script, etc.
  • High-level programming language
  • VS
  • Microsoft's development tools include most of the tools needed in the entire software life cycle
  • Script life cycle: the whole process of unity script from wake-up to destruction
  • The initial phase
  • Awake Awakening:
  • 当物体载入时立即调用一次:常用于在游戏开始前进行初始化,可以判断当满足某种条件执行此脚本 this。enable=true;
    
  • OnEnable when available:
  •  每当脚本对象启用时调用。
    
  • Start:
  •  物体载入且脚本对象启用时被调用有一次。常用于数据或游戏逻辑初始化,执行时机晚于  Awake。
    
  • Physical stage
  • FixUpdate fixed update:
  •  脚本启用后,固定时间被调用,适用于对游戏对象做物理操作,例如移动等
    
  •  设置更新频率;"Edit"-->"Project Setting"-->"Time"-->"FixUpdate"值,默认为0.02s
    
  • Update:
  •   脚本启用后,每帧渲染场景时调用,频率与设备性能和渲染量有关
    
  • LateUpdate delayed update:
  •   在Upfate函数被调用后执行,适用于跟随逻辑
    
  • Input event
  • OnMouseEnter mouse move in:
  •      鼠标一道当前Collider时调用。
    
  • OnMouseOver mouse over:
  •      鼠标经过当前Collider时调用
    
  • OnMouseExit mouse away:
  •      鼠标离开当前Collider时调用
    
  • OnMouseDown mouse down:
  •      鼠标按下时被调用
    
  • OnMouseUp mouse up:
  •      鼠标在当前Collider上抬起时调用
    
  • Scene rendering
  • OnBecameVisible when visible:
  •      当Mesh Renderer在任何相机上可见时调用
    
  • OnBecameInvisible when invisible:
  •      当MeshRenderer在任何相机上都不可见时调用
    

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
///Script Lifecycle
///
public class Lifecycle: MonoBehaviour
{ //Script: .cs text file class file // attached to game objects Medium //c# class: //Field //Properties (not written in the general script, cannot be displayed in the panel) //Constructor //Method [SerializeField]//Serialize the field, display the private variable private int in the compiler a=100; [HideInInspector]//Function: hide the field in the compiler public float b = 0; //public Lifecycle() //{//Do not write the constructor in the script // //Cannot be in the child thread Access to the main thread member // float c = time.time; //} public Lifecycle() { Debug.Log("Constructor"); } // Initial stage




















//
private void Awake()//Execution timing: create a game object (the script may not be enabled at this time) Immediately start execution and execute only once Function: Initialize
{ //Debug.Log("Awake–" + Time.time+"–" +this.name); } private void Start()//When the Awake function is executed: create game object -> script enable -> execute only once. Function: initialize { int a = 1;//input code during debugging : Int b = 2;//Right-click – Quickly monitor int c = a + b;//View "instant window" Debug.Log("start–" + Time.time+"–"+this.name); } // The physical phase









*//
private void FixedUpdate()// is executed every fixed time. (Time can be modified) Applicability: Suitable for physical manipulation of game objects (moving, rotating...) will not be affected by rendering time
{//Rendering time is not fixed (the amount of rendering per frame is different, and the machine performance is different)
//Debug.Log (Time.time);
}
private void OnMouseDown()
{ Debug.Log(" OnMouseDown"); }


private void Update()//Execution timing: rendering frame execution, execution interval is not fixed Applicability: processing game logic
{ //Single frame debugging: start debugging, run scene, pause game plus breakpoint, single frame run end debugging } }


Guess you like

Origin blog.csdn.net/weixin_51235620/article/details/115058857