学习Unity:day 15 2023/9/22

一、Rigidbody类

AddForce
如:if(Input.GetMouseButtonDown(0))
        {
            rig.AddForce(0, 500, 0);//刚体增加力
        }

二、Random类

Range()
如:Random.Range(0,10);//第二个数为整数:能取到0 取不到10       [0,1)
       Random.Range(0, 10.0f);  //第二个数为浮点数:能取到0 取到10  [0,1.0f]

三、Application类

1、退出项目
        Application.Quit();
2、跳转场景(已弃用)
        Application.LoadLevel("WWW");
Application.LoadLevel(1);
3、打开链接方法
        Application.OpenURL("https://www.baidu.com/");
4、四种不同的path
都为只读
1)输出当前项目的Assets目录
     Application.dataPath
2)Assets目录下StreamingAssets文件夹目录
     Application.streamingAssetsPath
3)persistentDataPath和temporaryCachePath的路径位置一般是相对所在系统的固定位置
      Application.persistentDataPath(可读可写)
4)Application.temporaryCachePath

四、SceneManager类

1、跳转场景

SceneManager.LoadScene("WWW");
SceneManager.LoadScene(1);

2、获取场景名称

SceneManager.GetActiveScene().name
Debug.Log("返回场景的名称:" + SceneManager.GetActiveScene().name);

3、获取场景索引

GetActiveScene().buildIndex
Debug.Log("返回 Build Settings 中场景的索引:" + SceneManager.GetActiveScene());

4、注意

一定要把场景加入到Build Settings中,不然获取的是空值

五、Quaternion类

1、Euler()方法

Euler通过欧拉角创建一个四元数:主要用来赋值旋转角度
transform.rotation = Quaternion.Euler(30,30,30)

2、identity属性

Quaternion.identity:单位旋转,相当于没有旋转

六、ray类(必须会)

1、方法

Ray :参加碰撞检测的射线

RaycastHit: 射线的碰撞信息

maxDistance:射线的长度

layerMask:射线遮罩
1)检测哪一层
2)不检测哪一层

2、例子(四个)

例1:Vector3 vStart = transform.position;
        Vector3 vEnd = vStart + new Vector3(0, 0, 100);
        Ray ray = new Ray(vStart, vEnd);
        Debug.DrawLine(vStart, vEnd, Color.red);//画出射线
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            Debug.Log("名字:" + hit.collider.name);//射线碰撞物体的名字
            Debug.Log("碰撞坐标:" + hit.point);//射线碰撞的坐标
            Debug.Log("法线:" + hit.normal);//射线命中的表面的法线
            Debug.Log("距离:" + hit.distance);//距离
            Debug.Log("碰撞物体:" + hit.collider.gameObject);//获取碰撞物体
        }

例2: if (Input.GetMouseButtonDown(0))//鼠标按下时发射摄像
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))//如果发生碰撞,返回碰撞信息hit
            {
                Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red);
                Debug.Log(hit.collider.name);//射线碰撞物体的名字
            }
        }

例3://射线另一种写法
        Ray ray = new Ray(transform.position, transform.forward);
        Debug.DrawLine(transform.position, transform.forward * 100, Color.blue);
        //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        //if (Physics.Raycast(ray, out hit, 100, ~512))//100是射线的长度
        if (Physics.Raycast(ray, out hit, 100, ~(1 << 5)))//100是射线的长度
        {
            Debug.Log(hit.collider.name);
        }
        //0000000000000000000000000000011
        //必须加上最大长度
        //(1 << layerNumber)只检测某一层(layerNumber表示layer层的标号)
        //~(1 << layerNumber)不检测某一层,其余都检测
        //(1 << layerNumber)|(1 << layer2Number)只检测某2个层

例4:

if (Input.GetMouseButtonDown(0))
        {
            //射线另一种写法
            Ray ray = new Ray(transform.position, transform.forward);
            Debug.DrawLine(transform.position, transform.forward * 100, Color.blue);
            RaycastHit[] hits;
            //返回所有碰撞信息
            hits = Physics.RaycastAll(ray);
            //增加射线碰撞距离
            hits = Physics.RaycastAll(ray, 100);
            //增加检测层
            hits = Physics.RaycastAll(ray, 100, 1 << 10);

            //输出所有物体碰撞信息
            foreach (var item in hits)
            {
                Debug.Log(item.collider.name);
            }
        }

七、Screen类

1、当前的分辨率

Screen.currentResolution

2、全屏设置

Screen.fullScreen = true;

3、屏幕窗口的当前高度(以像素为单位)

Screen.height

4、屏幕窗口的当前高度(以像素为单位)

Screen.width

5、切换屏幕分辨率

Screen.SetResolution(800,400,true)

6、屏幕自适应

(开发中)当前屏幕高/当前屏幕宽=发布后屏幕高/发布后的屏幕宽

八、Mathf类

用来计算

例:Debug.Log("圆周率" + Mathf.PI);
        //绝对值
        Debug.Log(Mathf.Abs(-10.5f));
        //返回最大值
        Debug.Log(Mathf.Max(1.2f, 2.4f));
        //返回次幂
        Debug.Log(Mathf.Pow(6, 1.8f));
        //返回平方根
        Debug.Log(Mathf.Sqrt(3 * 3 + 4 * 4));

九、Gizmos类

1、Gizmos 菜单中方法

例:public Transform target;
    void OnDrawGizmosSelected()
    {
        if (target != null)
        {
            Gizmos.color = Color.blue;
            Gizmos.DrawLine(transform.position, target.position);
        }
    }

2、注意

编辑器未运行,方法时运行

十、协同

1、定义协程

IEnumerator+方法名
yield return+.....

2、调用协程

1)字符串

StartCoroutine("MyFun");//调用协程


