API自学记录(一)
程序员文章站
2022-04-03 07:58:10
...
API自学记录(一)
- Manual为官方教程式手册,其中有很多基础教程,比如窗口的介绍,如何添加工程等。
- Scripting API单独介绍各个类,各个方法。(以此为主)
什么是事件函数
- 新建场景,创建一个空物体,在此物体上创建新的C#脚本,名为Event01。
编写脚本注意大小写,多做测试。
当一个物体被实例化时,会调用Awake函数。
场景启用时,会调用OnEnable函数
FixedUpdate每秒固定调用60次,每帧可调用多次。(与物理相关的,比如运动,保证物体平滑运动)
Update和LateUpdate每帧调用一次,不同之处为Update先调用,LateUpdate后调用。(这俩个函数可根据实际情况做出改变,并不一定会按照预设的数值来执行)
OnTrigger与OnCollision触发碰撞器
OnMouseXXX为输入事件,由鼠标控制事件的执行
Scene rendering与场景渲染有关
OnApplicationPause场景运行暂停时调用
测试代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Event01 : MonoBehaviour {
void Awake()
{
Debug.Log("Awake");
}
void OnEnable()
{
Debug.Log("Enable");
}
// Use this for initialization
void Start()
{
Debug.Log("Start");
}
void FixedUpdate()
{
Debug.Log("FixedUpdate");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update");
}
void LateUpdate()
{
Debug.Log("LateUpdate");
}
void OnApplicationPause()
{
Debug.Log("OnApplicationPause");
}
void OnDisable()
{
Debug.Log("Disable");
}
void OnApplicatoinQuit()
{
Debug.Log("OnApplicationQuit");
}
void Reset()
{
Debug.Log("Reset");
}
void OnDestroy()
{
Debug.Log("OnDestroy");
}
}
Time类中静态变量介绍
- captureFramerate通过设置帧的速率可以进行屏幕截图。
- deltaTime当前帧所占用的时间,1/60s左右浮动(smooth deltaTime变化相对平缓)
- fixedDeltaTime=1/60s
- fixedTime从游戏开始到现在的时间(realtimeSinceStartup(游戏暂停时也会持续增加)、time)
- frameCount从游戏开始到现在运行了多少帧
- timeScale时间的比例,可使游戏进行加速
- timeSinceLevelLoad场景加载后开始计时,一旦打开新的场景将重新计时
代码测试:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Time02 : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log("Time.deltaTime:" + Time.deltaTime);
Debug.Log("Time.fixedDeltaTime:" + Time.fixedDeltaTime);
Debug.Log("Time.fixedTime:" + Time.fixedTime);
Debug.Log("Time.frameCount:" + Time.frameCount);
Debug.Log("Time.realtimeSinceStartup:" + Time.realtimeSinceStartup);
Debug.Log("Time.smoothDeltaTime:" + Time.smoothDeltaTime);
Debug.Log("Time.time:" + Time.time);
Debug.Log("Time.timeScale:" + Time.timeScale);
Debug.Log("Time.timeSinceLevelLoad:" + Time.timeSinceLevelLoad);
Debug.Log("Time.unscaledTime:" + Time.unscaledTime);
}
// Update is called once per frame
void Update () {
Debug.Log("Time.deltaTime:" + Time.deltaTime);
Debug.Log("Time.fixedDeltaTime:" + Time.fixedDeltaTime);
Debug.Log("Time.fixedTime:" + Time.fixedTime);
Debug.Log("Time.frameCount:" + Time.frameCount);
Debug.Log("Time.realtimeSinceStartup:" + Time.realtimeSinceStartup);
Debug.Log("Time.smoothDeltaTime:" + Time.smoothDeltaTime);
Debug.Log("Time.time:" + Time.time);
Debug.Log("Time.timeScale:" + Time.timeScale);
Debug.Log("Time.timeSinceLevelLoad:" + Time.timeSinceLevelLoad);
Debug.Log("Time.unscaledTime:" + Time.unscaledTime);
}
}
Time.realtimeSinceStartup可做性能测试
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Time02 : MonoBehaviour {
public Transform cube;
public int runCount = 10000;
// Use this for initialization
void Start () {
float time00 = Time.realtimeSinceStartup;
for (int i = 0; i < runCount; i++)
{
Method0();
}
float time0 = Time.realtimeSinceStartup;
Debug.Log(time0 - time00);
float time1 = Time.realtimeSinceStartup;
for (int i = 0; i < runCount; i++)
{
Method1();
}
float time2 = Time.realtimeSinceStartup;
Debug.Log(time2 - time1);
float time3 = Time.realtimeSinceStartup;
for (int i = 0; i < runCount; i++)
{
Method2();
}
float time4 = Time.realtimeSinceStartup;
Debug.Log(time4 - time3);
}
// Update is called once per frame
void Update () {
}
void Method0()
{
}
void Method1()
{
int i = 2;
i += 2;
i += 2;
}
void Method2()
{
int i = 2;
i *= 2;
i *= 2; }
}
创建游戏物体的三种方法
- 直接创建
- 根据prefab创建或者另外一个游戏物体克隆
- 直接创建原始的物体
测试代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gameobject03 : MonoBehaviour {
public GameObject prefab;
// Use this for initialization
void Start () {
GameObject go = new GameObject("Cube");//第一种
GameObject.Instantiate(prefab);//可以根据prefab 或者 另外一个游戏物体克隆
GameObject.CreatePrimitive(PrimitiveType.Plane);
GameObject.CreatePrimitive(PrimitiveType.Cube);
}
}
如何给物体通过代码添加组件:
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.AddComponent<Rigidbody>();
go.AddComponent<Event01>();
如何禁用和启用一个物体
Tag用于更方便的区分不同的游戏物体
通过activeinHierarchy表示物体是否处于**状态
Debug.Log(go.activeInHierarchy);
go.SetActive(false);
Debug.Log(go.activeInHierarchy);
Debug.Log(go.tag);
GameObject与Component和Object的联系
Debug.Log(go.name);
Debug.Log(go.GetComponent<Transform>().name);
UnityEngine下Object拥有的静态方法
Light light = FindObjectOfType<Light>();
light.enabled = false;
Transform[] ts= FindObjectsOfType<Transform>();//不查找未**的游戏物体
foreach (Transform t in ts)
{
Debug.Log(t);
}
GameObject独有的静态方法
Find会遍历,耗费性能
Tag效率会更高一点
查找方法:
GameObject go = GameObject.Find("Main Camera");
GameObject[] gos= GameObject.FindGameObjectsWithTag("MainCamera");
GameObject go= GameObject.FindGameObjectWithTag("Finish");
go.SetActive(false);
游戏物体间消息的发送和接收
CompareTag比较标签
SetActive用来**或者禁用游戏物体
BroadcastMessage广播消息,传递一个方法的名字,不但调用自身还调用其子物体(多个),可减少游戏的耦合性
SendMassage针对某个目标发送消息,不会调用子物体
SendMassageUpwards向上发送,当前物体以及其父物体(只有一个)
public class Message04 : MonoBehaviour {
public GameObject target;
// Use this for initialization
void Start () {
target.BroadcastMessage("Attack",null,SendMessageOptions.DontRequireReceiver);
//控制是否报错
}
// Update is called once per frame
void Update () {
}
public class Cube : MonoBehaviour {
// Use this for initialization
void Start () {
}
void Attack() {
Debug.Log(this.gameObject + "正在攻击");
}
}
得到组件的各种方法函数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetComponent05 : MonoBehaviour {
public GameObject target;
// Use this for initialization
void Start () {
Cube cube = target.GetComponent<Cube>();
Transform t = target.GetComponent<Transform>();
Debug.Log(cube);
Debug.Log(t);
Debug.Log("---------------------------------");
Cube[] cubes = target.GetComponents<Cube>();
Debug.Log(cubes.Length);
Debug.Log("---------------------------------");
cubes = target.GetComponentsInChildren<Cube>();
foreach (Cube c in cubes)
{
Debug.Log(c);
}
Debug.Log("---------------------------------");
cubes = target.GetComponentsInParent<Cube>();
foreach (Cube c in cubes)
{
Debug.Log(c);
}
}
// Update is called once per frame
void Update () {
}
}
MonoBehaviour里的常用变量
isActiveAnd Enabled 判断组件是否**
Enabled =true/fouse
Print输出,相当于Debug.Log
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonoBehaviour06 : MonoBehaviour {
public Cube cube;
// Use this for initialization
void Start()
{
Debug.Log(this.isActiveAndEnabled);
Debug.Log(this.enabled);
enabled = false;//禁用组件
Debug.Log(name);
Debug.Log(tag);
Debug.Log(gameObject);
Debug.Log(transform);
print("haha");//必须在MonoBehaviour里使用
//取别的组件的
Debug.Log(cube.isActiveAndEnabled);
Debug.Log(cube.enabled);
cube.enabled = false;
Debug.Log(cube.name);
Debug.Log(cube.tag);
Debug.Log(cube.gameObject);
Debug.Log(cube.transform);
}
// Update is called once per frame
void Update () {
}
}
MonoBehaviour里Invoke的使用
ExecuteInMode在编辑模式下也会调用
Invoke调用某个方法
CancelInvoke取消当前脚本的方法,InvokeRepeating重复调用某方法
IsInvoking判断是否调用
public class Invoke07 : MonoBehaviour {
// Use this for initialization
void Start () {
//Invoke("Attack", 3);
InvokeRepeating("Attack", 4, 2);//隔4秒调用,之后每隔2秒调用一次
CancelInvoke("Attack");
}
// Update is called once per frame
void Update () {
bool res=IsInvoking("Attack");
print(res);
}
void Attack() {
print("攻击目标");
}
什么是协程,它是如何执行的
协程方法不会等这个方法执行完就继续向下执行,在运行过程中自身可以暂停
普通方法会等普通方法执行完之后接着向下执行
普通方法:
public class Corouting08 : MonoBehaviour {
public GameObject cube;
// Use this for initialization
void Start () {
print("hha");
ChangeColor();
print("hahaha");
}
void ChangeColor() {
print("hahaColor");
cube.GetComponent<MeshRenderer>().material.color = Color.blue;
print("hahaColor");
}
}
协程方法:
public class Corouting08 : MonoBehaviour {
public GameObject cube;
// Use this for initialization
void Start () {
print("hha");
//ChangeColor();
StartCoroutine(ChangeColor());//开启协程
print("hahaha");
}
//返回值是IEnumerator
IEnumerator ChangeColor() {
print("hahaColor");
yield return new WaitForSeconds(3);//3秒后执行下一句操作,暂停操作
cube.GetComponent<MeshRenderer>().material.color = Color.blue;
print("hahaColor");
yield return null;//用来返回
}
}
使用Coroutine实现颜色动画渐变
public class Corouting08 : MonoBehaviour {
public GameObject cube;
// Use this for initialization
void Start () {
}
void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
StartCoroutine(Fade());
}
}
IEnumerator Fade()
{
for(;;)
{
//cube.GetComponent<MeshRenderer>().material.color = new Color(i,i,i,i);
Color color = cube.GetComponent<MeshRenderer>().material.color;
Color newColor = Color.Lerp(color, Color.red,0.02f);
cube.GetComponent<MeshRenderer>().material.color = newColor;
yield return new WaitForSeconds(0.02f);
if (Mathf.Abs(Color.red.g - newColor.g) <= 0.01f)
{
break;
}
}
}
IEnumerator ChangeColor() {
print("hahaColor");
yield return new WaitForSeconds(3);//3秒后执行下一句操作,暂停操作
cube.GetComponent<MeshRenderer>().material.color = Color.blue;
print("hahaColor");
yield return null;//用来返回
}
}
Coroutine协程的开启和关闭
StartCoroutine
StopCoroutine
StopAllCoroutine停止所有协程
private IEnumerator ie;
void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
//ie = Fade();
//StartCoroutine(ie);
StartCoroutine("Fade");
}
if (Input.GetKeyDown(KeyCode.S)) {
//StopCoroutine(ie);
StopCoroutine("Fade");
}
}
上一篇: 搞笑动漫图片--漫画图片
下一篇: 搞笑漫画笑死人,人人都有搞笑的一面。