Unity【入门】重要组件和API

Unity重要组件和API

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

1、最小单位GameObject

1、成员变量
1、名字获取和修改
//print(gameObject.name);
//gameObject.name = "Test4_mod";
print(gameObject.name);
2、查看是否激活
print(gameObject.activeSelf);
3、查看是否为静态
print(gameObject.isStatic);
4、层级
print(gameObject.layer);
5、标签
print(gameObject.tag);
6、位置信息transform
print(transform.position);
2、静态方法
1、代码创建Unity自带几何体 CreatePrimitive
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.name = "TestCube";
2、查找对象
1、单个查找
    //通过对象名查找
        GameObject obj2 = GameObject.Find("TestCube");
        if (obj2 != null)
        {
   
            print(obj2);
        }
        else
        {
   
            print("查无此项");
        }

    //通过tag来查找
        GameObject obj3 = GameObject.FindWithTag("Player");
        if (obj3 != null)
        {
   
            print(obj3);
        }
        else
        {
   
            print("查无此项");
        }
    1、单个查找无法找到失活的对象
    2、存在多个满足条件时,无法精准找到
2、查找多个对象
    //通过tag找到多个对象(无法找到失活的对象)
        GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
        print(objs);
    //找到场景中挂载的某个脚本对象
        Test4 t = GameObject.FindObjectOfType<Test4>();
        print(t.gameObject.name);

Unity中的Object:
    命名空间在UnityEngine中,是集成万物之父的一个自定义类
C#中的object:
    命名空间在System中,两者不同
3、实例化对象(克隆对象)的方法
准备用来克隆的对象
	1、直接是场景上的某个对象
	2、可以是一个预设体对象
	public GameObject myObj;
		克隆动态创建,包含脚本等
		GameObject.Instantiate(myObj);
		//若继承了MonoBehavior,则Instantiate(myObj);
4、删除对象的方法
GameObject.Destroy(obj5);
延时删除
GameObject.Destroy(obj5,5);
删除指定脚本
Destroy(this.gameObject);
//Destroy会在下一帧移除
5、切换场景不移除
DontDestroyOnLoad(this.gameObject);
3、成员方法
1、创建空物体
GameObject obj = new GameObject();
GameObject obj1 = new GameObject("byCodeCreate"); //命名
GameObject obj2 = new GameObject("加脚本的物体",typeof(Test4)); //加脚本
2、为对象动态添加脚本(不能new)
Test4 ts3 = obj1.AddComponent(typeof(Test4)) as Test4;
Test4 ts4 = obj1.AddComponent<Test4>();
3、得到脚本
得到脚本的成员方法和继承Mono的类得到脚本的方法一样
4、标签比较
if (this.gameObject.CompareTag("Player"))
{
   
    print("对象的标签是Player");
}

if (this.gameObject.tag == "Player")
{
   
    print("对象的标签是Player");
}
5、设置激活失活(false失活,true激活)
obj1.SetActive(false);

了解其他的成员方法

//通知自己执行所有TestFun方法
gameObject.SendMessage("TestFun");
//广播自己及子类执行所有TestFun方法
gameObject.BroadcastMessage("TestFun");
//广播自己及父类执行所有TestFun方法
gameObject.SendMessageUpwards("TestFun");

2、时间相关Time

用于游戏中参与位移、计时、时间暂停等

1、时间缩放比例
//时间停止
Time.timeScale = 0;
//回复正常
Time.timeScale = 1;
//2倍速
Time.timeScale = 2;
2、帧间隔时间
概念:最近的一帧,用了多长时间(秒)
帧间隔时间,主要用来计算位移
//受timeScale影响
print(Time.deltaTime);
//不受timeScale影响
print(Time.unscaledDeltaTime);
3、游戏开始到现在的时间
print(Time.time);
print(Time.unscaledTime);
4、物理帧间隔时间 在FixedUpdate
设置步长:Edit -> Project Settiongs -> Time
void FixedUpdate()
{
   
    print(Time.fixedDeltaTime);
    print(Time.fixedUnscaledDeltaTime);
}
5、帧数
游戏从开始到现在跑了多少帧(循环)
print(Time.frameCount);

3、必不可少Transform

游戏对象(GameObject)位移、旋转、缩放、父子关系、坐标转换等相关操作都由它处理

它是Unity提供的及其重要的类

1、位置和位移
1、必备知识点Vector3基础
Vector3主要是用来表示三维坐标系中的一个点或者一个向量
声明
	Vector3 v = new Vector3();
	//只传x,y,默认z是0
    Vector3 v2 = new Vector3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值