当前位置:网站首页>Several important classes in unity

Several important classes in unity

2022-07-06 04:04:00 A plum in fall

List of articles

Preface

This blog lists Unity Some methods in the more important classes in , Not all of them , See API file .


One 、GameObject class

1. Find()

  1. Find the specified game object through the game object name
  2. Find() When looking for , As long as it is the game object under the current scene , By default, you can find . And no matter which object the search script is attached to , Can be found . With child parent relationship , You can also find .
  3. Game object with the same name , You can find several ?
    answer : Only one will be found

2. FindWithTag( Tag name )

  1. I'll just find An object that matches the tag name
  2. without ,null
  3. If the specified label is not added in advance , Error message :**** is not defined

3. FindGameObejctsWithTag()

  1. Find multiple game objects with matching names through tag names
  2. The return is GameObject An array of types
  3. You can traverse the returned array and other operations
  4. Get an array with a length of : call Length

4. Create game objects

5. SetActive( true );

6. GetComponent(typeOf(Transform))

  1. Generics are recommended GetComponent< >

Two 、Transform class

Properties in this class

1. parent

Get the parent object of the current game object

2. root

Get the top level of the current game object

3. localPosition

Get the location of the local coordinate system

4. GetInstanceID

Get the current GameObject's id( Unique )

5. childCount

Get the number of sub objects

Methods in this class

1. GetChild( int index )

Gets the GameObject

2. IsChildOf ()

Determine whether it is a child of the specified object

3. Find()

Look for objects

4. Translate()

Move in the specified direction and position

i. (x , y , z)
ii. Vector3.left|right|up|down|forward|back

Such as : Move to the left 1 A unit
transform.Translate(-1,0 , 0);
transform.Translate(Vector3.left);

transform.Translate() Function , The first variable is the speed of the object , The velocity here is a vector , Both case and direction , The latter variable is the relative coordinate system , The relative coordinate system here has two values , One is the world coordinates , One is its own coordinates , If the first coordinate is not filled in , The default is its own coordinate system .
notes :Space.self | world

transform.Translate(Vector3.up * Time.deltaTime, Space.Self);

5. Rotate()

Rotate at the specified angle

Rotate( Parameters 1, Parameters 2)

 Rotate the cube , With Y The axis rotates in the positive direction with the center point .
transform.Rotate(Vector3.up);
transform.Rotate(Vector3.up * Time.deltaTime);  
transform.Rotate(Vector3.up * Time.deltaTime * speed);
 Rotate the cube , With X The axis rotates as the center .
transform.Rotate(Time.deltaTime, 0, 0);
transform.Rotate(Time.deltaTime * speed, 0, 0);

6.LookAt()

Make the current object face the target position

Inherited from the parent class Component

1. attribute :name, gameObject, tag, transform…

2. GetComponent<>()

3. GetComponentInChildren<> ()

4. Instantiate

5. Instantiate ( The original object )

Instantiate( The original object , The location of the new object , The angle of the new object )
The method is Unity Medium Object Class
Used to generate a copy of the specified game object
When method execution is finished , A copy of the object will be treated as Object Type return

This method is generally written in Update() In the method , Usually under specific conditions , Otherwise, the method will be executed all the time , Carton . Such as : Click the left mouse button or a key on the keyboard , Will produce a copy …

notes : If the position and rotation angle of the new object are not specified through the second and third parameters , Then by default, a new object will be generated in the same position as the original object ( The new object will overwrite the original object )

3、 ... and 、Random class

Random class : Pseudo random number

1. seeds

2. Range()

Random.Range (min, max): What happened was min To max Random numbers in the range include min, But it doesn't include max

3. give an example

snake

Four 、Time class

1. deltaTime

float Return value
It takes time to execute from the previous frame ( decimal )

Two use scenarios :
1) Rotate or move , It's quite fast ,*deltaTime Control the speed
2)Update() Time mismatch caused by frame rate ,
FixedUpdate()— object ( rigid body )
resolvent :
transform.Translate(Vector3.up * Time.deltaTime)

2. time

Can be placed in update It calculates the time from the beginning to the moment

3. timeScale

Pause

5、 ... and 、Mathf class

