hello,大家好。我叫龙骑吖。

接下来我们将一步一步地学习这个项目。

03-设置源代码控制

  1. 创建仓库

  2. 选择Unity版本 2023以上

  3. 创建SRP通用项目

  4. 首次提交

ignore文件分析

  • 斜杆 / :从项目的根目录开始匹配

    • 假设你项目的根目录为:H:\Unity Pro\肉鸽幸存者\RouGeXIngCunZhe

    • /library表示H:\Unity Pro\肉鸽幸存者\RouGeXIngCunZhe\library

  • [Ll] :表示可以 使用大写L,也可以使用小写l

  • * 通配符:匹配当前目录下所有以 xxx 结尾的文件,不会匹配子目录

    • *.csproj:匹配所有以.csproj结尾的文件。

    • *.tmp:匹配所有以.tmp结尾的文件,但不会匹配子目录中的文件。例如,它会匹配file.tmp,但不会匹配subdir/file.tmp

# Unity默认忽略路径
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Recordings can get excessive in size
/[Rr]ecordings/

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.aab
*.unitypackage
*.app

# Crashlytics generated file
crashlytics-build.properties

# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*

# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*

第一次使用Unity6问题

Unity.exe - Warning
Unity requires Windows 10 Version 21H1 (build 19043) or newer. The operating system detected does not meet this requirement. This may lead to unexpected behavior. Do you wish to continue?
是(Y) 否(N)
  • Unity编辑器要求运行在Windows 10 Version 21H1(版本号为19043)或更高版本的操作系统上。

  • 当前检测到的操作系统版本不满足这个要求。

  1. 可能的影响

    • 如果继续使用当前版本的操作系统,可能会导致Unity编辑器出现意外行为或不稳定的情况。

  2. 选择

    • 是(Y):继续使用当前版本的Unity编辑器,尽管可能存在风险。

    • 否(N):停止启动Unity编辑器,避免潜在的问题。

官方解释:

根据您提供的信息,Unity要求Windows 19043或更新的版本,而您当前的系统版本是Windows 10 19041。虽然安装最新版本后出现了这个错误,但实际上这个错误不会影响您的使用,您可以跳过这个错误继续运行Unity。这个错误只是一个警告,不会对您的项目运行产生实质性影响。

如果您想要解决这个警告,您可以尝试更新您的Windows系统到19043或更新的版本。但如果您选择跳过这个错误,您仍然可以继续使用Unity进行项目开发,不会受到太大影响。

04-导入文件

  • 构建安卓平台

  • 导入资源

  • 设置你喜欢的窗口排列

05-我向你介绍,戴夫

  • 拖入戴夫

  • 设置精灵为单张图片

06-将物理应用于Dave

  • 创建PlayerController脚本让戴夫移动

新版本RigidBody2D的velocity变更

使用rb.linearVelocity来获取或设置刚体(Rigidbody)的线性速度。线性速度是物体在世界空间中的移动速度,不包括由于旋转产生的任何速度分量。

using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class PlayerController : MonoBehaviour
{

    private Rigidbody2D rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        rb.linearVelocity = Vector3.right;
    }

    private void Update()
    {
        
    }
}

07-使用操纵杆控制 Dave

  • 将代码提交

  • 设置画布分辨率

  • 添加操纵杆

  • 通过操纵杆移动Dave

画布分辨率

注意:手机竖屏模式的分辨为:1080 × 1920

UI Scale Mode:

  • Scale With Screen Size:这种模式下UI元素会跟随屏幕分辨率的变化而自动调整大小

Screen Match Mode

  • Match Width Or Height:决定了UI元素如何适应不同的屏幕尺寸

    • Width:决定UI会优先匹配屏幕宽度,高度则会根据宽度的比例进行调整

通过操纵杆移动Dave

using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerController : MonoBehaviour
{
    [SerializeField] MobileJoystick mobileJoystick;
    [SerializeField] float moveSpeed;

    private Rigidbody2D rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void FixedUpdate()
    {
        rb.linearVelocity = mobileJoystick.GetMoveVector() * moveSpeed * Time.deltaTime;
    }
}

08-11 自定义摇杆

  • 制作摇杆轮廓

  • 创建MobileJoystic脚本

  • 允许玩家在自定义的区域内点击

  • 开始时隐藏摇杆,点击区域是显示摇杆

  • 移动旋钮

  • 抬起后,隐藏摇杆

  • 外部可以访问移动的向量

