Unity commonly used core classes

insert image description here
1. Transform transformation
a. Position movement

//表示在世界坐标上向x正方向(右)位移10米(x,y,z)
transform.position = new Vector3(10, 0, 0);
//表示在本地坐标向右位移10米
transform.localPosition = new Vector3(10, 0, 0);
//Space.World:世界坐标系 Space.Self:自身坐标系
//在Update中使用Translate可以一直移动
transform.Translate(new Vector3(10,0,0),Space.World);
transform.Translate(new Vector3(10,0,0),Space.Self);
transform.Translate(new Vector3(10,0,0));//默认是世界坐标
//若使用position一直移动
transform.position += new Vector3(10, 0, 0);

World coordinates: take the origin of the scene as the origin
Local coordinates: take the position of the parent object as the origin
transform: It is a class used to describe the position, size, rotation and other information of the object.
Translate: It is an object of the transform class, attached to each object; a method of the transform class, used to change the position
b and rotation angle of the object

//四元数:Quaternion  欧拉角:Euler
//表示在世界坐标绕y轴旋转45°
transform.rotation = Quaternion.Euler(0, 45, 0);
//表示在本地坐标绕y轴旋转45°
transform.localRotation = Quaternion.Euler(0, 45, 0);
//表示一个游戏对象绕另一个游戏对象旋转(位置,方向,速度)
transform.RotateAround(sphere.position,Vector3.up,1);

transform.Rotate(new Vector3(0,45,0),Space.World);
transform.Rotate(new Vector3(0,45,0));//默认Space.World
transform.Rotate(new Vector(0,45,0),Space.Self);

The difference between transform.rotation and transform.Rotate:
The Rotate() method is: how many degrees to rotate. Accumulate on the original basis, that is, how many angles are rotated.
The rotation attribute is: rotate to a certain angle, that is, it is executed every frame in update. But the angle of each rotation is 45, so it is rotated to 45 degrees.
For example, if you only want it to rotate up to how much, use rotation;
if you want it to rotate all the time, you can use Rotaterotation to directly change the value to achieve the rotation effect.
c. Self scaling

//以x轴方向放大2倍
transform.localScale = new Vector3(2,1,1);

Speaking of this, let's think about a topic: how can a cube be better displaced, rotated, and scaled at a certain point of its own?
(See the bottom of the text for my thoughts)

2. Vector3 (vector)
vector: a quantity with both size and direction
a, back: backward
Vector3.back;
new Vector3(0,0,-1)
b, down: downward
Vector3.down;
new Vector3(0 ,-1,0);
c, forward: forward
new Vector3(0,0,1);
d, left: leftward
new Vector3(-1,0,0);
e, right: rightward
new Vector3(1 ,0,0);
f, one: add 1 to X, Y, Z each
new Vector3(1,1,1);
g, up: up
new Vector3(0,1,0);
h, zero: (0, 0,0)
new Vector3(0,0,0);

3.
The difference between GameObject GameObject and gameObject
GameObject is the base class of the game object class (all game objects)
gameObject is the object mounted by the script (the game object where the script component is located)

//1、创建空对象(分别取名father和son)
GameObject father = new GameObject("father");
GameObjec son = new GameObject("son");
//2、把son放在father下面
//方法一:
son.transform.parent = father.transform;
//方法二:
son.transform.setParent(father.transform);
//3、查找子对象
GameObject qiu = transform.GetChild(0).gameObject;
//4、查找父对象
GameObject cube = transform.parent.gameObject;

4. Stop
Time.time: Indicates the time from the start of the game to the present, and will stop calculating as the game pauses
Time.deltaTime: Indicates the time from the previous frame to the current time, in seconds
Time.timeScale: Time scaling, default It is 1, if set <1, it means that the time slows down, if it is set >1, it means that the time speeds up, which can be used to speed up and slow down the game, =0 means stop

5. Clone the game object
Step by step:
a. Clone the game object at the position of the preset body
GameObject go1 = Instantiate(cube);
b. Clone the game object at a fixed position (no operation, the position is the position of the preset body)
3. Setting The game object does not rotate
Quaternion.identity is the same as Quaternion(0,0,0,0)
at one time:
GameObject go2 = Instantiate(cube,new Vector3(0,0,5),Quaternion.identity);

