Unity学习笔记 - API

什么是API

        API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。

API关系图(部分)

Object

Unity可以引用的所有对象的基类。

UnityEngine.Object类是Unity所有内置对象的基类。

任何从UnityEngine.Object继承类的公共变量,都可以作为一个拖放目标。

函数:

FindObjectOfType:返回第一个类型为 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:返回类型为type的对象的数组Object[]。

GameObject

Unity场景中所有实体的基类。

变量(部分):transform:附加到此GameObject的Transform。

                         tag:此游戏对象的标签。

                         layer: 此游戏对象所在的层。

                        scene:此游戏对象所在的场景。

构造函数:

//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>();

函数:

AddComponent:将类型为componentType的组件添加到该游戏对象。

//public Component AddComponent(Type componentType);


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

GetComponent:若该游戏对象附加了类型为type的组件,则返回该组件,否则返回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:根据给定布尔值激活/停用gameObject。

//public void SetActive (bool value);

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

FindWithTag:返回一个标记为tag的活动GameObject,未找到返回null。

//public static GameObject FindWithTag (string tag);

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

Component

UnityEngine.Component是所有附加到游戏对象的基类。

主要用来获取游戏对象所涵盖的刚体等相关内容。或对其它游戏对象发送消息。

变量:transform:附加到此 GameObject 的 Transform。

          gameObject:此组件附加到的游戏对象。始终将组件附加到游戏对象。

大多数变量已被弃用,通过GetComponent函数获取弃用的变量。如:

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

Transform

对象的位置、旋转和缩放。

 

//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

通过物理模拟控制对象的位置。

物体添加Rigidbody组件后,运动受Unity物理引擎控制。在脚本中推荐使用FixedUpdate函数来施加力和更改Rigidbody设置。原因是物理更新在测量的时间步骤中执行,而时间步骤与帧更新不一致。FixedUpdate 在每次进行物理更新前调用,因此在该函数中做出的任何更改都将直接处理。

 

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

Collider

所有碰撞体的基类。

//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

所有渲染器的常规功能。

还有其他的一些renderer如:Line Renderer,Mesh Renderer等。

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

Camera

摄像机,供玩家观看世界的设备。

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

猜你喜欢

转载自blog.csdn.net/hanshuangzirui/article/details/125767084