Unity study notes - API

What is an API

        API (Application Programming Interface, application programming interface) are some predefined functions, the purpose is to provide applications and developers based on a certain software or hardware to access a set of routines without accessing the source code, or understanding the inner workings Mechanism details.

API relationship diagram (partial)

Object

The base class for all objects that Unity can reference.

The UnityEngine.Object class is the base class for all built-in objects in Unity.

Any public variable of a class inheriting from UnityEngine.Object can be used as a drag and drop target.

function:

FindObjectOfType: Returns the first  type loaded active object of type .

//public static T FindObjectOfType ();
//public static T FindObjectOfType (bool includeInactive);
//public static Object FindObjectOfType (Type type);
//public static Object FindObjectOfType (Type type, bool includeInactive);


CanvasRenderer canvas = FindObjectOfType<CanvasRenderer>();
//CanvasRenderer canvas = Object.FindObjectOfType<CanvasRenderer>();

FindObjectsOfType: Returns an array Object[] of objects of type type.

GameObject

The base class for all entities in a Unity scene.

Variables (parts): transform: The Transform attached to this GameObject.

                         tag: The tag of this game object.

                         layer: The layer this game object is in.

                        scene: The scene where this game object is located.

Constructor:

//public GameObject ();
//public GameObject (string name);
//public GameObject (string name, params Type[] components);

GameObject game = new GameObject("gameobject",typeof(Rigidbody),typeof(BoxCollider));
game.AddComponent<CapsuleCollider>();

function:

AddComponent: Adds a component of type componentType to this game object.

//public Component AddComponent(Type componentType);


Collider collider = this.gameObject.AddComponent<Collider>();
//Collider collider = this.gameObject.AddComponent(typeof(Collider)) as Collider;

GetComponent: If the game object is attached with a component of type type, return the component, otherwise return null.

//public Component GetComponent (Type type);

Rigidbody rb = this.gameObject.GetComponent<Rigidbody>();
//Rigidbody rb = GetComponent<Rigidbody>();
//Rigidbody rb = gameObject.GetComponent(typeof(Rigidbody)) as Rigidbody;

SetActive: Activates/deactivates a gameObject based on a given boolean value.

//public void SetActive (bool value);

this.gameObject.SetActive(false);
SetActive(true);

FindWithTag: Returns an active GameObject marked as tag, returns null if not found.

//public static GameObject FindWithTag (string tag);

GameObject player = GameObject.FindWithTag("player");

Component

UnityEngine.Component is the base class for all game objects attached to.

It is mainly used to obtain related content such as the rigid body covered by the game object. Or send messages to other game objects.

Variables: transform: The Transform attached to this GameObject.

          gameObject: The game object this component is attached to. Always attach components to game objects.

Most variables have been deprecated, get deprecated variables through GetComponent function. like:

Rigidbody rigidbody = component.GetComponent<Rigidbody>();

Transform

The object's position, rotation, and scale.

 

//foreach (Transform child in transform) //遍历子项

this.transform.localPosition = new Vector3(0,0,0); //相对于父变换的变换位置。
this.transform.localRotation = new Vector3(0,0,0); //相对于父级变换旋转的变换旋转。
this.transform.localScale = new Vector3(1,0,0);    //相对于 GameObjects 父对象的变换缩放。

this.transform.parent; //变换的父级。
this.transform.root;  //返回层级视图中最顶层的变换。

//Find:查找名称为name的子项。
Transform child = this.transform.Find(name); 

//Translate:根据 translation 的方向和距离移动变换。
this.transform.Translate(Vector3.forward*Time.deltaTime);  //相对自身移动。
this.transform.Translate(Vector3.up*Time.deltaTime,Space.World); //相对世界坐标移动。

//Rotate:以各种方式旋转GameObjects。
this.transform.Rotate(90.0f,0.0f,0.0f);  //绕x轴旋转90°。
this.transform.Rotate(90.0f,0.0f,0.0f,Space.World);

//SetParent:设置变换的父级。
this.transform.SetParent(parent);
this.transform.SetParent(parent,worldPositionStays);

Rigidbody

Control the position of objects through physics simulations.

After the Rigidbody component is added to the object, the movement is controlled by the Unity physics engine. It is recommended to use the FixedUpdate function in scripts to apply forces and change Rigidbody settings. The reason is that the physics update is performed at the time step of the measurement, which does not coincide with the frame update. FixedUpdate is called before every physics update, so any changes made in this function will be processed directly.

 

Rigidbody rb = GetComponent<Rigidbody>();
rb.mass = 1;   //刚体的质量。
rb.drag = 20;   //对象的阻力。阻力越大,对象越慢。
rb.freezeRotation = false; //控制物理是否会更改对象的旋转。
rb.useGravity = true;   //控制重力是否影响该刚体。
rb.isKinematic = true;  //控制物理是否影响刚体。

Collider

Base class for all colliders.

//isTrigger:碰撞体是否为触发器。
//center:中心点相对于物体位置的坐标。

void OnCollisionEnter(Collision collision){
    //当碰撞体接触另一个碰撞体。
    //Collision用于描述碰撞。
}

void OnCollisionExit(Collision collisionInfo){
    //当碰撞体停止接触另一个碰撞体。
}

void OnCollisionStay(Collision collisionInfo){
    //当碰撞体停留在另一个碰撞体。每帧调用。
}

void OnTriggerEnter(Collider other){
    /*
    GameObject和另一个GameObject碰撞时,Unity调用OnTriggerEnter。
    两个GameObject碰撞时,OnTriggerEnter在FixedUpdate函数上发生。
    注:两个 GameObjects 都必须包含 Collider 组件。其中一个必须启用 Collider.isTrigger,并包                        
含 Rigidbody。如果两个 GameObjects 都启用了 Collider.isTrigger,则不会发生碰撞。如果两个 
GameObjects 都没有 Rigidbody 组件,情况同样如此。
    */
}

void OnTriggerExit(Collider other){}
void OnTriggerStay(Collider other){}

Renderer

General functionality for all renderers.

There are other renderers such as: Line Renderer, Mesh Renderer, etc.

Renderer rend = GetComponent<Renderer>();
rend.enabled = true;   //控制渲染器的启用和停用。
rend.isVisible = true;   //控制渲染器在任何相机中是否可见。
rend.material;   //返回指定给渲染器的实例化材质

Camera

A camera, a device for the player to view the world.

//fieldOfView:相机的垂直视场。
//depth:摄像机在摄像机渲染顺序中的深度。深度较低的摄像机在深度较高的摄像机之前渲染。

Guess you like

Origin blog.csdn.net/hanshuangzirui/article/details/125767084