我的demo保卫萝卜中的技术要点

 管理类:

GameManager(单例),GameController(单例);

一些其他的管理类(PlayerManager,AudioSourceManager,FactoryManager)作为GameManager的成员变量存在(这样也可以保证只有一个存在,并且初始化在GameManager之后)

使用到的多种设计模式

:复盘一下我用过的设计模式-CSDN博客

:: Scoll View 结合 GridLayoutGroup 组件可以实现组件的整齐排列

RectTransform

RectTransform 和 Transfrom 的区别

Inspector,Awake,OnEnable与Start之间微妙的关系

Inspector 早于 Awake 早于 OnEnable 早于 Start

地图编辑器  编辑类

MapTool类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;#if Tool
[CustomEditor(typeof(MapMaker))]
public class MapTool : Editor
{private MapMaker mapMaker;//关卡文件列表private List<FileInfo> fileList = new List<FileInfo>();private string[] fileNameList;//当前编辑的关卡索引private int selectIndex = -1;public override void OnInspectorGUI(){base.OnInspectorGUI();if(Application.isPlaying){mapMaker = MapMaker.Instance; EditorGUILayout.BeginHorizontal();//获取操作的文件名fileNameList = GetNames(fileList);int currentIndex = EditorGUILayout.Popup(selectIndex, fileNameList);if (currentIndex != selectIndex)  //当前选择对象是否改变{selectIndex = currentIndex;//实例化地图的方法mapMaker.InitMap();//加载当前选择的Level文件mapMaker.LoadLevelFile(mapMaker.LoadLevelInfoFile(fileNameList[selectIndex]));}if (GUILayout.Button("读取关卡列表")){LoadLevelFiles();}EditorGUILayout.EndHorizontal();EditorGUILayout.BeginHorizontal();if(GUILayout.Button("回复地图编辑器默认状态")){mapMaker.RecoverTowerPoint();}if(GUILayout.Button("清除怪物路点")){mapMaker.CLearMonsterPath();}EditorGUILayout.EndHorizontal();if(GUILayout.Button("保存当前关卡数据文件")){mapMaker.SaveLevelFileByJson();}}}//加载关卡数据文件private void LoadLevelFiles(){CLearList();fileList = GetLevelFile();}//清楚文件列表private void CLearList(){fileList.Clear();selectIndex = -1;}//具体读取关卡列表的方法private  List<FileInfo> GetLevelFile(){//自动帮我们读后缀是.json的文件string[] files = Directory.GetFiles(Application.streamingAssetsPath + "/Json/Level/", "*.json");List<FileInfo> list = new List<FileInfo>();for (int i = 0; i < files.Length; i++){FileInfo file = new FileInfo(files[i]);list.Add(file);}return list;}//获取关卡文件的名字private string[] GetNames(List<FileInfo> files){List<string> names = new List<string>();foreach (FileInfo file in files){names.Add(file.Name);}//将列表转成数组return names.ToArray();}}
#endif

Mapmaker 类

using LitJson;
using System.Collections.Generic;
using System.IO;
using UnityEngine;/// <summary>
/// 地图编辑工具,游戏中作为地图加载产生工具
/// </summary>public class MapMaker : MonoBehaviour
{
#if Toolpublic bool drawLine;  //是否画线public GameObject gridGo;  //格子的预制体private static MapMaker _instance;public static MapMaker Instance { get => _instance; }
#endif//地图的有关属性private float mapWidth;  //地图宽private float mapHeight;  //地图高//格子[HideInInspector]public float gridWidth;[HideInInspector]public float gridHeight;//当前关卡索引//[HideInInspector]public int bigLevelID;//[HideInInspector]public int levelID;//全部的格子对象public GridPoint[,] gridPoints;//行列数public const int yRow = 8;public const int xCloumn = 12;//怪物路径点[HideInInspector]public List<GridPoint.GridIndex> monsterPath;//怪物路径点的具体位置[HideInInspector]public List<Vector3> monsterPathPos;//关卡的背景道路渲染private SpriteRenderer bgSR;private SpriteRenderer roadSR;//每一波次产生的怪物ID列表public List<Round.RoundInfo> roundInfoList;[HideInInspector]public Carrot carrot;private void Awake(){
#if Tool_instance = this;InitMapMaker();
#endif}//初始化地图public void InitMapMaker(){Calculatesize();gridPoints = new GridPoint[xCloumn, yRow];monsterPath = new List<GridPoint.GridIndex>();for (int x = 0; x < xCloumn; x++){for (int y = 0; y < yRow; y++){
#if ToolGameObject itemGo = Instantiate(gridGo,transform.position,transform.rotation);
#endif#if Game//问工厂要资源的方法GameObject itemGo = GameController.Instance.GetGameObjectResource("Grid");
#endifitemGo.transform.position = new Vector3(x * gridWidth - mapWidth / 2 + gridWidth / 2,y * gridHeight - mapHeight / 2 + gridHeight / 2);itemGo.transform.SetParent(transform);gridPoints[x, y] = itemGo.GetComponent<GridPoint>();gridPoints[x, y].gridIndex.xIndex = x;gridPoints[x, y].gridIndex.yIndex = y;}}bgSR = transform.Find("BG").GetComponent<SpriteRenderer>();roadSR = transform.Find("Road").GetComponent<SpriteRenderer>();}#if Game//加载地图public void LoadMap(int bigLevel, int level){bigLevelID = bigLevel;levelID = level;LoadLevelFile(LoadLevelInfoFile("Level" + bigLevelID.ToString() + "_" + levelID.ToString() + ".json"));monsterPathPos = new List<Vector3>();for (int i = 0; i < monsterPath.Count; i++){monsterPathPos.Add(gridPoints[monsterPath[i].xIndex, monsterPath[i].yIndex].transform.position);}//起始点与终止点GameObject startPointGo = GameController.Instance.GetGameObjectResource("startPoint");startPointGo.transform.position = monsterPathPos[0];startPointGo.transform.SetParent(transform);GameObject endPointGo = GameController.Instance.GetGameObjectResource("Carrot");endPointGo.transform.position = monsterPathPos[monsterPathPos.Count - 1] - new Vector3(0, 0, 1);endPointGo.transform.SetParent(transform);carrot = endPointGo.GetComponent<Carrot>();}#endif//纠正位置public Vector3 CorrectPosition(float x, float y){return new Vector3(x - mapWidth / 2 + gridWidth / 2, y - mapHeight / 2 + gridHeight / 2);}//计算地图格子宽高private void Calculatesize(){//视口坐标,左下角(0,0),右上角(1,1)Vector3 leftDown = new Vector3(0, 0);Vector3 rightUp = new Vector3(1, 1);//将视口转换为世界坐标Vector3 posOne = Camera.main.ViewportToWorldPoint(leftDown);Vector3 posTwo = Camera.main.ViewportToWorldPoint(rightUp);mapWidth = posTwo.x - posOne.x;mapHeight = posTwo.y - posOne.y;gridWidth = mapWidth / xCloumn;gridHeight = mapHeight / yRow;}#if Tool//画格子用于辅助设计private void OnDrawGizmos(){if(drawLine){Calculatesize();Gizmos.color = Color.green;//画行for(int y = 0; y <= yRow; y++){Vector3 startPos = new Vector3(-mapWidth / 2, -mapHeight / 2 + y * gridHeight);Vector3 endPos = new Vector3(mapWidth / 2, -mapHeight / 2 + y * gridHeight);Gizmos.DrawLine(startPos, endPos);}//画列for(int x = 0;x <= xCloumn;x++){Vector3 startPos = new Vector3(-mapWidth / 2 + x * gridWidth ,mapHeight / 2);Vector3 endPos = new Vector3(-mapWidth / 2 + x * gridWidth, -mapHeight / 2);Gizmos.DrawLine(startPos, endPos);}}}
#endif/// <summary>/// 有关地图编辑的方法/// </summary>//清除怪物路点public void CLearMonsterPath(){monsterPath.Clear();}//恢复地图编辑默认状态public void RecoverTowerPoint(){CLearMonsterPath();for (int x = 0; x < xCloumn; x++){for (int y = 0; y < yRow; y++){gridPoints[x, y].InitGrid();}}}//初始化地图public void InitMap(){bigLevelID = 0;levelID = 0;RecoverTowerPoint();roundInfoList.Clear();bgSR.sprite = null;roadSR.sprite = null;}#if Tool//生成LevelInfo对象用来保存文件private LevelInfo CreateLevelInfoGo(){LevelInfo levelInfo = new LevelInfo{bigLevelID = this.bigLevelID,levelID = this.levelID,};levelInfo.gridPoints = new List<GridPoint.GridState>(); ;for (int x = 0; x < xCloumn; x++){for (int y = 0; y < yRow; y++){levelInfo.gridPoints.Add(gridPoints[x, y].gridState);}}levelInfo.monsterPath = new List<GridPoint.GridIndex>();for (int i = 0; i < monsterPath.Count; i++){levelInfo.monsterPath.Add(monsterPath[i]);}levelInfo.roundInfo = new List<Round.RoundInfo>();for (int i = 0; i < roundInfoList.Count; i++){levelInfo.roundInfo.Add(roundInfoList[i]);}Debug.Log("保存成功");return levelInfo;}//保存当前关卡的数据文件public void SaveLevelFileByJson(){LevelInfo levelInfoGo = CreateLevelInfoGo();string filePath = Application.streamingAssetsPath + "/Json/Level/" + "Level"+ bigLevelID.ToString() + "_" + levelID.ToString() + ".json";string saveJsonStr = JsonMapper.ToJson(levelInfoGo);StreamWriter sw = new StreamWriter(filePath);sw.Write(saveJsonStr);sw.Close();}#endif//读取关卡文件解析json转化为LevelInfo对象public LevelInfo LoadLevelInfoFile(string fileName){LevelInfo levelInfo = new LevelInfo();string filePath = Application.streamingAssetsPath + "/Json/Level/" + fileName;if (File.Exists(filePath)){StreamReader sr = new StreamReader(filePath);string jsonStr = sr.ReadToEnd();sr.Close();levelInfo = JsonMapper.ToObject<LevelInfo>(jsonStr);return levelInfo;}Debug.Log("文件加载失败,加载路径是:" + filePath);return null;}//public void LoadLevelFile(LevelInfo levelInfo){bigLevelID = levelInfo.bigLevelID;levelID = levelInfo.levelID;for (int x = 0; x < xCloumn; x++){for (int y = 0; y < yRow; y++){gridPoints[x, y].gridState = levelInfo.gridPoints[y + x * yRow];//更新格子的状态gridPoints[x, y].UpdateGrid();}}monsterPath.Clear();for (int x = 0; x < levelInfo.monsterPath.Count; x++){monsterPath.Add(levelInfo.monsterPath[x]);}roundInfoList = new List<Round.RoundInfo>();for (int i = 0; i < levelInfo.roundInfo.Count; i++){roundInfoList.Add(levelInfo.roundInfo[i]);}bgSR.sprite = Resources.Load<Sprite>("Pictures/NormalMordel/Game/"+ bigLevelID.ToString() + "/" + "BG" + (levelID / 3).ToString());roadSR.sprite = Resources.Load<Sprite>("Pictures/NormalMordel/Game/"+ bigLevelID.ToString() + "/" + "Road" + levelID.ToString());}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/54350.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

高德地图JS API AMap.MouseTool绘制

fang &#x1f916; 作者简介&#xff1a;水煮白菜王 &#xff0c;一位资深前端劝退师 &#x1f47b; &#x1f440; 文章专栏&#xff1a; 高德AMap专栏 &#xff0c;记录一下平时在博客写作中&#xff0c;总结出的一些开发技巧✍。 感谢支持&#x1f495;&#x1f495;&#…

本地搭建我的世界服务器(JAVA)简单记录

网上参考教程挺多的&#xff0c;踩了不少坑&#xff0c;简单记录一下&#xff0c;我做的是一个私人服务器&#xff0c;就是和朋友3、4个人玩。 笨蛋 MC 开服教程 先放一个比较系统和完整的教程&#xff0c;萌新可用&#xff0c;这个教程很详细&#xff0c;我只是记录一下自己的…

【QT】定时器使用

文章目录 关于 Qt 定时器使用的注意细节总结实例-检查工具使用周期时间是否合理UI设计头文件 remind.h源文件 remind.cpp实现效果 关于 Qt 定时器使用的注意细节总结 一、创建与初始化 使用 QTimer 类来创建定时器。可以在构造函数中指定父对象&#xff0c;确保定时器在正确的…

自动化测试常用函数

目录 一、元素的定位 1、cssSelector 2、xpath &#xff08;1&#xff09;xpath 语法 1、获取HTML页面所有的节点 2、获取HTML页面指定的节点 3、获取一个节点中的直接子节点 4、获取一个节点的父节点 5、实现节点属性的匹配 6、使用指定索引的方式获取对应的节点内容…

【鸿蒙】HarmonyOS NEXT星河入门到实战8-自定义组件-组件通信

目录 1、模块化语法 1.1 模块化基本认知 1.2 默认导出和导入 1.2.1 在ets下新建tools目录 1.2.2 在tools下新建moduls.ets文件 1.2.3 index.ets 1.3 按需导出和导入 1.4 全部导入 2、自定义组件 -基础 2.1 自定义组件 - 基本使用 2.2 自定义组件 -通用样式 2.2.1 et…

大数据Flink(一百二十二):阿里云Flink MySQL连接器介绍

文章目录 阿里云Flink MySQL连接器介绍 一、特色功能 二、​​​​​​​语法结构 三、​​​​​​​​​​​​​​WITH参数 阿里云Flink MySQL连接器介绍 阿里云提供了MySQL连接器&#xff0c;其作为源表时&#xff0c;扮演的就是flink cdc的角色。 一、特色功能 MySQ…

neo4j导入csv数据

neo4j数据可视化实践 手动输入数据 - 官方democsv数据导入准备数据数据处理导入步骤① 导入疾病表格② 导入药物表格③导入疾病-药物关系表格 爬虫的csv文件 手动输入数据 - 官方demo 点击之后&#xff0c;按照左边10张图中的代码&#xff0c;复制粘贴熟悉语法 效果如下 csv数据…

基于JAVA+SpringBoot+Vue的学生干部管理系统

基于JAVASpringBootVue的学生干部管理系统 前言 ✌全网粉丝20W,csdn特邀作者、博客专家、CSDN[新星计划]导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末附源码下载链接&#x1f345; 哈…

【AI视频】Runway:Gen-2 运镜详解

博客主页&#xff1a; [小ᶻZ࿆] 本文专栏: AI视频 | Runway 文章目录 &#x1f4af;前言&#x1f4af;Camera Control&#xff08;运镜&#xff09;&#x1f4af;Camera Control功能测试Horizonta&#xff08;左右平移&#xff09;Vertical&#xff08;上下平移&#xff0…

UNION嵌套STRUCT的两种类型

1. STRUCT里面的总长度大于UNION中的最大长度 在UNION类型中&#xff0c;嵌套如STRUCT类型&#xff0c;其中STRUCT的类型还比UNION类型中最大的类型的长度还长的时候&#xff0c;会如何处理呢&#xff0c;看下面示例 程序源码 #include "stdafx.h"typedef unsigned…

103.WEB渗透测试-信息收集-FOFA语法(3)

免责声明&#xff1a;内容仅供学习参考&#xff0c;请合法利用知识&#xff0c;禁止进行违法犯罪活动&#xff01; 内容参考于&#xff1a; 易锦网校会员专享课 上一个内容&#xff1a;102.WEB渗透测试-信息收集-FOFA语法&#xff08;2&#xff09; FOFA使用实例 组件框架 …

DAY20信息打点-红蓝队自动化项目资产侦察武器库部署企查产权网络空间

2.自动化-网络空间-AsamF 1.去GitHub上下载项目之后使用CMD打开 2.输入命令AsamF_windows_amd64.exe -v生成配置文件 3.AsamF会在~/.config/asamf/目录下生成config.json文件 C:\Users\Acer\.config\asamf 5.根据文档输入命令去查询所需信息&#xff08;已经没有用了&#x…

LabVIEW多语言支持优化

遇到的LabVIEW多语言支持问题&#xff0c;特别是德文显示乱码以及系统区域设置导致的异常&#xff0c;可能是由编码问题或区域设置不匹配引起的。以下是一些可能的原因及解决方案&#xff1a; 问题原因&#xff1a; 编码问题&#xff1a;LabVIEW内部使用UTF-8编码&#xff0c;但…

react的事件绑定

文章目录 基本示例使用箭头函数事件对象阻止默认行为绑定事件处理函数的上下文 在 React 中&#xff0c;事件绑定主要通过 JSX 属性来实现。事件处理函数被传递给相应的事件属性&#xff0c;例如 onClick、onChange 等。这些属性会被绑定到 HTML 元素上&#xff0c;并在事件发生…

大白话解读末日期权是什么意思?末日期权与黑天鹅!

今天带你了解大白话解读末日期权是什么意思&#xff1f;末日期权与黑天鹅&#xff01;末日期权与黑天鹅事件的关系主要体现在风险和波动性管理上&#xff0c;交易者需要谨慎对待这两者的互动。 末日期权和期权黑天鹅事件之间的关系主要体现在风险管理和市场波动性上。 末日期…

【SA8155P】AIS Camera相关内容的简单介绍

高通车载相机模块(AIS,Automotive lmage System)是专门针对车载系统特性而设计的一套车载视觉架构,可用于AVM、RVC、DMS等常见车载视频应用开发。车载Camera系统的图像大部分是给自动驾驶等使用,更多考虑的是远距离传输、多摄像头图像处理等场景。 本文仅对AIS Camera相关…

2024考研数学真题解析-数二:

第一类间断点&#xff1a;可去间断点&#xff0c;跳跃间断点。 幂指函数x的取值范围是x>0。 接着分母不能为0推出x1&#xff0c;x2是间断点&#xff0c;由幂指函数x>0可知&#xff0c;x0也是间断点。 先求x0处的极限&#xff0c;这里没有必要求0左右两边的极限&#xff0…

百元头戴式耳机都有哪些?五大精品独家推荐!

在当今市场中&#xff0c;耳机已经成为我们生活中不可或缺的电子设备之一。而对于追求性价比的朋友来说&#xff0c;如何在百元价位内挑选到一款音质出色、舒适耐用的头戴式耳机&#xff0c;无疑是一大难题。百元头戴式耳机都有哪些&#xff1f;为了帮助大家在琳琅满目的产品中…

Linux之CentOS 7.9-Minimal部署Oracle 11g r2 安装实测验证(桌面模式)

前言: 发个之前的库存… Linux之CentOS 7.9-Minimal部署Oracle 11g r2 安装实测验证(桌面模式) 本次验证的是CentOS_7_Minimal-2009桌面模式来部署Oracle 11g r2,大家可根据自身环境及学习来了解。 环境:下载地址都给你们超链好了 1、Linux系统镜像包: 1.1 CentOS-7-x86_…

嵌入式AI---如何用C++实现YOLO的NMS(非极大值抑制)算法

文章目录 前言一、为什么需要NMS算法&#xff1f;二、什么是NMS算法&#xff1f;三、如何使用C编写一个NMS算法1、预测框定义2、滤除无效框 总结 前言 YOLO系列的目标检测算法在边缘部署方面展现出了强大的性能和广泛的应用潜力。大部分业务场景是利用PyTorch在服务器端完成检…