Unity3D
Basic Skills
- Usually set the values of private values in the
Start()function. - Only gameObject has the function of
GetComponent Transform- Only transform has the function of
Find(string name) - The function of
Find(string name)for a transform can only find the direct children of a transform
- Only transform has the function of
Physics Engine
Collider
Wheel Collider
Wheel collider is a collider rather than the mesh of the wheel. You can add force or torque to the collider to make the collider rotate like a wheel.
Rigidbody
Rigidbody has a element named velocity, which can set the speed(Vector3) of the gameobject.
Detect collision or collision position between objects
- Use
OnCollisionEnterorOnTriggerEnter- Required object to have collider and rigidbody
- Use
Physics.OverlapSphere(point)to detect whether the point collides with other colliders. - Use
Collider.Raycastto get the accurate collision position. It will ignore other colliders but this collider.
Scripting
Find Objects
- Use
transform.root.gameObjectcan find the root object - Use
gameObject.GetComponentInChild()can find the script attached to the child objects. - Use
transform.Find()can find the children objects, but this is only used to find thedirect childrenobjects.
Attributes
Attributes of C# can add metadata (information about the types defined in a program) to the program. In Unity, it can be used to show information in Unity editor’s inspector.
The attributes work by placing the name of the attribute enclosed in square brackets ([]) above the declaration of the entity to which it applies. There are several different attributes in Unity
| Name | Function |
|---|---|
| HideInInspector | Hide public parameter in Unity Inspector |
| SerializeField | Show the private parameter in Unity Inspector |
| Space | Show a space line in Unity Inspector |
| Header(“XX”) | Show a comment for the parameter in Unity Inspector |
| Range(min, max) | Show the value range in Unity Inspector |
Asynchronous
Unity can use async/await keywords of C# to achieve asynchronous methods, which can be found here.
However, some Unity library functions cannot be executed correctly in the asynchronous thread. Instead, Unity has its own asynchronous keyword, which is IEnumerator/yield return.
yield return keyword has many kinds of return type, which shows following
| Type | Function |
|---|---|
yield return new WaitForEndofFrame() |
Wait until the end of current frame |
yield return new WaitForFixedFrame() |
Wait until next frame |
yield return new WaitForSeconds(xx) |
Wait for xx seconds |
yield return new WaitForWhile(func) |
Wait until func return false |
yield return new WaitForUntil(func) |
Wait until func return true |
In Unity scripts, sometimes we need to do some actions after changing the properties of the GameObject, e.g. position, rotation. Unity will finish the changing action in the end of current frame. So we need to wait for a while before doing other actions.
Note. the event of delegate trigger is also finished in the current frame.
Also in Unity, we need to use yield return null or allow the code return to the main thread to do some changes in the scene.
Note. don’t use yield return 0 because it is the same with yield return null but it will lead to boxing action.
Compile
Conditional Compile
Unity has many targeted platforms, so the codes can be conditional complied according to this link
Compile Pipeline
- Unity will compile C# scripts to IL with C# compiler firstly
- Unity will also combines all the other assemblies like the
UnityEngine.dllandmscorlib.dll - Unity will use
MonoorIL2CPPto build the IL scripts to naive code