您的位置:首页 > 教育 > 锐评 > 牡丹江最新疫情行动轨迹_和县网站建设_广东新闻今日大件事_百度知道客服电话人工服务

牡丹江最新疫情行动轨迹_和县网站建设_广东新闻今日大件事_百度知道客服电话人工服务

2024/10/6 0:31:54 来源:https://blog.csdn.net/zaizai1007/article/details/142266376  浏览:    关键词:牡丹江最新疫情行动轨迹_和县网站建设_广东新闻今日大件事_百度知道客服电话人工服务
牡丹江最新疫情行动轨迹_和县网站建设_广东新闻今日大件事_百度知道客服电话人工服务

 管理类:

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());}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com