Unity中的笔记

常用组件
常用组件

Unity中常见的生命周期函数

1.Awake():该函数在脚本对象实例化时就立即调用,换句话说,对象初始化之后第一调用的函数就是Awake,当一个游戏对象上挂载了多个脚本,都带有awake,它是乱序的,没有固定顺序执行

2.onEable()(不常用):脚本被启用时调用一次

3.Start():则Start函数在第一帧更新之前被调用,意思就是在创建出游戏对象后,在第一帧刷新前被调用(也就是在update执行之前调用之前)

4.FixedUpdate():物理更新函数,不受电脑性能影响,每0.02秒(这里可以理解为每帧花0.02秒)更新一次(时间可以在首选项中进行更改)(FixedUpdte固定时间可修改: Edit-> Project Setting-> Time-> Fixed TimeStep)

5.Update():更新函数,每帧执行一次,受电脑性能影响

6.LateUpdate()(不常用):稍后更新函数,在每次Update函数更新完后立即更新,时间间隔和Update一样

7.Ondisable()(不常用):当脚本被禁用时调用-次,当脚本反复被禁用或启用,则反复调用OnDisable和OnEable

8.OnDestory():当脚本被销毁时披调用,只会在被激活的物体上调用

[x-title]向量[/x-title]

标量:只有大小的量。
向量:既有大小,也有方向。
向量的模:向量的大小。
单位向量:大小为1的向量。
单位化,归一化:把向量转为单位向量的过程。


加法

A(x1y1) + B(x2y2) = x1+x2,y1+y2 = 3,3

减法

A(x1y1) - B(x2y2) = x1-x2,y1-y2 = -1,1

乘法

A(x1y1) 2 = x12,y1*2

点乘

A . b = x1x2 + y1y2 = n = |a||b|cos&=cos& n=cos& &=

[x-title]Vector3的使用[/x-title]

void Start()
{
//向量,坐标,旋转,缩放
Vector3 v = new Vector3(1,1,0.5f);
v = Vector3.zero;
v = Vector3.right;

Vector3 V2 = Vector3.forward;

//计算俩个向量的夹角

Debug.Log("夹角"+Vector3.Angle(v,V2));
print("距离" + Vector3.Distance(v,V2));
print("点乘" + Vector3.Dot(v,V2));
print("叉乘" + Vector3.Cross(v, V2));
print("插值" + Vector3.Lerp(Vector3.zero,Vector3.one,0.8f));
print("模" + v.magnitude);
print("规范化的向量" + v.normalized);

}


[x-title]欧拉角&四元数[/x-title]

void Start()
{
//旋转 欧拉角&四元数
Vector3 OL = new Vector3(0,30,0);
Quaternion quaternion = Quaternion.identity;
//欧拉角转四元数
quaternion= Quaternion.Euler(OL);
//四元数转欧拉角
OL = quaternion.eulerAngles;
//看向一个物体
quaternion = Quaternion.LookRotation(new Vector3(0,0,0));

}


[x-title]线&射线[/x-title]

void Update()
{
    //绘制一条线
    Debug.DrawLine(new Vector3(1,1,0), new Vector3(0,0,0), Color.red);
    //射线
    Debug.DrawRay(Vector3.zero, Vector3.up, Color.blue);
}

[x-title]物体类的使用[/x-title]

void Start()
{

    //拿到当前脚本所挂载的游戏物体
    //GameObject go = this.gameObject;

    //名称
    //Debug.Log(gameObject.name);
    //标签
    //Debug.Log(gameObject.tag);
    //图层
    //Debug.Log(gameObject.layer);
    //打印立方体的名称
    //Debug.Log("立方体的名称" + LIFTI.name);
    //当前真正的激活状态
    //Debug.Log(LIFTI.activeInHierarchy);
    //当前的激活状态
    //Debug.Log(LIFTI.activeSelf);
    //获取transform组件
    //Debug.Log(transform.position);

    //获取其他组件
    //BoxCollider bc = GetComponent<BoxCollider>();
    //获取当前物体的子物体身上某个组件
    //GetComponentInChildren<CapsuleCollider>(bc);
    //获取当前物体的父物体身上的某个组件
    //GetComponentInParent<BoxCollider>();
    //添加组件
    //gameObject.AddComponent<AudioSource>();
    //通过游戏物体的名称获取游戏物体
    //GameObject test = GameObject.Find("Test");
    //通过游戏物体的标签获取游戏物体
    //test = GameObject.FindWithTag("dr");
    //Debug.Log("test.name");
    //通过预制体实例化一个游戏物体
    //GameObject C = Instantiate(P,Vector3.zero,Quaternion.identity);
    //销毁
    //Destroy(C);
}

[x-title]游戏时间的使用[/x-title]

public class test : MonoBehaviour
{
float timer = 0;
// Start is called before the first frame update
void Start()
{
//游戏开始到现在所花的时间
Debug.Log(Time.time);
//时间缩放值
Debug.Log(Time.timeScale);
//固定时间间隔
Debug.Log(Time.fixedDeltaTime);
//上一帧到这一帧所用的游戏时间
Debug.Log(Time.deltaTime);

}

// Update is called once per frame
void Update()
{
    timer += timer + Time.deltaTime;
    //Debug.Log(Time.deltaTime);
    //如果大于3秒
    if(timer > 3) 
    {
        print("3秒了");
        
    }
}

}


[x-title]application很重要[/x-title]

void Start()
{
//游戏数据
Debug.Log(Application.dataPath + "./新建文本文档 (6).txt");
//持久化文件夹路径
Debug.Log(Application.persistentDataPath);
//SteamingAssets文件夹路径
Debug.Log(Application.streamingAssetsPath);
//临时文件
Debug.Log(Application.temporaryCachePath);
//控制是否在后台运行
Debug.Log(Application.runInBackground);
//打开url
Application.OpenURL("HTTPS://WWW.AVBK.CN/");
//退出游戏
Application.Quit();
}


[x-title]场景切换[/x-title]

void Start()
{
    //俩个类。场景类,场景管理类
    //SceneManager.LoadScene("Myscene");
    //获取当前场景
    Scene scene = SceneManager.GetActiveScene();
    //场景名称
    Debug.Log(scene.name);
    //场景是否被加载了
    Debug.Log(scene.isLoaded);
    //场景索引
    Debug.Log(scene.buildIndex);
    GameObject[] gos = scene.GetRootGameObjects();
    Debug.Log(gos.Length);

    //场景管理类
    //Debug.Log(SceneManager.sceneCount);
    //创建新场景
    Scene newScene = SceneManager.CreateScene("newScene");
    //已加载场景个数
    Debug.Log(SceneManager.sceneCount);
    //卸载场景
    SceneManager.UnloadSceneAsync(newScene);
    //加载场景
    SceneManager.LoadScene("MyScene", LoadSceneMode.Additive);
    // Update is called once per frame

}


更新于: 2023年05月02日 15:43
145
0
发表评论