6. Destroy the game object
a. Directly destroy the game object go1
Destory(go1);
b. After stopping for 3 seconds, destroy the object go2
Destory(go2,3);

7. Find the game object
a. Find the game object by the game object name (Player)
GameObject cube1 = GameObject.Find(“Player”);
Disadvantage 1: Duplicate name appears, you may not find the one you want
Disadvantage 2: If the object Too much, too much performance
b. Find the game object through the tag name
GameObje cube2 = GameObject.FindWithTag("Player");
Disadvantage 1: Duplicate names appear, you may not find the one you want
Disadvantage 2: Not too much performance
c , Find multiple game objects with the same tag
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");

8. Obtain and add components

//1)、获取脚本组件Move(本脚本和Move脚本在同一个游戏对象上)
Move m = transform.GetComponent<Move>();
//2)、本脚本不和Move脚本在同一个游戏对象
//a.找到游戏对象(假设脚本在Main Camera上)
GameObject camera = GameObject.Find(“Main Camera”);
//GameObject camera = GameObject.FindWithTag(“Main Camera”);
//b.查找游戏对象上面的脚本
Move m = camera.transform.GetComponent<Move>();
//3)、添加组件(Rigidbody)
a、Rigidbody r = gameObject.AddComponent<Rigidbody>();//添加刚体
b、r.AddForce(Vector3.forward*1000);//给向前的力

9. Random
// returns a number between 0.0 (inclusive) and 1.0 (inclusive)
float a = Random.value;
//includes the minimum but not the maximum
int b = Random.Range(0,100);
//includes the maximum and minimum
float c = Random. Range(0.0f,5.5f);

10. Input event
a. Mouse event
GetMouseButton(0): press the left mouse button and the program keeps running, release it to stop
GetMouseButton(1): press the right mouse button and the program keeps running, release it to stop
GetMouseButton (2): Press the middle mouse button and the program keeps running, and release it to stop
GetMouseButtonDown(0): When the
left mouse button is pressed, the program runs
once (2): When the middle mouse button is pressed, the program does not run, and when the middle mouse button is released, the program runs once.
0 = "Fire1" 1 = "Fire2" 2 = "Fire3"
GetButtonDown("Fire1");
Expansion:
void OnMouseEnter(){} indicates that the method will be performed when the mouse moves the position
void OnMouseExit(){} indicates that the mouse moves out of the position The position changes back to the original
b. Keyboard event
Input.GetKeyDown(KeyCode.A) //W, S, D
Input.GetKeyDown(KeyCode.LeftArrow) //RightArrow UpArrow DownArrow
Input.GetDown(KeyCode.Space)
custom horizontal and vertical build
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis(“Vertical”);
transform.Translate(new Vector3(h,0,v) Time.deltaTime speed);
c. The custom button
GetButton returns true according to the button name when the corresponding virtual button is pressed hour.
GetButtonDown returns true the frame the virtual button with the given name is pressed.
GetButtonUp returns true when the user releases the virtual button with the specified name.

11. Coroutine
insert image description here

 //使用协程(不要单独放在Update中,如果要放就添加条件)
 void Start(){
    
    StartCoroutine(“Print”)}//或者Print()//方法名
 IEnumerator Print()
{
    
    
   yield return new WaitForSeconds(3f);
   print(1);
}
//停止协程
//打印5个数字,每隔1秒打印一个数字,当打印到3的时候停止打印
Coroutine c;
Void Start(){
    
    c=StartCoroutine(Clone())}
IEnumerator Clone()
{
    
    
 for(int i=1;i<=5;i++)
 {
    
    
  print(i);
  if(i==3)
  {
    
    StopCoroutine(c)}
 }
  Yield return new WaitForSeconds(1);
}

12. Commissions and events
Since I haven't learned it yet, I won't write it.

Small question: The cube is displaced, rotated, and scaled by a certain point of itself
Answer: Create a parent object (empty object) for the cube, so that when the parent object rotates, the cube also rotates with the parent object.

Guess you like

Origin blog.csdn.net/weixin_44706943/article/details/126473499