Common script events:
Update: each frame is called once.
Start: called before the first update operation
Awake: called when a script instance is created
Fixedupdate: calls each fixed physical time interval
Lateupdate: Call each frame once, after update
Monobehaviour class:
The monobehaviour class is a very important class in uniyt and defines basic script behavior. All scripts must be inherited directly or indirectly from it. Monobehaviour also defines the response functions for various specific events. These function names all start with on. Below are some common event response functions.
Onmouseenter: called when the mouse is moved into the GUI control or collision body
Onmouseover: hover your mouse over ~~~
Onmouseexit: remove the mouse ~~~
Onmousedown: press the mouse ~~~~
Onmouseup: Release the mouse ~~~
Ontriggerenter: when other collision bodiesEnterTrigger call
Ontriggerexit :~~~~~~~~Leave~~~~~~~~
Ontriggerstay :~~~~~~~Stay~~~~~~~~~
Oncollisionenter: When the collision body or rigid body and other collision bodies or rigid bodiesContact TimeCall
Oncollisionexit :~~~~~~~~~~~~~~~~~~~~~~~~When leaving~~~
Oncollisionstay :~~~~~~~~~~~~~~~~~~~~~~~~Keep in touch~~~
Oncontrollercolliderhit: called when the controller is moved and the collision body is collided
Onbecamevisible: called when visible to any camera
Onbecameinvisible: called when any camera is invisible
Onenable: called when an object is enabled or activated
Ondisable: called when the object is disabled or deactivated
Ondestory: called when the script is destroyed
Ongui: called when rendering GUI and processing GUI messages
Interaction between scripts and components:
An object consists of several components. You usually need to use scripts to access various components of an object and set component parameters. For common built-in components of the system, Unity provides a very convenient access method. You only need to access the member variables corresponding to the component in the script, these member variables are defined in monobehaviour and inherited by the script.
Common components and corresponding variables are as follows.
| Component name |
Variable name |
Component functions |
| Tranform |
Tranform |
Set the object location, rotation, scaling |
| Rigidbody |
Rigidbody |
Set the physical engine's rigid body attributes |
| Renderer |
Renderer |
Rendering Object Model |
| Light |
Light |
Set lighting Properties |
| Camera |
Camera |
Set camera Properties |
| Collider |
Collider |
Set collision body attributes |
| Animation |
Animation |
Set animation Properties |
| Audio |
Audio |
Set sound attributes
|
Note: If a component does not exist in the object, null is returned.
Access components not listed in the trademark, or scripts on the Access Object (scripts belong to custom components), and obtain component references through the following method:
Getcomponent: Get the component
Getcomponents: Get the component list
Getcomponentlnchildren: Get the component of an object or a child object.
Getcomponentsinchildren: obtains the component list of an object or a child object.
Note: These functions are time-consuming, so they are generally not used in the update function. You should set an external object that accepts component references, use the getcomponent function in the Start function to obtain component references.
Example: there is a move. CS script in a cube, and the Tranform component of the cube is obtained in the move. CS script.
Tranform c_tranform; move;
Void start () {// method 1 c_tranform = Tranform; // method 2 c_tranform = getcomponent <transform> ();
Move = getcomponent <move> ();} void update {}
Objects in the access scenario:
In unity3d development, the script not only needs to access the component of the object where the script is located, but also frequently needs to access other objects:
1. Search by name: Use the function gameobject. FindIf an object with the specified name exists in the scenario, the reference of the object is returned. Otherwise, null is returned. If multiple objects with the same name exist, the reference of the first object is returned.
Gameobject cube = gameobject. Find ("mycube ");
2. Search for: gameobject. findwithtag,If an object with the specified tag exists in the scenario, the reference of the object is returned. Otherwise, null is returned. If multiple objects use the same tag, the reference of the first object is returned.
Note: Like getcomponent, gameobject. find and gameobject. findwithtag is also a time-consuming parameter, so it is not recommended to call them in the update function, but to find them once during initialization and save them in the variable.
Coroutine ):
Coroutine also becomesCollaboration ProgramOrCoroutine, Can run in parallel with the main program, and is similar to multithreading, but at any time of formulation there will be only one collaborative program running, other collaborative programs will be suspended. It can be used to enable a program to continue running after a period of time.
The coroutine functions are as follows:
- Startcoroutine: Start a collaboration program
- Stopcoroutine: Stop a collaboration program
- Stopallcoroutine: Terminate all collaborators
- Waitforseconds: wait several seconds.
- Waitforfixedupdate: Wait until you know the next fiexdupdate call.
Javascript example:
Function start () {print ("starting" + time. time); yield waitandprint (); // enable a collaborative program print ("done" + time. time)} function waitandprint () {yield witforseconds (5); // wait for 5 S print ("waitandprint" + time. time )}
C # example:
Ienumerator start () {debug. log ("Wait 5 S" + time. time); yield return startcoroutine (waitmethod ();} ienumerator waitmethod () {yield return New waitforseconds (5f); debug. log ("waiting for 5 S" + time. time );}
Note that the return type of the collaborative function in C # Must be ienumerator, yield should be replaced by yield return, and startcoroutine () function should be used to start the coroutine.
[Original] unity3d deep dive-Script Development BASICS (scripts)