Unity之创建第一个2D游戏项目

news/2025/2/4 1:03:27/

一 Unity环境配置

1.1 Untity资源官网下载:https://unity.cn/releases

7809e26a5c1f45e49d0a972fde264483.png

1.2 Unity Hub集成环境,包含工具和项目的管理

ee8bd7d92b3443aba065134e51b97fcc.png

1.3 Unity Editor编辑器

a5afe3362d9d48d1be137de76d1cedaa.png1.4 Visual Studio 2022脚本编辑器

d8ad8983dc964e4cac255e34973cca83.png

1.5 AndroidSKD,JDK,NDK工具,用于android环境的运行

7043b3c77f664511a052ac60f28c06d6.png

二 创建Unity项目

2.1 新建2D模板项目211d66aca7eb4c96850de94c176e3c8a.png

d74e22e6c2d6428492cc0af1149f9e40.png

2.2 新建2D物体

146964934d02462c9e3ccdadb34d4c8e.png

2.3 新建C#脚本文件 

595d8433b82543e0bdef2e9602ea282a.png

2.4 脚本文件拖拽到物理区域,关联物体 

ca6d8b0a44ff4953a9b4a27bd77b0480.png

2.5 点击脚本打开 Visual Studio 进行编辑

a789d84939034ac4a4dc7736b5ee1f9e.png

2.6 输入Debug.Log(gameObject.name);获取物体的名字,点击运行 

f55a21616dec4cd5b92fc8347c3cd3bb.png

2.7 调试 ,脚本文件保存后,可以看到UnityEditor里面的脚本文件会同步变化

cf9c057fc4424d07bec360f46632273f.png

2.9 点击顶部运行按钮就可以在控制台看到日志输出信息,可以看到打印出了物理对象的名字和标签

4b19bc14f8d6475cae98e3fef977c9a4.png

三 运行问题

3.1 第一次运行可能会出现错误,显示Unity脚本显示“杂项文件”,并且无语法提示的问题

73a154711a974aa48ab4ff15e690e55d.png

3.2  解决方法:点击 编辑(Edit)>首选项(Preferences)打开首选项窗口 

e2c639fdb0264ead81f6b134c7aec7ee.png

3.3 在首选项窗口中,选择 外部工具(External Tools)选项卡,将 外部脚本编辑器(External Script Editor)的设置改为 Visual Studio 2019等编辑器 b5372929d89d4cb98ef0deee76e289d0.png

3.4 可以看到语法能够正常显示了 

808c68efbbad43668fb3b1d4a373aea0.png

四,物体组件认识

4.1  一个物理有很多组件,点击物理,默认组件信息就会出来

fc756b5b0e644826a4689bd6f369bc29.png

4.2 如下可以给物理新加组件信息,比如给物体新加声音组件

099b6ceb991b4c6196f34f8763afcc6a.png

4.3 脚本关联物体后,也也属于物体的一个组件 ,可以在脚本中获取物体的其它组件和控制物体的组件

36238f326f914a3e9e040eea9a29feb6.png

4.4  物体下面还可以创建多个物体,我们创建一个胶囊子物体,那胶囊就属于子组件

012ea1cff2ec4240a7483fe37a584bba.png

4.5 脚本获取基础组件和子组件,父组件。如下获取物体和组件实例:

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;public class main : MonoBehaviour
{public GameObject Capsule;//胶囊组件public GameObject Prefab;//预设体// Start is called before the first frame updatevoid Start(){//拿到当前脚本所挂载的游戏物体//GameObject go = this.gameObject;//名称UnityEngine.Debug.Log(gameObject.name);//tagUnityEngine.Debug.Log(gameObject.tag);//layerUnityEngine.Debug.Log(gameObject.layer);//胶囊的名称UnityEngine.Debug.Log(Capsule.name);//胶囊当前真正的激活状态UnityEngine.Debug.Log(Capsule.activeInHierarchy);//胶囊当前自身激活状态UnityEngine.Debug.Log(Capsule.activeSelf);//获取Transform组件//Transform trans = this.transform;UnityEngine.Debug.Log(transform.position);//获取其他组件BoxCollider bc = GetComponent<BoxCollider>();//获取当前物体的子物体身上的某个组件GetComponentInChildren<CapsuleCollider>(bc);//获取当前物体的父物体身上的某个组件GetComponentInParent<BoxCollider>();//添加一个组件Capsule.AddComponent<AudioSource();//通过游戏物体的名称来获取游戏物体//GameObject test = GameObject.Find("Test");//通过游戏标签来获取游戏物体GameObject test = GameObject.FindWithTag("Enemy");test.SetActive(false);UnityEngine.Debug.Log(test.name);//通过预设体来实例化一个游戏物体GameObject go = Instantiate(Prefab, Vector3.zero, Quaternion.identity);//销毁Destroy(go);}// Update is called once per framevoid Update(){}
}

