利用将近两天的时间学习并整理了常用API的知识点!

目录

脚本生命周期流程图(续上)

常用API

核心类图

*Component类

编辑

*Transform类

练习:在层级未知情况下查找子物体(递归)

*GameObject类

编辑

*Object类

练习:查找血量最低的敌人且改变颜色

Unity User Manual 2021.3 (LTS) - Unity 手册https://docs.unity.cn/cn/current/Manual/index.html


脚本生命周期流程图(续上)

unity2019使用那个api compatibility level unity api手册_世界坐标系

unity2019使用那个api compatibility level unity api手册_游戏引擎_02

 

unity2019使用那个api compatibility level unity api手册_缩放比例_03

 

 

常用API

核心类图

unity2019使用那个api compatibility level unity api手册_游戏引擎_04

 

*Component类
Component类提供了查找(在当前物体、后代、先辈)组件的功能
//获取当前物体所有组件
var allComponent=this.GetComponents<Component>();
foreach (var item in allComponent)
{Debug,Log(item.GetType();}
//获取后代物体的指定类型组件(从自身开始)
this.GetComponentsInChildren<MeshRenderer>().material.color;
//获取先辈物体的指定类型组件(从自身开始)
this.GetComponentsInParent<MeshRenderer>().material.color;

unity2019使用那个api compatibility level unity api手册_世界坐标系_05

 

unity2019使用那个api compatibility level unity api手册_游戏引擎_06

 

unity2019使用那个api compatibility level unity api手册_unity_07

 

 

*Transform类
Transform类提供了查找(父、根、子[根据索引和名称])变换组件、改变位置、角度、大小的功能;
//child为每个子物体的变换组件
foreach(Transform child in this.transform)
//物体相对于世界坐标系原点的位置
this.transform.position
//物体相对于父物体轴心点的位置
this.transform.localposition
//相对于父物体缩放比例
this.transform.localScale
//理解为:物体与模型缩放比例(自身缩放比例*父物体缩放比例)
this.transform.lossyScale
如:父物体localScale为2,当前物体localScale为3,则lossyScale为6
//向自身坐标系Z轴移动1m
this.transform.Translate(0,0,1);
//向世界坐标系Z轴移动1m
this.transform.Translate(0,0,1,space.World);
//向自身坐标系Z轴旋转1度
this.transform.Rotate(0,0,1);
//向世界坐标系Z轴旋转1度
this.transform.Rotate(0,0,1,space.World);
//围绕旋转
this.transform.RotateAround(Vector3.zero,Vector3.up[y轴],1);
//获取根物体变换组件
this.transform.root;
//获取父物体变换组件
this.transform.parent;
//设置父物体
public Transform TF;
public transform.SetParent(TF,(ture)[默认世界坐标]/false[自身坐标]);

unity2019使用那个api compatibility level unity api手册_世界坐标系_08

 

(ture世界坐标,false自身坐标(自身的坐标就是与cube5的距离))

//根据名称获取子物体
Transform childTF=this.transform.Find(“子物体名称”);
//根据索引获取子物体
For(int i=0;i<count;i++)
{Transform childTF=this.transform.GetChild(i);}

unity2019使用那个api compatibility level unity api手册_缩放比例_09

unity2019使用那个api compatibility level unity api手册_System_10

 

unity2019使用那个api compatibility level unity api手册_缩放比例_11

 

unity2019使用那个api compatibility level unity api手册_unity_12

 

unity2019使用那个api compatibility level unity api手册_unity_13

 

 

练习:在层级未知情况下查找子物体(递归)

unity2019使用那个api compatibility level unity api手册_游戏引擎_14

 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class TransformHelper : MonoBehaviour

{

    private void OnGUI()

    {

        if (GUILayout.Button("层级未知,查找子物体"))

        {

            var childTF = TransformHelper.GetChild(this.transform,"Cube (5)");

            childTF.GetComponent<MeshRenderer>().material.color = Color.red;

        }

    }

            /// <summary>

            /// 在层级未知情况下查找子物体

            /// </summary>

            /// <param name="parentTF">父物体变换组件</param>

            /// <param name="childName">子物体名称</param>

            /// <returns></returns>

    public static Transform GetChild(Transform parentTF,string childName)

    {

        //在子物体中查找

        Transform childTF = parentTF.Find(childName);

        if(childTF!=null)return childTF;

        //将问题交由子物体

        int count = parentTF.childCount;

        {

            for(int i=0;i<count;i++)

            {

                childTF = GetChild(parentTF.GetChild(i), childName);

                if (childTF != null)

                    return childTF;
            }

        }

        return null;

    }

    

}

unity2019使用那个api compatibility level unity api手册_游戏引擎_15

 (效果图)

*GameObject类

//在场景中物体激活状态(物体实际激活状态)
this.gameObject.activeInHierarchy
//物体自身激活状态(物体在Inspector面板中的状态)
this.gameObject.activeSelf;

unity2019使用那个api compatibility level unity api手册_缩放比例_16

 

(activeInHierarchy——false,activeSelf——ture)
//设置物体启用/禁用
this.gameObject.SetActive(ture/false);
//创建物体
GameObject lightGO=new GameObject();
//添加组件
Light light=lightGO.AddComponent<Light>();
light.color=Color.red;
light.type=LightType.Point;
//在场景中根据名称查找物体(不建议使用)
GameObject.Find(“游戏对象名称”);
//获取所有使用该标签的物体
GameObject[]allEnemy=GameObject.FindGameObjectsWithTag(“标签”);
//获取使用该标签的物体(单个)
GameObject player=GameObject.FindWithTag(“标签”);

unity2019使用那个api compatibility level unity api手册_System_17

 

unity2019使用那个api compatibility level unity api手册_unity_18

 

 

*Object类

Destory 删除一个游戏对象、组件或资源
//加载新场景时,使目标对象不被自动销毁
DontDestoryOnLoad(transform.gameObject);
//根据类型查找对象
Object.FindObjectOfType<  >();

练习:查找血量最低的敌人且改变颜色

HP实例成员挂在物体中,就是一实例变量

unity2019使用那个api compatibility level unity api手册_unity_19

 

找Enemy类型的对象,把所有对象的引用放在一个数组里

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Enemy : MonoBehaviour

{

    public float HP;

}
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class FindEnemyDome : MonoBehaviour

{

    private void OnGUI()

    {

        if(GUILayout.Button("查找血量最低的敌人"))

        {

            //查找场景中所有Enemy类型的引用

            Enemy[] allEnemy= Object.FindObjectsOfType<Enemy>();

            //获取血量最低的对象引用

            Enemy min = FindEnemyByMinHP(allEnemy);

            //根据Enemy类型引用,获取其他组件类型引用

            min.GetComponent<MeshRenderer>().material.color = Color.red;

        }

    }

    public Enemy FindEnemyByMinHP(Enemy[] allEnemy)

    {

        //假设第一个就是血量最低的敌人

        Enemy min = allEnemy[0];

        //依次与后边比较

        for(int i=1;i<allEnemy.Length;i++)

        {

            if(min.HP>allEnemy[i].HP)

            {

                min = allEnemy[i];//替换成当前的引用,不要加HP改属性值了

            }

        }

        return min;

    }

}

Time类

unity2019使用那个api compatibility level unity api手册_缩放比例_20

unity2019使用那个api compatibility level unity api手册_缩放比例_21