利用将近两天的时间学习并整理了常用API的知识点!
     
    
   
每日一句:能打动人的从来不是花言巧语,而是恰到好处的温柔以及真挚的内心
    
     目录
    
   
    
     Unity User Manual 2021.3 (LTS) – Unity 手册https://docs.unity.cn/cn/current/Manual/index.html
    
   
    脚本生命周期流程图(续上)
   
    
     
      
       
        常用API
       
      
     
    
   
    
     
      
       
        核心类图
       
      
     
    
   
    
     
      
       
        
       
      
     
    
   
    
     
      
       
        *
       
      
     
    
    
     
      
       
        Component类
       
      
     
    
   
    
     
      
       
        Component类提供了查找(在当前物体、后代、先辈)组件的功能
       
      
     
    
   
    
     
      
       
        //获取当前物体所有组件
       
      
     
    
   
    
     
      
       
        v
       
      
     
    
    
     
      
       
        ar allComponent=this.GetComponents<Component>();
       
      
     
    
   
    
     
      
       
        f
       
      
     
    
    
     
      
       
        oreach (var item in allComponent)
       
      
     
    
   
    
     
      
       
        {Debug,Log(item.GetType();}
       
      
     
    
   
    
     
      
       
        //获取后代物体的指定类型组件(从自身开始)
       
      
     
    
   
    
     
      
       
        t
       
      
     
    
    
     
      
       
        his.GetComponentsInChildren<MeshRenderer>().material.color;
       
      
     
    
   
    
     
      
       
        //获取先辈物体的指定类型组件(从自身开始)
       
      
     
    
   
    
     
      
       
        t
       
      
     
    
    
     
      
       
        his.GetComponentsInParent<MeshRenderer>().material.color;
       
      
     
    
   
     
   
     
   
     
   
    
     
      
       
        *
       
      
     
    
    
     
      
       
        Transform类
       
      
     
    
   
    
     
      
       
        Transform类提供了查找(父、根、子[根据索引和名称])变换组件、改变位置、角度、大小的功能;
       
      
     
    
   
    
     
      
       //child为每个子物体的变换组件
      
     
    
   
    
     
      
       f
      
     
    
    
     
      
       oreach(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
      
     
    
   
    
     
      
       t
      
     
    
    
     
      
       his.transform.Translate(0,0,1);
      
     
    
   
    
     
      
       //向世界坐标系Z轴移动1m
      
     
    
   
    
     
      
       t
      
     
    
    
     
      
       his.transform.Translate(0,0,1,space.World);
      
     
    
   
    
     
      
       //向自身坐标系Z轴旋转1度
      
     
    
   
    
     
      
       t
      
     
    
    
     
      
       his.transform.Rotate(0,0,1);
      
     
    
   
    
     
      
       //向世界坐标系Z轴旋转1度
      
     
    
   
    
     
      
       t
      
     
    
    
     
      
       his.transform.Rotate(0,0,1,space.World);
      
     
    
   
    
     
      
       //围绕旋转
      
     
    
   
    
     
      
       t
      
     
    
    
     
      
       his.transform.RotateAround(Vector3.zero,Vector3.up[y轴],1);
      
     
    
   
    
     
      
       //获取根物体变换组件
      
     
    
   
    
     
      
       t
      
     
    
    
     
      
       his.transform.root;
      
     
    
   
    
     
      
       //获取父物体变换组件
      
     
    
   
    
     
      
       t
      
     
    
    
     
      
       his.transform.parent;
      
     
    
   
    
     
      
       //设置父物体
      
     
    
   
    
     
      
       p
      
     
    
    
     
      
       ublic Transform TF;
      
     
    
   
    
     
      
       p
      
     
    
    
     
      
       ublic transform.SetParent(TF,(ture)[默认世界坐标]/false[自身坐标]);
      
     
    
   
     
   
    
     
      
       (
      
     
    
    
     ture世界坐标,false自身坐标(自身的坐标就是与cube5的距离)
    
    
     
      
       )
      
     
    
   
    
     
      
       //根据名称获取子物体
      
     
    
   
    
     
      
       Transform childTF=this.transform.Find(“子物体名称”);
      
     
    
   
    
     
      
       //根据索引获取子物体
      
     
    
   
    
     
      
       F
      
     
    
    
     
      
       or(int i=0;i<count;i++)
      
     
    
   
    
     
      
       {Transform childTF=this.transform.GetChild(i);}
      
     
    
   
     
   
     
   
     
   
     
   
     
   
    
     
      
       
        练习:在层级未知情况下查找子物体(递归)
       
      
     
    
   
     
   
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;
    }
    
}
     
   
    
     
      
       
        (效果图)
       
      
     
    
   
    
     
      
       
        *
       
      
     
    
    
     
      
       
        GameObject类
       
      
     
    
   
    
     
      
       
        //在场景中物体激活状态(物体实际激活状态)
       
      
     
    
   
    
     
      
       
        t
       
      
     
    
    
     
      
       
        his.gameObject.activeInHierarchy
       
      
     
    
   
    
     
      
       
        //物体自身激活状态(物体在Inspector面板中的状态)
       
      
     
    
   
    
     
      
       
        t
       
      
     
    
    
     
      
       
        his.gameObject.activeSelf;
       
      
     
    
   
     
   
    
     
      
       
        (activeInHierarchy——false,activeSelf——ture)
       
      
     
    
   
    
     
      
       
        //设置物体启用/禁用
       
      
     
    
   
    
     
      
       
        t
       
      
     
    
    
     
      
       
        his.gameObject.SetActive(ture/false);
       
      
     
    
   
    
     
      
       
        //创建物体
       
      
     
    
   
    
     
      
       
        GameObject lightGO=new GameObject();
       
      
     
    
   
    
     
      
       
        //添加组件
       
      
     
    
   
    
     
      
       
        Light light=lightGO.AddComponent<Light>();
       
      
     
    
   
    
     
      
       
        l
       
      
     
    
    
     
      
       
        ight.color=Color.red;
       
      
     
    
   
    
     
      
       
        l
       
      
     
    
    
     
      
       
        ight.type=LightType.Point;
       
      
     
    
   
    
     
      
       //在场景中根据名称查找物体(不建议使用)
      
     
    
   
    
     
      
       GameObject.Find(“游戏对象名称”);
      
     
    
   
    
     
      
       //获取所有使用该标签的物体
      
     
    
   
    
     
      
       GameObject[]allEnemy=GameObject.FindGameObjectsWithTag(“标签”);
      
     
    
   
    
     
      
       //获取使用该标签的物体(单个)
      
     
    
   
    
     
      
       GameObject player=GameObject.FindWithTag(“标签”);
      
     
    
   
     
   
     
   
    
     
      
       
        *
       
      
     
    
    
     
      
       
        Object类
       
      
     
    
   
    
     
      
       
        Destory 删除一个游戏对象、组件或资源
       
      
     
    
   
    
     
      
       
        //加载新场景时,使目标对象不被自动销毁
       
      
     
    
   
    
     
      
       
        DontDestoryOnLoad(transform.gameObject);
       
      
     
    
   
    
     
      
       
        //根据类型查找对象
       
      
     
    
   
    
     
      
       
        Object.FindObjectOfType<  >();
       
      
     
    
   
    
     
      
       
        练习:查找血量最低的敌人且改变颜色
       
      
     
    
   
    
     
      
       
        HP实例成员挂在物体中,就是一实例变量
       
      
     
    
   
    
     
      
       
        
       
      
     
    
   
    
     
      
       
        找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类
       
      
     
    
   
     
   
     
   
    
     
      一定要经常翻看UNITY文档呦!
     
    
   
    
     
      
       Unity User Manual 2021.3 (LTS) – Unity 手册
      
      
       
       https://docs.unity.cn/cn/current/Manual/index.html
      
     
    
   
 

 
     
     
    