五 鼠标和触摸事件

5.1 鼠标事件

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Windows;
using Input = UnityEngine.Input;public class main : MonoBehaviour
{void Start(){}// Update is called once per framevoid Update(){//鼠标的点击//按下鼠标 0左键 1右键 2滚轮if (Input.GetMouseButtonDown(0)){UnityEngine.Debug.Log("按下了鼠标左键");}//持续按下鼠标if (Input.GetMouseButton(0)) {UnityEngine.Debug.Log("持续按下鼠标左键");}//抬起鼠标if (Input.GetMouseButtonUp(0)) {UnityEngine.Debug.Log("抬起了鼠标左键");//按下键盘按键}if (Input.GetKeyDown(KeyCode.A)) {UnityEngine.Debug.Log("按下了A");}//持续按下按键if (Input.GetKey(KeyCode.A)) {UnityEngine.Debug.Log("持续按下A");}//抬起键盘按键if (Input.GetKeyUp("a")){UnityEngine.Debug.Log("松开了A");}}
}

5.2 保存运行后可以看到控制台有对应的日志输出

0033d5d5031041c990b03baf295cc152.png

5.3 手机单点,多点触控、

using UnityEngine;
using Input = UnityEngine.Input;public class main : MonoBehaviour
{void Start(){//开启多点触控Input.multiTouchEnabled = true;}// Update is called once per framevoid Update(){//判断单点触摸if (Input.touchCount == 1){//触摸对象Touch touch = Input.touches[0];//触摸位置UnityEngine.Debug.Log(touch.position);//触摸阶段switch (touch.phase){case UnityEngine.TouchPhase.Began:break;case UnityEngine.TouchPhase.Moved:break;case UnityEngine.TouchPhase.Stationary:break;case UnityEngine.TouchPhase.Ended:break;case UnityEngine.TouchPhase.Canceled:break;}}//判断单点触摸if (Input.touchCount == 2){//触摸对象1Touch touch0 = Input.touches[0];//触摸对象1Touch touch1 = Input.touches[1];}}
}

5.4 物体向量移动,添加物体控制组件

a21b866612564aa0b962db8a08c07144.png

编写向量移动脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlarerControll : MonoBehaviour
{public CharacterController characterController;// Start is called before the first frame updatevoid Start(){characterController = GetComponent<CharacterController>();}// Update is called once per framevoid Update(){//水平轴float horizontal = Input.GetAxis("Horizontal");//垂直轴float vertical = Input.GetAxis("Vertical");//创建成一个方向向量Vector2 dir = new Vector2(horizontal,vertical);Debug.DrawRay(transform.position, dir, Color.red);characterController.SimpleMove(dir);}
}

六 鼠标控制物体移动

6.1 2D用transform属性控制移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlarerControll : MonoBehaviour
{public CharacterController characterController;// Start is called before the first frame updatevoid Start(){characterController = GetComponent<CharacterController>();}// Update is called once per framevoid Update(){if (Input.GetMouseButton(0)){//目前的鼠标二维坐标转为三维坐标Vector2 curMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);//目前的鼠标三维坐标转为世界坐标curMousePos = Camera.main.ScreenToWorldPoint(curMousePos);transform.position = curMousePos ;}}
}

6.2 在携程里面控制物体移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlarerControll : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){StartCoroutine(OnMouseDown());//在Start方法中调用StartCoroutine(要调用的协程方法)}// Update is called once per framevoid Update(){}//协程IEnumerator OnMouseDown(){//1. 得到物体的屏幕坐标Vector3 cubeScreenPos = Camera.main.WorldToScreenPoint(transform.position);//2. 计算偏移量//鼠标的三维坐标Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeScreenPos.z);//鼠标三维坐标转为世界坐标mousePos = Camera.main.ScreenToWorldPoint(mousePos);Vector3 offset = transform.position - mousePos;//3. 物体随着鼠标移动while (Input.GetMouseButton(0)){//目前的鼠标二维坐标转为三维坐标Vector3 curMousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeScreenPos.z);//目前的鼠标三维坐标转为世界坐标curMousePos = Camera.main.ScreenToWorldPoint(curMousePos);//物体世界位置transform.position = curMousePos + offset;yield return new WaitForFixedUpdate(); //这个很重要,循环执行}}
}

