unity-概念与实操入门

编译器推荐(Rider)

jet brain旗下软件,多好用不用我多说了
在这里插入图片描述
在这里插入图片描述

坐标

世界坐标系

这个球就位于世界坐标系0,0,0,也就是正中心
在这里插入图片描述

相对坐标系

相对于父Sphere的坐标系
在这里插入图片描述

资源商店快捷入口

在这里插入图片描述

地面制作

在这里插入图片描述
地形、山、树、草
在这里插入图片描述

脚本

新建脚本

第一种方式
在这里插入图片描述
第二种方式
在这里插入图片描述

生命周期

在这里插入图片描述

vs测试打印

在这里插入图片描述

对象

标签(自带集合属性)

在这里插入图片描述

图层

在这里插入图片描述
和摄像机这个属性对应,是否显示
在这里插入图片描述

预设体

创建预设体(cocos相同)

拖下来即可
在这里插入图片描述
在这里插入图片描述

定位预设体文件位置

在这里插入图片描述

预设体添加、更新新内容

在这里插入图片描述

预设体变体

只做了解,后期使用时再测试
在这里插入图片描述

结构体

vector

using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using Vector3 = UnityEngine.Vector3;

public class VectorTest : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        //向量,坐标,旋转,缩放
        Vector3 v = new Vector3();
        //创建结构体
        v = Vector3.zero;
        v = Vector3.right;

        Vector3 v2 = Vector3.forward;
        
        //计算两个向量夹角
        Debug.Log(Vector3.Angle(v,v2));
        //计算两点之间的距离
        Debug.Log(Vector3.Distance(v,v2));
        //点乘
        Debug.Log(Vector3.Dot(v,v2));
        //叉乘
        Debug.Log(Vector3.Cross(v,v2));
        //插值
        Debug.Log(Vector3.Lerp(Vector3.zero,Vector3.one,0.5f));
        //向量的模
        Debug.Log(v.magnitude);
        //规范化向量
        Debug.Log(v.normalized);
    }

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

在这里插入图片描述

对象旋转

欧拉角,四元数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateTest : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        //旋转:欧拉角,四元数
        Vector3 rotate = new Vector3(0,30,0);
        //创建初始化旋转0,0的欧拉角
        Quaternion quaternion = Quaternion.identity;
        //通过欧拉角转为四元数
        quaternion = Quaternion.Euler(rotate);
        //四元数转为欧拉角
        rotate = quaternion.eulerAngles;
        //看向一个物体
        quaternion = Quaternion.LookRotation(new Vector3(0, 0, 0));
    }

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

常用类

时间类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TimeTest : 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);
    }

    // Update is called once per frame
    void Update()
    {
    
    
        timer += Time.deltaTime;
        //上一帧到这一帧所用的游戏时间
        //Debug.Log(Time.deltaTime);
        //如果大于3秒
        if(timer > 3){
    
    
            Debug.Log("大于3秒了");
        }
    }
}

在这里插入图片描述

Application类(资源路径,网页跳转)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ApplicationTest : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        //游戏数据文件夹路径(只读,加密压缩)
        Debug.Log(Application.dataPath);    //D:\Study\GitCode\unity\01_project\Assets
        Debug.Log(Application.dataPath + "/测试文件夹");
        //持久化文件夹路径(不同平台的可存储文件的路径,不固定)
        Debug.Log(Application.persistentDataPath);
        //StramingAssets文件夹路径(只读,二进制文件加和配置文件)
        Debug.Log(Application.streamingAssetsPath); //D:\Study\GitCode\unity\01_project\Assets\streamingAssets
        //临时文件夹
        Debug.Log(Application.temporaryCachePath);  //C:/Users/29658/AppData/Local/Temp/DefaultCompany/01_project
        //控制是否在后台运行
        Debug.Log(Application.runInBackground);     //True(可以在buildSetting里设置后台运行关闭)
        //打开Url
        Application.OpenURL("https://www.baidu.com");
        //退出游戏
        // Application.Quit();
    }

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

在这里插入图片描述

场景切换

在这里插入图片描述

SceneLoad场景加载

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneTest : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        //注意LoadScene会导致加载卡顿
        //后期还是要使用LoadSceneAsync异步加载

        //两个类
        //场景类
        //场景切换
        // SceneManager.LoadScene(0);  //通过索引跳转
        // SceneManager.LoadScene("MyScene");   //通过名称跳转
        //获取当前场景
        Scene scene = SceneManager.GetActiveScene();
        //场景name
        Debug.Log(scene.name);
        //场景路径
        Debug.Log(scene.path);
        //场景索引
        Debug.Log(scene.buildIndex);
        //所有场景的数组
        GameObject[] gos = scene.GetRootGameObjects();
        Debug.Log(gos.Length);

        //场景管理类
        //创建新场景,动态创建场景,压缩内存占用
        SceneManager.CreateScene("newScene");
        //已加载场景个数,场景计数
        Debug.Log(SceneManager.sceneCount);
        //卸载场景
        SceneManager.UnloadSceneAsync("newScene");
        //场景替换
        //使MyScene替换当前场景,独立加载
        // SceneManager.LoadScene("MyScene",LoadSceneMode.Single);
        //场景叠加,同时加载,两个场景物体叠加
        SceneManager.LoadScene("MyScene",LoadSceneMode.Additive);
        
        
    }

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