2)方法

StartCoroutine(MyFun());//调用协程
注意:StopCoroutine(MyFun());//终止不了协程

3、终止协程

     StopCoroutine("MyFun");//终止协程

4、终止所有协程

     StopAllCoroutines();//终止所有协程

5、等待时间

     yield return new WaitForSeconds(5);//5秒以后执行

6、另一种写法

例://另一种写法
        IEnumerator myFun;//定义一个协程变量
        myFun = MyFun();//赋值方法
        StartCoroutine(myFun);
        StopCoroutine(myFun);
    IEnumerator MyFun()
    {
        Debug.Log("111111协同被调用0");
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("111111协同被调用1");
        yield return null;
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("111111协同被调用2");
    }

7、注意

1)StartCoroutine("MyFun");//调用协程
     StartCoroutine("MyFun2");//调用协程
     MyFun正在执行中,开启另一个MyFun2

例:void Start()
    {
        StartCoroutine("MyFun");//调用协程
        StartCoroutine("MyFun2");//调用协程
    }
    IEnumerator MyFun()
    {
        Debug.Log("MyFun调用");
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun被调用1");
        yield return null;
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun被调用2");
    }
    IEnumerator MyFun2()
    {
        Debug.Log("MyFun2被调用");
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun2被调用1");
        yield return null;
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun2被调用2");
    }

2)yield return MyFun();
      yield return MyFun2();
      MyFun()执行完执行MyFun2()
例:void Start()
    {
        StartCoroutine("MyFun");//调用协程
    }
    IEnumerator MyFun()
    {
        yield return MyFun2();
        yield return MyFun3();
    }
    IEnumerator MyFun2()
    {
        Debug.Log("MyFun2被调用");
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun2被调用1");
        yield return null;
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun2被调用2");
    }
    IEnumerator MyFun3()
    {
        Debug.Log("MyFun3被调用");
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun3被调用1");
        yield return null;
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun3被调用2");
    }

十一、场景中的鼠标事件

1、鼠标按下

    private void OnMouseDown()
    {
        Debug.Log("鼠标按下");
    }

2、鼠标抬起

    void OnMouseUp()
    {
        Debug.Log("鼠标抬起");
    }

3、鼠标在按下时拖动

    private void OnMouseDrag()
    {
        Debug.Log("鼠标按下时拖动");
    }

4、鼠标进入

    private void OnMouseEnter()
    {
        Debug.Log("鼠标进入");
    }

5、鼠标离开

    void OnMouseExit()
    {
        Debug.Log("鼠标离开");
    }

6、鼠标经过

    void OnMouseOver()
    {
        Debug.Log("鼠标经过");
    }

7、鼠标按下抬起时调用

    void OnMouseUpAsButton()
    {
        Debug.Log("鼠标按下抬起时调用");//单击
    }

  • 数据持久化

一、Resources类

1、定义

允许您查找和访问资源等对象。

2、使用

img = Resources.Load("body_02") as Texture;
img = Resources.Load("01/body_03") as Texture;
Sphere = Resources.Load("Sphere") as GameObject;

3、注意

1)Resources文件夹里存放的资源不能超过2g
2)把资源加载到内存并不在场景里,如果需要使用,需要实例化到场景中

二、玩家偏好

1、作用

存储小型数据,把数据存在注册表中

2、存

        PlayerPrefs.SetFloat("01", 15.5f);
        PlayerPrefs.SetInt("ing", 14);
        PlayerPrefs.SetString("name", "zhangsan");

3、取

        Debug.Log(PlayerPrefs.GetFloat("01"));
        Debug.Log(PlayerPrefs.GetInt("ing"));
        Debug.Log(PlayerPrefs.GetString("name"));

4、注意

        PlayerPrefs.SetInt("name", 3);
        PlayerPrefs.SetFloat("name", 3.3f);
        PlayerPrefs.SetString("name", "zhangsan");
        PlayerPrefs.GetInt("name");
        PlayerPrefs.GetFloat("name");
        PlayerPrefs.GetString("name");
    只有PlayerPrefs.GetString("name");有值

5、删除所有键和值

PlayerPrefs.DeleteAll();//请谨慎使用

6、从偏好中删除 key 及其对应值

PlayerPrefs.DeleteKey("name");

7、有没有键,返回true或false

PlayerPrefs.HasKey("name");

8、保存

PlayerPrefs.Save();

9、属于与玩家偏好结合使用

例:public string Name
    {
        set { PlayerPrefs.SetString("name",value); }
        get { return PlayerPrefs.GetString("name"); }
    }
    void Start()
    {
        //存
        Name = "张三";
        //取
        Debug.Log(Name);
   }

三、Vector类

1、定义一个向量

Vector3 v3 = new Vector3(0,0,0);

2、简写

        Debug.Log("Vector3.up:" + Vector3.up);// 0  1  0
        Debug.Log("Vector3.down:" + Vector3.down);//0  -1  0
        Debug.Log("Vector3.left:" + Vector3.left);//-1  0  0
        Debug.Log("Vector3.right:" + Vector3.right);//1  0  0
        Debug.Log("Vector3.forward:" + Vector3.forward);//0 0 1
        Debug.Log("Vector3.back:" + Vector3.back);//0 0 -1
        Debug.Log("Vector3.one:" + Vector3.one);//1 1 1
        Debug.Log("Vector3.zero:" + Vector3.zero);//0 0 0

3、返回向量长度

例:Vector3 v1 = new Vector3(1, 1, 1);
        Vector3 v2 = new Vector3(2, 2, 2);
        float f1 = Vector3.Magnitude(v2 - v1);
        //float f2 = Vector3.Magnitude(v1 - v2);
        Debug.Log("返回向量长度" + f1);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顾筱黎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值