Event Trigger

PointerDown - 当指针按下(点击或触摸开始)时调用。

注意:不能直接脱脚本,需要拖拽挂载到对象的脚本

自定义脚本

using UnityEngine;

namespace DY
{
    /// <summary>
    /// 1. 允许玩家在自定义的区域内点击
    /// 2. 开始时隐藏,点击后显示
    /// 3. 控制旋钮
    /// 4. 返回一个方向,以控制玩家
    /// </summary>
    public class MobileJoystick : MonoBehaviour
    {
        [SerializeField] RectTransform joystickOutline;
        [SerializeField] RectTransform joysticknob;

        [Header("设置")]
        
        [SerializeField] float moveFactor;

        private float canvasScaleX;
        private Vector3 clickPos;
        private bool canControl;
        private Vector3 move;

        private void Start()
        {
            Hide();
            canvasScaleX = GetComponentInParent<Canvas>().GetComponent<RectTransform>().localScale.x;
        }

        private void Update()
        {
            if (canControl)
                ControlJoyStick();
        }

        private void ControlJoyStick()
        {
            Vector3 dir = Input.mousePosition - clickPos;
            float moveMagnitute = dir.magnitude * moveFactor * canvasScaleX;
            float realWidth = joystickOutline.rect.width / 2 * canvasScaleX;
            moveMagnitute = Mathf.Min(moveMagnitute, realWidth);
            move = dir.normalized * moveMagnitute;
            joysticknob.position = clickPos + move;

            if(Input.GetMouseButtonUp(0))
                Hide();
        }

        private void Show()
        {
            joystickOutline.gameObject.SetActive(true);
            canControl = true;
        }

        private void Hide()
        {
            joystickOutline.gameObject.SetActive(false);
            canControl = false;
        }

        /// <summary>
        /// 在自定义的区域内点击,触发的回调函数
        /// </summary>
        public void ClickedOnJoystickZoneCallback()
        {
            clickPos = Input.mousePosition;
            joystickOutline.position = clickPos;
            Show();
        }

        public Vector3 GetMoveDir()
        {
            return move / canvasScaleX;
        }

    }
}

12-14 设置摄像机

  • 设置背景颜色

  • 创建CameraController

15-21 竞技场

  • 使用tileMap添加墙壁和碰撞体

  • 处理人物与墙壁排序

  • 设置内置的精灵排序

  • 添加装饰物

内置渲染排序

Unity6中的内置渲染排序放在了Seting 文件夹中

22 几种2D碰撞体实验

每种形状生成2000个物体,圆形最佳

23-24 重构打包

  • 将玩家的渲染与逻辑分离

  • 将面板中的所有对象进行分层

打包中的知识点

打包中遇到的问题

1. 输入系统问题

PlayerSettings->Active Input Handling is set to Both, this is unsupported on Android and might cause issues with input and application performance. Please choose only one active input handling. Ignore and continue? (This dialog won't appear again in this Editor session if you'll choose Yes)

解决方案:

在Unity中,Player Settings中的Active Input Handling选项决定了项目将使用哪种输入系统来处理用户输入。这个设置对于不同平台可能有不同的最佳实践。对于Android平台,同时启用旧的输入系统(Input Manager)和新的输入系统(Input System Package)是不被支持的,并且可能会导致输入问题或影响应用性能。

  1. 选择一个输入系统:你需要决定是使用旧的Input Manager还是新的Input System。这取决于你的项目需求以及你是否已经在项目中使用了新输入系统的功能。

    • 如果你的项目还没有使用新的输入系统,或者你不想切换到新的输入系统,可以选择只保留Input Manager

    • 如果你已经开始使用新的输入系统,或者计划将来迁移到它,那么你应该选择只保留Input System (Package)

  2. 修改Player Settings

    • 打开Edit -> Project Settings -> Player

    • Other Settings部分找到Active Input Handling

    • 将其设置为Input Manager OnlyInput System (Package) Only,根据你上面的选择。

  3. 测试更改:在做出更改后,构建并测试你的游戏,以确保所选的输入系统能够正常工作,并且没有出现预期之外的行为。

  4. 继续构建:如果你选择了忽略警告并继续,那么在当前编辑器会话期间,你将不会再看到这个警告。然而,建议先解决问题再进行构建,以避免潜在的问题。

自定义创建不同的层级