异步场景加载

using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Serialization;
using UnityEngine;
using UnityEngine.SceneManagement;

public class AsyncTest : MonoBehaviour
{
    
    
    AsyncOperation operation;
    // Start is called before the first frame update
    void Start()
    {
    
    
        StartCoroutine(loadScene());
    }

    //携程方法用来异步加载场景
    IEnumerator loadScene(){
    
    
        operation = SceneManager.LoadSceneAsync("MyScene"); //参数可以用索引或者名称
        //实现加载完场景时不自动跳转,需要用户手动点击“进入游戏”
        operation.allowSceneActivation = false; 
        yield return operation;
    }

    private float timer = 0;
    // Update is called once per frame
    void Update()
    {
    
    
        //输出加载进度0-0.9(可以制作进度条)
        Debug.Log(operation.progress);
        //如果>=5秒,才可以跳转
        if (timer > 5)
        {
    
    
            operation.allowSceneActivation = true;
        }
        
    }
}

操作监听

键鼠监听

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KeyTest : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        //鼠标的点击
        //按下鼠标 0左键 1右键 2滚轮
        if (Input.GetMouseButtonDown(0))
        {
    
    
            Debug.Log("按下了鼠标左键");
        }
        //持续按下鼠标左键
        if (Input.GetMouseButton(0))
        {
    
    
            Debug.Log("持续按下鼠标左键");
        }
        
        //抬起鼠标左键
        if (Input.GetMouseButtonUp(0))
        {
    
    
            Debug.Log("抬起了鼠标左键");
        }
        
        //键盘操作
        //键盘按下A
        Input.GetKeyDown(KeyCode.A);
        //键盘持续按下A
        Input.GetKey(KeyCode.A);
        //抬起键盘按键A
        Input.GetKeyUp(KeyCode.A);

    }
}

虚拟轴

主要负责兼容问题
在这里插入图片描述
Edit-project setting - inputManager - Axes
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AxesTest : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        //获取水平轴
        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("空格");
        }

    }
}

触摸事件(手机触屏)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchTest : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        //开启多点触摸
        Input.multiTouchEnabled = true;
    }

    void Update()
    {
    
    
        //判断单点触摸
        if (Input.touchCount == 1)
        {
    
    
            //触摸对象
            Touch touch = Input.touches[0];
            //触摸位置
            Debug.Log(touch.position);
            //触摸阶段
            switch (touch.phase)
            {
    
    
                case TouchPhase.Began:
                    break;
                case TouchPhase.Moved:
                    break;
                case TouchPhase.Stationary:
                    break;
                case TouchPhase.Ended:
                    break;
                case TouchPhase.Canceled:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
        
        //判断多点触摸
        if (Input.touchCount == 2)
        {
    
    
            Touch touch = Input.touches[0];
            Touch touch1 = Input.touches[1];
        }
    }
}

light(灯光)

directional(定向光)

在这里插入图片描述

spot(手电筒)

在这里插入图片描述

point(灯泡)

在这里插入图片描述

阴影

在这里插入图片描述

碰撞器、碰撞回调

在这里插入图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CollierCallBack : MonoBehaviour
{
    
    
    //创建一个预设体
    public GameObject PrefabTest;
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
    }
    
    //监听发生碰撞
    private void OnCollisionEnter(Collision collsion)
    {
    
    
        //落地后创建生成的预设体
        Instantiate(PrefabTest, transform.position, Quaternion.identity);
        //销毁自身
        Destroy(gameObject);
        //获取碰撞到的物体
        Debug.Log(collsion.gameObject.name);

    }
    
    //持续碰撞中
    private void OnCollisionStay(){
    
    
    
    }

    //碰撞结束
    private void OnCollisionExit(Collision other)
    {
    
    
        
    }
}

触发器

   //触发开始
    private void OnTriggerEnter(Collider other)
    {
    
    
        GameObject door = GameObject.Find("Door");
        if (door != null)
        {
    
    
            //隐藏door.object
            door.SetActive(false);
        }
    }

    //正在触发
    private void OnTriggerStay(Collider other)
    {
    
    
        throw new NotImplementedException();
    }

    //触发结束
    private void OnTriggerExit(Collider other)
    {
    
    
        throw new NotImplementedException();
    }

射线检测(不懂)

点击地面,使球移动到鼠标点击的位置
在这里插入图片描述

using UnityEngine;

public class RayTest : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        //方式1,发射射线
        Ray ray = new Ray(Vector3.zero, Vector3.up);
    }

    // Update is called once per frame
    void Update()
    {
    
    
        //if按下鼠标左键
        if (Input.GetMouseButton(0))
        {
    
    
            //方式2,发射射线
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //声明一个碰撞信息类
            RaycastHit hit;
            //碰撞检测
            bool res = Physics.Raycast(ray,out hit);
            //如果碰撞到的情况下,hit就有内容了
            if (res)
            {
    
    
                Debug.Log(hit.point);
                transform.position = hit.point;
            }
            //多检测
            // RaycastHit[] hits = Physics.RaycastAll(ray);
        }
    }
}

粒子系统(需要单门学习了)

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_39123467/article/details/128502955
今日推荐