unity学习笔记

25 Application

		游戏数据文件夹路径  只读,加密压缩
        Debug.Log(Application.dataPath);
        持久化文件夹路径 可写 可储存文件的路径
        Debug.Log(Application.persistentDataPath);
        StreamingAssets文件夹路径(只读,把不需要加密压缩的放到此文件夹下 )
        Debug.Log(Application.streamingAssetsPath);
        临时文件夹
        Debug.Log(Application.temporaryCachePath);
        控制是否在后台运行
        Debug.Log(Application.runInBackground);
        打开url
        Application.OpenURL("www.baidu.com");
        退出游戏
        Application.Quit();

26 场景类的使用

//两个类  场景类,场景管理类(先添加场景)
        //场景跳转
        通过场景索引
        SceneManager.LoadScene(0);
        通过场景名称
        SceneManager.LoadScene("MyScene");
        获取当前场景
        Scene scene = SceneManager.GetActiveScene();
        场景名称
        Debug.Log(scene.name);
        场景是否已经被加载
        Debug.Log(scene.isLoaded);
        //场景路径
        Debug.Log(scene.path);
        场景索引
        Debug.Log(scene.buildIndex);
        得到当前游戏场景的所有游戏物体(返回数组)
        GameObject[] gos = scene.GetRootGameObjects();
        Debug.Log(gos.Length);

        //场景管理类
        创建新场景
        Scene scene1=SceneManager.CreateScene("NewScene");
        当前已加载场景的数量
        Debug.Log(SceneManager.sceneCount);
        卸载场景
        SceneManager.UnloadSceneAsync(scene1);
        加载新场景 删除旧场景的加载
        SceneManager.LoadScene("MyScene",LoadSceneMode.Single);
        加载新场景,叠加旧场景(可能会产生卡顿)
        SceneManager.LoadScene("MyScene", LoadSceneMode.Additive);
    }

27 异步加载场景并获取进度

   AsyncOperation operation;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(loadScene());
    }
    //协程方法用来异步加载场景
    IEnumerator loadScene()
    {
        operation = SceneManager.LoadSceneAsync(0);
        //加载完场景不要自动跳转
        operation.allowSceneActivation = false;
        yield return operation;
    }
    float timer = 0;

    // Update is called once per frame
    void Update()
    {
        //输出加载进度
        Debug.Log(operation.progress);
        timer += Time.deltaTime;
        if (timer > 5)
        {
            operation.allowSceneActivation = true;
        }
    }
}

28 transform

 void Start()
    {
        
        //获取位置
        Debug.Log(transform.position);     //在世界中的位置
        Debug.Log(transform.localPosition);  //相对于父物体的位置(在面板中的位置)
        //获取旋转
        Debug.Log(transform.rotation);     //相对于世界的旋转
        Debug.Log(transform.localPosition);  //相对于父物体的旋转
        Debug.Log(transform.eulerAngles);  //相对于世界的欧拉角度(返回verctor3三维向量  代表三个角度的旋转)
        Debug.Log(transform.localEulerAngles);//相对于父物体的欧拉角
        //获取缩放
        Debug.Log(transform.localScale);//相对父物体的缩放
        //向量
        Debug.Log(transform.forward);
        Debug.Log(transform.right);
        Debug.Log(transform.up);
        
        //父子关系
        //获取父物体
        transform.parent.gameObject
        //获取子物体
        Debug.Log(transform.childCount);
        //解除与子物体的父子关系
        transform.DetachChildren();
        //获取子物体
        Transform trans=transform.Find("Child");
        trans.GetChild(0);//获取第0个子物体
        //判断一个物体是不是另外一个物体的子物体
        bool res = trans.IsChildOf(transform);
        //设置为父物体
        trans.SetParent(transform);
    }

    // Update is called once per frame
    void Update()
    {
        //时时刻刻看向(0,0,0)
        transform.LookAt(Vector3.zero);
        //旋转
        transform.Rotate(Vector3.up,1);
        //绕某个物体旋转
        transform.RotateAround(Vector3.zero,Vector3.up,1);
        //移动
        transform.Translate(Vector3.forward * 0.1f);
    }

29 键盘鼠标按键的捕捉

//鼠标的点击
        //按下鼠标左键 0左键 1右键 2滚轮
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("鼠标左键点击了");
        }
        //持续按下载鼠标
        if (Input.GetMouseButton(0))
        {
            Debug.Log("持续按下鼠标左键");
        }
        //抬起鼠标
        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("抬起了鼠标左键");
        }

        //如果使用字符 区分大小写,keycode不区分大小写

        //按下键盘按键
        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("按下了A");
        }
        //持续按下键盘按键
        if (Input.GetKey(KeyCode.A))
        {
            Debug.Log("持续按下A");
        }
        //抬起键盘按键
        if (Input.GetKeyUp(KeyCode.A)){
            Debug.Log("抬起了A");
        }

30 获取虚拟轴和虚拟按键

//获取水平轴
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Debug.Log(horizontal+"    "+vertical);
        //虚拟按键
        if (Input.GetButtonDown("Jump"))
        {
            Debug.Log("空格按下");
        }
        if (Input.GetButton("Jump"))
        {
            Debug.Log("空格");
        }
        if (Input.GetButtonUp("Jump"))
        {
            Debug.Log("空格抬起");
        }

