Unity 向量计算、欧拉角与四元数转换、输出文本、告警、错误、修改时间、定时器、路径、

小明 2025-05-02 03:11:00 7
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class c2 : MonoBehaviour
{
    // 定时器
    float t1 = 0;
    void Start()
    {
        // 向量
        Vector3 v1 = new Vector3(0, 0, 2);
        Vector3 v2 = new Vector3(0, 0, 3);
        // 计算两个向量的夹角
        Debug.Log(Vector3.Angle(v1, v2));
        // 计算向量的模
        Debug.Log(v2.magnitude);
        // 两点之间的距离 (计算v1、v2 两个点之间的距离) 
        Debug.Log(Vector3.Distance(v1, v2));
        // 计算过程时,参数1 + (参数2 - 参数1)* 参数3
        // 插值 (0,0,0)  (0,0,1)   (0.1f) = 过程:0+ ((1-0)*0.1)  = (0,0,0.1)
        // 插值 (0,0,2)  (0,0,2)   (0.2f) = 过程:2+((2-2)*0.2) = (0,0,0.2)
        // 插值 (0,0,2)  (0,0,2)   (0.2f) = 过程:2+((2-2)*0.2) = (0,0,0.2)
        // 插值 (3,6,1)  (9,5,2)   (0.1f) =
        // 过程:3+ ((9-3)*0.1) = 3.6
        // 过程:6+ ((5-6)*0.1) = 5.9
        // 过程:1+ ((2-1)*0.1) = 1.1
        // 结果:(3.6,5.9,1.1)
        Debug.Log(Vector3.Lerp(new Vector3(3,6,1), new Vector3(9,5,2),0.1f));
        // 欧拉角 x y z
        // 四元数 x y z w
        // 欧拉角
        Vector3 rotate = new Vector3(60, 50, 0);
        // 四元数
        Quaternion quaternion = Quaternion.identity;
        // 欧拉角 转 四元数
        quaternion = Quaternion.Euler(rotate);
        Debug.Log("欧拉角 转 四元数");
        Debug.Log(quaternion);
        // 四元数 转 欧拉角
        Debug.Log("四元数 转 欧拉角");
        Debug.Log(quaternion.eulerAngles);
        // 这是一个朝向敌人的向量
        Vector3 dir = Vector3.left;
        // 获得一个朝向这个向量的旋转
        quaternion = Quaternion.LookRotation(dir);
        // 输出文本
        Debug.Log("输出文本");
        // 输出警告
        Debug.LogWarning("输出警告");
        // 输出错误
        // Debug.LogError("输出错误");
        // 时间相关
        // 游戏开始到现在所用时间
        // Debug.Log(Time.time);
        // 在编辑中-》项目设置-》时间:(时间尺度、固定时间步进)
        // 时间尺度:时间缩放数值
        Debug.Log(Time.timeScale);
        // 修改 时间尺度 (增加重量组件可以看效果)
        // Time.timeScale = 0.1f;
        // 固定时间步进:固定时间间隔
        Debug.Log(Time.fixedDeltaTime);
        // 路径相关
        // 找到 Assets 路径下的 某文件 (可读 某些不可写)若PC端可读可写
        Debug.Log(Application.dataPath + "/test.txt");
        // 持久化路径 可读可写  默认C盘
        Debug.Log(Application.persistentDataPath);
        // 在Asset文件下的 StreamingAssrts 文件夹内的文件 不会被加密(适合放配置文件)
        // 找到 在Asset文件下的 StreamingAssrts路径
        Debug.Log(Application.streamingAssetsPath);
        // 在Asset文件下的 Resources 文件夹内的文件 加载比较方便
        // 找到 临时文件 路径
        Debug.Log(Application.temporaryCachePath);
        // 判断是否 后台运行
        Debug.Log(Application.runInBackground);
        // 打开一个网址
        // Application.OpenURL("http://baidu.com");
        // 退出 (好像有问题,后面再尝试一下)
        // Application.Quit();
    }
    // Update is called once per frame
    void Update()
    {
        // 帧之间的间隔时间(跟硬件相关)
        // Debug.Log(Time.deltaTime);
        // 计时器
        t1 += Time.deltaTime;
        if (t1 >= 5)
        {
            Debug.Log("5秒到了");
            t1 = 0;
        }
    }
}

The End
微信