6.3 用Translate滑动鼠标移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlarerControll : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// 定义了一个名为sizespeed的公共(public)浮点型(float)变量,初始值为1public float sizespeed = 1;// 定义了一个名为mouseSpeed的公共浮点型变量,初始值为10public float mouseSpeed = 10;  // 定义了一个名为lastMousePosition的私有(private)Vector3类型变量private Vector3 lastMousePosition;    // Update is called once per framevoid Update(){// 获取鼠标滚轮的输入值,并将其赋值给名为mouse的局部(local)浮点型变量float mouse = -Input.GetAxis("Mouse ScrollWheel");   // 鼠标中键按住拖动if (Input.GetMouseButton(0)){   // 获取当前鼠标位置和上一次鼠标位置之间的差值,并将其赋值给名为deltaMousePosition的局部Vector3类型变量Vector3 deltaMousePosition = Input.mousePosition - lastMousePosition;// 将摄像机的位置向左右和上下移动,移动的距离由鼠标的移动距离和鼠标速度决定transform.Translate(deltaMousePosition.x * mouseSpeed * Time.deltaTime, deltaMousePosition.y * mouseSpeed * Time.deltaTime, 0);    }// 将摄像机的位置向上或向下移动,移动的距离由鼠标滚轮的输入值和大小速度决定transform.Translate(new Vector3(0, mouse * sizespeed, 0) * Time.deltaTime, Space.World);// 将鼠标当前位置赋值给lastMousePosition变量,以便下一帧计算鼠标位置差值lastMousePosition = Input.mousePosition;    }
}

七 向量的认识

7.1 向量在游戏角色世界是非常重要的一个概念,上面大部分物体的移动都是通过向量Vector3 

7.2 向量指一个同时具有大小和方向的量. 它通常画为一个带箭头的线段(如下图).线段的长度可以表示向量的大小,而向量的方向也就是箭头所指的方向.物理学中的位移、速度、力等都是矢量

7.3 只要向量的大小和方向相同, 即视为相等的向量, 如下图所示都是相同的向量.

7.4 向量的加法可以用几种三种法则来解释, 比如下面的三角形法则

7.5 向量的减法也有类似运算法则, 三角形法则和平行四边形, 记得箭头总是由减数指向被减数:

7.6 向量 b 与一个标量(实数)相乘还是一个向量, 观察下面的当标量改变时候, 向量 a 的变化:


http://www.ppmy.cn/news/1094718.html

相关文章

宠物行业如何进行软文营销

如今&#xff0c;宠物已经成为了人们生活中不可或缺的一部分&#xff0c;大众对于萌宠的喜爱与日俱增&#xff0c;随着“萌宠经济”升温&#xff0c;越来越多的商机开始出现&#xff0c;伴随着宠物市场竞争的日益激烈&#xff0c;宠物行业的营销光靠硬广告很难吸引受众&#xf…

[刷题记录]牛客面试笔刷TOP101

牛客笔试算法必刷TOP101系列,每日更新中~ 1.合并有序链表2023.9.3 合并两个排序的链表_牛客题霸_牛客网 (nowcoder.com) 题意大致为: 将两个链表中的元素按照从小到大的顺序合并成为一个链表. 所给予的条件: 给出的所要合并的链表都是从小到大顺序排列的. 思路: 创建一…

【CSDN技术】Markdown编辑器如何使用-csdn博客编写入门

Markdown编辑器如何使用-csdn博客编写入门 欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants 创建一个自…

若依集成mybatisplus【亲测有效】

一 修改pom文件 最外层的pom.xml增加如下配置 hutool工具包和lombok工具包如果不需要的话可以自行删除并修改后面的类里面的代码 <!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</…

Vue的props配置项

简介&#xff1a;Vue中的组件模板是可以复用的&#xff0c;但是模板中的数据是可以改变的。props配置项用于解决组件迁移复用时接受和保存传递给组件的数据的问题。 1.如何给组件传递数据&#xff1f; 答&#xff1a;按照key:value的形式进行传递。 2.如何保存传递给组件的数…

xxl-job-admin 核心类解析 XxlJobAdminConfig

com.xxl.job.admin.core.conf.XxlJobAdminConfig xxl-job-admin应用启动时springboot初始化的核心配置类 主要封装了一些属性&#xff1a; xxl.job.i18n > i18n: 目前仅支持: zh_CN zh_TC en 三种类型 xxl.job.accessToken > accessToken: 用于接口验证使用 spring.mai…

为什么5G 要分离 CU 和DU?(4G分离RRU 和BBU)

在 Blog 一文中&#xff0c;5G--BBU RRU 如何演化到 CU DU&#xff1f;_5g rru_qq_38480311的博客-CSDN博客 解释了4G的RRU BBU 以及 5G CU DU AAU&#xff0c;主要是讲了它们分别是什么。但是没有讲清楚 为什么&#xff0c;此篇主要回答why。 4G 为什么分离基站为 RRU 和 BBU…

python爬取网站数据笔记分享

编码问题 因为涉及到中文&#xff0c;所以必然地涉及到了编码的问题&#xff0c;借这个机会算搞清楚。 问题要从文字的编码讲起。原本的英文编码只有0~255&#xff0c;刚好是8位1个字节。为了表示各种不同的语言&#xff0c;自然要进行扩充。中文的话有GB系列。可能还听说过U…