31 获取触摸

void Start()
    {
        //开启多点触摸
        Input.multiTouchEnabled = true;
    }

    // Update is called once per frame
    void Update()
    {
        //判断单点触摸
        if (Input.touchCount == 1)
        {
            //触摸对象
            Touch touch = Input.touches[0];
            //触摸位置
            Debug.Log(touch.position);
            //触摸阶段
            switch (touch.phase)
            {
                case TouchPhase.Began:
                    Debug.Log("触摸开始");
                    break;
                case TouchPhase.Moved:
                    break;
                case TouchPhase.Stationary:
                    break;
                case TouchPhase.Ended:
                    break;
                case TouchPhase.Canceled:
                    break;
            }
        }
        if (Input.touchCount == 2)
        {
            Touch touch = Input.touches[0];
            Touch touch1 = Input.touches[1];
            switch (touch.phase)
            {
                case TouchPhase.Began:
                    Debug.Log("触摸开始");
                    break;
                case TouchPhase.Moved:
                    break;
                case TouchPhase.Stationary:
                    break;
                case TouchPhase.Ended:
                    break;
                case TouchPhase.Canceled:
                    break;
            }
            switch (touch1.phase)
            {
                case TouchPhase.Began:
                    Debug.Log("触摸开始");
                    break;
                case TouchPhase.Moved:
                    break;
                case TouchPhase.Stationary:
                    break;
                case TouchPhase.Ended:
                    break;
                case TouchPhase.Canceled:
                    break;
            }
        }
    }

34 游戏音效

public class AudioTest : MonoBehaviour
{
    //AudioClip
    //获取两个音频
    public AudioClip music;
    public AudioClip se;
    //获取播放器组件
    private AudioSource player;
    // Start is called before the first frame update
    void Start()
    {
        player = GetComponent<AudioSource>();
        //设定播放音频片段
        player.clip = music;
        //循环
        player.loop = true;
        //音量
        player.volume = 0.5f;
        //播放
        player.Play();
    }

    // Update is called once per frame
    void Update()
    {
        //按空格切换声音的播放与暂停
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //如果当前正在播放声音
            if (player.isPlaying)
            {
                //暂停播放
                player.Pause();
                //停止播放
                //player.Stop();
            }
            else
            {
                //开始播放(解除暂停)
                player.UnPause();
                //重头开始播放
                //player.Play();
            }
        }
        //按鼠标左键播放音效
        if (Input.GetMouseButtonDown(0))
        {
            player.PlayOneShot(se);
        }
    }
}

37 角色控制器

private CharacterController player;
    // Start is called before the first frame update
    void Start()
    {
        player = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        //水平轴
        float horizontal = Input.GetAxis("Horizontal");
        //垂直轴
        float vertical = Input.GetAxis("Vertical");
        //创建成一个方向向量
        Vector3 dir = new Vector3(horizontal,0,vertical);
        //Debug.DrawRay(transform.position,dir,Color.red);
        //朝该方向移动
        player.SimpleMove(dir*2);

    }

39 碰撞的触发与监听

public class FireTest : MonoBehaviour
{
    //创建一个爆炸的预设体
    public GameObject prefab;
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

    }
    //监听发生碰撞
    private void OnCollisionEnter(Collision collision)//colsion  是碰撞到的物体
    {
        //获取碰撞到的物体
        Debug.Log(collision.gameObject.name);
        //创建一个爆炸物体
        Instantiate(prefab,transform.position,Quaternion.identity);
        //销毁自身
        Destroy(gameObject);
    }
    //持续碰撞中
    private void OnCollisionStay(Collision collision)
    {
        
    }
    //结束碰撞
    private void OnCollisionExit(Collision collision)
    {
        
    }
}

39 触发

//触发开始
    private void OnTriggerEnter(Collider other)//Collider进入触发的碰撞器
    {
        GameObject door = GameObject.Find("door");
        if (door!=null)
        {
            door.SetActive(false);
        }
    }
    //正在触发
    private void OnTriggerStay(Collider other)
    {
        
    }
    //触发结束
    private void OnTriggerExit(Collider other)
    {
        
    }

42 射线检测

void Start()
    {
        //创建射线
        //1
        //Ray ray = new Ray(Vector3.zero,Vector3.up);
       
    }

    // Update is called once per frame
    void Update()
    {
        //2
        if (Input.GetMouseButtonDown(0))//按下鼠标左键发射射线
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //声明一个碰撞信息类
            RaycastHit hit;
            //碰撞检测
            bool res = Physics.Raycast(ray, out hit);
            //如果碰撞到物体了,hit就有内容了
            if (res == true)
            {
                Debug.Log(hit.point);//打印碰撞坐标
                transform.position = hit.point;
            }
            //多检测(返回一个数组)
            //RaycastHit[] hits = Physics.RaycastAll(ray, 100,1<<10);//射线是无限远,设定一个物体,只检测前十层

        }

45 动画

void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            GetComponent<Animation>().Play("left_right");
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

nan_black

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

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

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

打赏作者

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

抵扣说明:

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

余额充值