Provides a series of methods and attributes for mathematical operations

1. Find the absolute value ABS()

2. Find the maximum number or the minimum number MAX() MIN()

3. Square root : Sqrt()

4. rounding

Round()

Returns the rounded to the nearest integer /f/.
9.1 Round to the nearest 9
9.5 rounding 10
-9.1 rounding -9
-9.4 rounding -9
-9.5 rounding -10

Ceil()

Returns greater than or equal to f Minimum integer of .

Floor()

Returns less than or equal to f Maximum integer for .
If this number is positive , After rounding : Integral part
If this number is negative , After rounding : reduce 1, Such as :-9.1 result :-10

5. Angle and radian values ( Constant values )

Deg2Rad

1 How many radians does the angle equal

Rad2Deg

1 How many angles does radian equal

6. Positive infinity 、 Negative infinity ( Constant values )

Infinity

print Coming out is Infinity

NegativeInfinity

print Coming out is -Infinity

7. Trigonometric functions :Sin(),Cos(),Tan()…

8. Power and exponent :Log()、Log10()、Pow()

9. Lerp()

stay a And b Press... Between t Linear interpolation .

10. SmoothDamp()

Gradually change a value to the desired goal over time .

11. MoveTowards()

Will value current towards target near .

12. PingPong()

PingPong Returns a value , The value will be at the value 0 And length Between increasing and decreasing . Type table tennis .

6、 ... and 、Input class

Mouse input

Input Class provides related properties and methods for processing mouse input

1. mousePostion

Get the specific coordinates of the mouse on the current screen
It's a two-dimensional coordinate (Z Axis is always 0)

The coordinates are related to the current screen pixels
If the mouse is in the lower left corner of the current screen , Then for (0,0)
If the mouse is in the upper right corner of the screen , Then for (Screen.Width, Screen.Height)

notes : The current screen refers to the current Game window

2. GetMouseButtonDown( Key code )

When the specified mouse button code is pressed , return true
otherwise , return false
Such as :

if(Input.GetButtonDown(2))
{
    
   print(Random.Range(0,10);   //[0,10)
}
else
{
    
   print(Random,Range(-10,0))  ///[-10,0)
}

Left mouse button :0
Right mouse button :1
Middle of mouse :2

Judge whether the middle mouse button is pressed currently , If you press , return [0,10) Random number between , Otherwise return to [-10,0)

3. GetMouseButtonUp( Key code )

When the specified mouse button code is pressed , return true
otherwise , return false

Generally, before implementing this method , Press first , Then lift

4. GetMouseButton( Key code )

When the specified mouse is pressed all the time , return true
otherwise , return false

5. GetAxis(“Mouse X”)

Method returns float Data of type

Get the distance the mouse moves horizontally

When the mouse moves along the positive half axis in the horizontal direction , Get a positive number

When the mouse moves on the negative half axis in the horizontal direction , Get a negative number

6. GetAxis(“Mouse Y”)

Method returns float Data of type

Get the distance the mouse moves in the vertical direction

When the mouse moves on the positive half axis in the vertical direction , Get a positive number

When the mouse moves on the negative half axis in the vertical direction , Get a negative number

Keyboard entry

Input

1. GetKeyDown( Keyboard keys )

Specify when a keyboard key is pressed , return true, otherwise , return false

2. GetKeyUp( Keyboard keys )

Specify when a keyboard key is lifted , return true, otherwise , return false

3. GetKey( Keyboard keys )

Specify that a keyboard key is pressed all the time , return true, otherwise , return false

notes : Key code of keyboard keys
i. “*****”
Such as "a"
ii. KeyCode
Such as KeyCode.A

4. GetAxis(“Horizontal”)

Used to judge the horizontal direction key on the keyboard

5. GetAxis(“Vertical”)

It is used to judge which vertical direction key on the keyboard is pressed

If the method returns 0 To 1 A positive number between , It means pressing the up direction

If the method returns 0 To -1 Negative numbers between , Indicates that the downward direction is pressed

return -1 To 1 Decimal between


Reference source

Official documents :API file
Reference video : Darnay Education Unity Basics
Reference material : Netizens' notes

原网站

版权声明
本文为[A plum in fall]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060401187955.html