Unity-基础脚本

“Test_02”

VectorTest【向量】

public class VectorTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Vector3 v0, v1;
        // 【方法一】向量, 坐标,旋转,缩放
        Vector3 v = new Vector3(1,1,0.5f);
        // 【方法二】初始化一个全为0的向量(0,0,0)
        v0 = Vector3.zero;
        // 初始化一个全为1的向量(1,1,1)
        v1 = Vector3.one;
        // 改v向量值
        v.x = 0.5f;
        v.y = 0.6f;
        v.z = 0.7f;
        
        // 例子
        Vector3 v_x, v_z;
        v_x = Vector3.right;
        v_z = Vector3.forward;
        // 得到两个向量的夹角
        Debug.Log(Vector3.Angle(v_x,v_z));
        // 两点之间的距离
        Debug.Log(Vector3.Distance(v_x,v_z));
        // 向量的点乘
        Debug.Log(Vector3.Dot(v_x,v_z));
        // 向量叉乘
        Debug.Log(Vector3.Cross(v_x,v_z));
        // 插值
        Debug.Log(Vector3.Lerp(v_x,v_z, 0.5f));
        // 向量的模
        Debug.Log(v_x.magnitude);
        // 规范化向量
        Debug.Log(v_x.normalized);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

RotateTest【旋转】

public class RotateTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // 旋转: 欧拉角、四元数
        //   欧拉角(x,y,z)
        Vector3 rotate = new Vector3(0,30,0);
        //   四元数(x,y,z,w)
        Quaternion quaternion = new Quaternion(0,0,0,0);
        //     创建无旋转的四元数
        Quaternion quaternion1 = Quaternion.identity;
        //     将欧拉角转为四元数
        quaternion1 = Quaternion.Euler(rotate);
        //     将四元数转为欧拉角
        Vector3 rotate1 = quaternion1.eulerAngles;
        //   A看向某物体(1,1,1)时A的旋转
        Quaternion quaternion2 = Quaternion.identity;
        quaternion2 = Quaternion.LookRotation(new Vector3(1, 1, 1));
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

DebugTest【调试】

public class DebugTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("test");
        Debug.LogWarning("test2");
        Debug.LogError("test3");
    }

    // Update is called once per frame
    void Update()
    {
        // 绘制一条线【从(0,0,0)到(1,1,1)画一条蓝色的线】(起点、终点)
        Debug.DrawLine(new Vector3(1,0,0),new Vector3(1,1,0),Color.blue);
        // 绘制一条射线(起点、方向)【第二个方向坐标是将第一个起点坐标作为原点的坐标】
        Debug.DrawRay(new Vector3(1,0,0),new Vector3(1,1,0),Color.magenta);
    }
}

EmptyTest【物体设置】

目录结构

Test_02

public class EmptyTest : MonoBehaviour
{
    // 创建子子物体
    public GameObject Cube; 
    // 创建子物体
    public GameObject Plane;
    
    // 创建预设体
    public GameObject Prefab;
    
    // Start is called before the first frame update
    void Start()
    {
        // 拿到当前脚本所挂载的游戏物体
        // GameObject go = gameObject;
        // Debug.Log(go.name);
        // 物体名称
        Debug.Log(gameObject.name);
        // 物体标签
        Debug.Log(gameObject.tag);
        // 物体图层
        Debug.Log(gameObject.layer);
        // 获取物体Transform组件
        Debug.Log(transform.position);
        // 获取物体的除了Transform组件的其他组件【使用泛型】
        BoxCollider bc = GetComponent<BoxCollider>();
        Debug.Log(bc.center);
        // 获取物体的子物体上的组件
        CapsuleCollider cc = GetComponentInChildren<CapsuleCollider>();
        Debug.Log(cc.center);
        // 获取当前物体的父物体上的组件
        CapsuleCollider cc1 = Cube.GetComponentInParent<CapsuleCollider>();
        Debug.Log(cc1.center);
        // 在物体上添加组件
        gameObject.AddComponent<AudioSource>();
        
        
        // 在该脚本中通过物体名称获取项目中的其他物体
        GameObject test = GameObject.Find("Test");
        Debug.Log(test.name);
        // 在该脚本中通过物体标签获取项目中其他物体
        GameObject[] enemy = GameObject.FindGameObjectsWithTag("Enemy");
        Debug.Log(enemy.Length);
        // 在该脚本中设置其他物体的激活状态
        test.SetActive(false);

        
        // 子子物体名称
        Debug.Log(Cube.name);
        // 子子物体是否被激活(子物体未激活,子子物体的真正激活状态也是未激活)
        //   表示的是当前子子物体真正的激活状态
        Debug.Log(Cube.activeInHierarchy);
        //   表示子子物体自身激活状态的属性值
        Debug.Log(Cube.activeSelf);
        
        // 通过预设体来实例化物体, 并自定义物体位置和旋转
        GameObject go1 = Instantiate(Prefab, Vector3.zero, Quaternion.identity);
        // 实例化的物体作为当前脚本挂载物体的子物体
        GameObject go2 = Instantiate(Prefab, transform);
        // 销毁物体
        Destroy(go1);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

TimeTest【时间设置】

public class TimeTest : MonoBehaviour
{
    private float timer = 0;
    
    // Start is called before the first frame update
    void Start()
    {
        // 从开始到现在所花的时间
        Debug.Log(Time.time);
        // 时间缩放值(倍速,默认1.0)
        Debug.Log(Time.timeScale);
        // 固定时间间隔
        Debug.Log(Time.fixedDeltaTime);
       
    }

    // Update is called once per frame
    void Update()
    {
        // 上一帧到这一帧所用的时间
        // Debug.Log(Time.deltaTime);
        // 可用作计时器
        timer = timer + Time.deltaTime;
        if (timer > 3.0)
        {
            Debug.Log("大于3秒了");
            timer = 0;
        }
         
    }

    private void FixedUpdate()
    {
        
    }
}

ApplicationTest【文件路径】

public class ApplicationTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // 数据文件夹路径(如果要读取路径下的文件夹直接拼接即可)【只读,项目打包后内容会被加密压缩】
        Debug.Log(Application.dataPath);
        // 持久化文件夹路径(不同系统路径不同,一般在C盘)【可写】
        Debug.Log(Application.persistentDataPath);
        // StreamingAssets文件夹路径(配置文件)【只读,但项目打包后不会被隐藏加密的文件】
        Debug.Log(Application.streamingAssetsPath);
        // 临时文件夹【可写,可用于存档缓存】
        Debug.Log(Application.temporaryCachePath);
        // 控制应用在后台运行
        Debug.Log(Application.runInBackground);
        // 运行就打开url
        // Application.OpenURL("https://www.baidu.com");
        // 退出游戏
        // Application.Quit();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值