当前位置:网站首页>同事看了我的代码惊呼:居然是这么在Unity中用单例的
同事看了我的代码惊呼:居然是这么在Unity中用单例的
2022-07-25 10:29:00 【InfoQ】
- CSDN主页
- GitHub开源地址
- Unity3D插件分享
- 简书地址
- 我的个人博客
- QQ群:1040082875
一、前言
二、单例模式介绍

三、实现单例模式的DataManager
3-1、定义单例对象
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DataManager : MonoBehaviour
{
public static DataManager Instance;
private void Awake()
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
}

3-2、单例对象去读取数据保存下来
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class DataManager : MonoBehaviour
{
public static DataManager Instance;
string m_JsonContent;//临时文档数据
RootData m_JsonData;//临时接收JSON解析数据
//数据保存到这个List里面,其他脚本就可以调用到了
public List<StationAllInfo> m_StationsAllInfo = new List<StationAllInfo>();
private void Awake()
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
private void Start()
{
ReadJSONData();
}
void ReadJSONData()
{
string pathstations = Application.streamingAssetsPath + "/地铁站.json";
using (StreamReader SR = new StreamReader(pathstations))
{
m_JsonContent = SR.ReadToEnd();
Debug.Log(m_JsonContent);
SR.Close();
SR.Dispose();
//保存JSON数据
m_JsonData = JsonUtility.FromJson<RootData>(m_JsonContent);
}
for (int i = 0; i < m_JsonData.GisJosnDatas.Count; i++)
{
if (m_JsonData.GisJosnDatas[i].properties.LINE_ID != "")//剔除无用的信息,保存有用的信息
{
StationAllInfo item = new StationAllInfo();
item.X = m_JsonData.GisJosnDatas[i].properties.X;
item.Y = m_JsonData.GisJosnDatas[i].properties.Y;
item.STACODE = m_JsonData.GisJosnDatas[i].properties.STACODE;
item.S_NAME = m_JsonData.GisJosnDatas[i].properties.S_NAME;
item.LINE_ID = m_JsonData.GisJosnDatas[i].properties.LINE_ID;
//05 换成 5 (比如05号线 换成5号线)
if (m_JsonData.GisJosnDatas[i].properties.LINE_ID.Substring(0, 1) == "0")
{
item.LINE_NAME = m_JsonData.GisJosnDatas[i].properties.LINE_ID.Substring(1, 1) + "号线";
}
else
{
switch (m_JsonData.GisJosnDatas[i].properties.LINE_ID)//对字母缩写的站名进行处理
{
case "fs":
item.LINE_NAME = "房山线";
break;
case "bt":
item.LINE_NAME = "八通线";
break;
case "yz":
item.LINE_NAME = "亦庄线";
break;
case "cp":
item.LINE_NAME = "昌平线";
break;
case "jc":
item.LINE_NAME = "机场线";
break;
default:
item.LINE_NAME = m_JsonData.GisJosnDatas[i].properties.LINE_ID + "号线";
break;
}
}
item.STYPE = m_JsonData.GisJosnDatas[i].properties.STYPE;
m_StationsAllInfo.Add(item);
}
}
}
}
3-3、在其他脚本中使用数据
using UnityEngine;
public class UseData : MonoBehaviour
{
private void Update()
{
//点击键盘W 显示数据
if (Input.GetKeyDown(KeyCode.W))
{
ShowData();
}
}
private void ShowData()
{
for (int i = 0; i < DataManager.Instance.m_StationsAllInfo.Count; i++)
{
Debug.Log(DataManager.Instance.m_StationsAllInfo[i].X + " "
+DataManager.Instance.m_StationsAllInfo[i].Y + " "
+ DataManager.Instance.m_StationsAllInfo[i].STACODE + " "
+ DataManager.Instance.m_StationsAllInfo[i].S_NAME + " "
+ DataManager.Instance.m_StationsAllInfo[i].LINE_ID + " "
+ DataManager.Instance.m_StationsAllInfo[i].LINE_NAME + " "
+ DataManager.Instance.m_StationsAllInfo[i].STYPE + " ");
}
}
}


3-4、切换场景读取数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeScenes : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
SceneManager.LoadScene(1);
}
}
}



四、后言
边栏推荐
猜你喜欢
随机推荐
Flask framework -- flask caching
The most detailed MySQL index analysis (mind map is attached at the end of the article)
The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts
HCIP(11)
How to notify users of wechat applet version update?
【高并发】通过源码深度分析线程池中Worker线程的执行流程
mysql主从复制与读写分离
HCIA experiment (09)
史上最全的立创元器件封装库导入AD详细教程(一直白嫖一直爽)
大佬们,flink cdc table api , mysql to mysql,一个应用程序,可以
HCIP(11)
ESP8266 使用 DRV8833驱动板驱动N20电机
100W!
Tree dynamic programming
Learn NLP with Transformer (Chapter 6)
30000 word express Servlet
Ue4.26 source code version black screen problem of client operation when learning Wan independent server
BeautifulSoup的一些用法
Hcip experiment (01)
SQL语言(二)








