[Unity 3D] learning notes 28: Unity tool class

Source: Internet
Author: User

Unity provides developers with many convenient development tools. They are all functions and methods encapsulated by the system. For example, the time class that implements time and the random number random. Range () method.


Time

Time class, mainly used to obtain the current system time.

Using unityengine; using system. collections; public class script_04_13: monobehaviour {void ongui () {guilayout. label ("current game time:" + time. time); guilayout. label ("Time consumed by the previous frame:" + time. deltatime); guilayout. label ("fixed increment time:" + time. fixedtime); guilayout. label ("the previous frame consumes a fixed time:" + time. fixeddeltatime );}}
Run:


Time. Time: indicates the total time the game has been running since the start of the game.
Time. deltatime: gets the time consumed to complete the previous frame in the update () method.
Time. fixedtime: the total time consumed by the fixedupdate () method.
Time. fixeddeltatime: the time consumed by the last frame of a fixed update.



Program waiting

The waitforseconds () method can be used in a program to wait for a period of time in seconds, and the main thread of the game can directly enter the waiting state. The return value of this method is ienumerator type.

Using unityengine; using system. collections; public class script_04_14: monobehaviour {ienumerator start () {// wait for return test () ;}// return wait time ienumerator test () {debug. log ("start waiting:" + time. time); yield return New waitforseconds (2); debug. log ("end wait:" + time. time );}}
Run:


Call yield return New waitforseconds (2) where you need to wait, and 2 in the method indicates waiting for 2 seconds.



Mathematics

Unity encapsulates a mathf class for developers.

Mathf. Abs (int I) returns the absolute value of an integer.

Mathf. Sin (5) returns the sine value.

Mathf. Max (0,100) returns the maximum of two numbers.

Mathf. Pi circumference Rate

If you are interested, you can view the API documentation or Google



Quaternary

It is a very important tool class. In unity, Game objects are rotated, and the lower layer is implemented by the ons. It can accurately calculate the angle of rotation of Game objects.

The following is a simple example. In this example, click rotate fixed angle, the game object will rotate 50 degrees along the Y axis, and click interpolation to rotate the fixed angle, the game object will be rotated 60 degrees along the Y axis.

Using unityengine; using system. collections; public class script_04_16: monobehaviour {// whether to start interpolation and rotation bool isrotation = false; void ongui () {If (guilayout. button ("Rotating fixed angle", guilayout. height (50) {gameobject. transform. rotation = Quaternion. euler (0.0f, 50366f, 0.0f);} If (guilayout. button ("interpolation rotation fixed angle", guilayout. height (50) {isrotation = true ;}} void Update () {// start difference value rotation if (isrotation) {gameobject. transform. rotation = Quaternion. slerp (gameobject. transform. rotation, quaternion. euler (0.0f, 601_f, 0.0f), time. time * 0.1f );}}}
Run:

Click to rotate the fixed angle:

Click interpolation to rotate the fixed angle:


Quaternion. Euler ()



Quaternion. slerp ()



Random Number

Sometimes the program needs to obtain a random number, which is easy to implement using the random. Range () method.

Using unityengine; using system. collections; public class script_04_15: monobehaviour {void start () {int A = random. range (0,100); float B = random. range (0.0f, 10.0f); debug. log ("Get an integer random number between 0 and" + a); debug. log ("getting a floating point random number between 0.0f-10.0f" + B );}}
Run:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.