222 lines
6.7 KiB
C#
222 lines
6.7 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
|
||
/// <summary>
|
||
/// 根据数据动态加载设备
|
||
/// </summary>
|
||
public class DeviceManager : MonoBehaviour
|
||
{
|
||
[HideInInspector]
|
||
public static DeviceManager instance;
|
||
|
||
|
||
private void Awake()
|
||
{
|
||
instance = this;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据json加载设备
|
||
/// </summary>
|
||
/// <param name="info"></param>
|
||
public void LoadDeviceByJson(DeviceData info)
|
||
{
|
||
ClearDevices();
|
||
GameObject device;
|
||
foreach (var item in info.devices)
|
||
{
|
||
Debug.Log(item.deviceSubType);
|
||
device=GameObject.Instantiate(Resources.Load<GameObject>("Prefab/Devices/" + item.deviceSubType));
|
||
device.name=item.deviceSubType;
|
||
device.transform.parent= transform;
|
||
device.transform.position = new Vector3(float.Parse(item.deviceX), float.Parse(item.deviceY), float.Parse(item.deviceZ));
|
||
device.transform.eulerAngles = new Vector3(device.transform.eulerAngles.x, float.Parse(item.deviceAngle), device.transform.eulerAngles.z);
|
||
device.GetComponent<ModelItem>().code = item.deviceSeq;
|
||
device.GetComponent<ModelItem>().floorIndex = int.Parse(item.floor);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清空本地设备
|
||
/// </summary>
|
||
public void ClearDevices()
|
||
{
|
||
foreach (Transform item in transform)
|
||
{
|
||
item.gameObject.SetActive(false);
|
||
Destroy(item.gameObject);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将子物体中的设备数据记录成json(StreamingAssets/Json/DeviceInfo.json)
|
||
/// </summary>
|
||
public static void RecordDataToJson(GameObject devicelist)
|
||
{
|
||
Debug.Log("开始记录...");
|
||
DeviceData devicelistData = new DeviceData();
|
||
devicelistData.devices = new DeviceInfo[devicelist.GetComponentsInChildren<ModelItem>(true).Length];
|
||
int index = 0;
|
||
foreach (ModelItem item in devicelist.GetComponentsInChildren<ModelItem>(true))
|
||
{
|
||
DeviceInfo _deviceinfo = new DeviceInfo();
|
||
_deviceinfo.hallCode = "";
|
||
_deviceinfo.deviceName = item.name;
|
||
_deviceinfo.deviceType = GetDeviceType(item.deviceType);
|
||
_deviceinfo.floor = item.floorIndex.ToString();
|
||
_deviceinfo.deviceSubType = GetPrefabAssetPath(item.gameObject);
|
||
_deviceinfo.deviceX = item.transform.position.x.ToString();
|
||
_deviceinfo.deviceY = item.transform.position.y.ToString();
|
||
_deviceinfo.deviceZ = item.transform.position.z.ToString();
|
||
_deviceinfo.deviceAngle = item.transform.eulerAngles.y.ToString();
|
||
_deviceinfo.deviceSeq = "";
|
||
|
||
devicelistData.devices[index]= _deviceinfo;
|
||
index++;
|
||
}
|
||
string infojson =JsonUtility.ToJson(devicelistData);
|
||
infojson=infojson.Replace("{\"devices\":", "");
|
||
infojson= infojson.Remove(infojson.Length-1);
|
||
Debug.Log(infojson);
|
||
CreateFile(Application.streamingAssetsPath+"/Json/DeviceInfo.json",infojson);
|
||
Debug.Log("保存成功!"+ Application.streamingAssetsPath + "/Json/DeviceInfo.json");
|
||
}
|
||
/// <summary>
|
||
/// 获取设备类型编号
|
||
/// </summary>
|
||
private static string GetDeviceType(DeviceType type)
|
||
{
|
||
string typecode = "";
|
||
switch (type)
|
||
{
|
||
case DeviceType.JiaoFeiJi:
|
||
typecode = "01";
|
||
break;
|
||
case DeviceType.FaPiaoDaYinJi:
|
||
typecode = "03";
|
||
break;
|
||
case DeviceType.YeWuJi:
|
||
typecode = "02";
|
||
break;
|
||
case DeviceType.SheXiangTou:
|
||
typecode = "04";
|
||
break;
|
||
case DeviceType.YuShouLiZhongDuan:
|
||
typecode = "06";
|
||
break;
|
||
case DeviceType.WangShangGuoWangYunZhongDuan:
|
||
typecode = "17";
|
||
break;
|
||
|
||
}
|
||
return typecode;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取预制体资源路径。
|
||
/// </summary>
|
||
/// <param name="gameObject"></param>
|
||
/// <returns></returns>
|
||
public static string GetPrefabAssetPath(GameObject gameObject)
|
||
{
|
||
#if UNITY_EDITOR
|
||
// Project中的Prefab是Asset不是Instance
|
||
if (UnityEditor.PrefabUtility.IsPartOfPrefabAsset(gameObject))
|
||
{
|
||
DirectoryInfo path = new DirectoryInfo(UnityEditor.AssetDatabase.GetAssetPath(gameObject));
|
||
// 预制体资源就是自身
|
||
return path.Name.Replace(".prefab", "");
|
||
}
|
||
|
||
// Scene中的Prefab Instance是Instance不是Asset
|
||
if (UnityEditor.PrefabUtility.IsPartOfPrefabInstance(gameObject))
|
||
{
|
||
// 获取预制体资源
|
||
var prefabAsset = UnityEditor.PrefabUtility.GetCorrespondingObjectFromOriginalSource(gameObject);
|
||
DirectoryInfo path = new DirectoryInfo(UnityEditor.AssetDatabase.GetAssetPath(prefabAsset));
|
||
return path.Name.Replace(".prefab", "");
|
||
}
|
||
|
||
#endif
|
||
|
||
// 不是预制体
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Creates the file.创建文本的方法
|
||
/// </summary>
|
||
/// <param name="_filePath">File path. 文件的路径</param>
|
||
/// <param name="_data">Data. 文件的内容</param>
|
||
public static void CreateFile(string _filePath, string _data)
|
||
{
|
||
StreamWriter sw;
|
||
DirectoryInfo fileinfo=new DirectoryInfo(_filePath);
|
||
if (!Directory.Exists(fileinfo.Parent.FullName))
|
||
{
|
||
Directory.CreateDirectory(fileinfo.Parent.FullName);
|
||
}
|
||
FileInfo fi = new FileInfo(_filePath);
|
||
//创建并覆盖文件
|
||
sw = fi.CreateText();
|
||
sw.WriteLine(_data);//以行的形式写入
|
||
//sw .Write (_data);//首位衔接的方式写入
|
||
sw.Close();//关闭流
|
||
sw.Dispose();//销毁流
|
||
}
|
||
|
||
}
|
||
[Serializable]
|
||
public struct DeviceData {
|
||
|
||
public DeviceInfo[] devices;
|
||
}
|
||
|
||
[Serializable]
|
||
public struct DeviceInfo {
|
||
/// <summary>
|
||
/// 营业厅代码
|
||
/// </summary>
|
||
public string hallCode;
|
||
/// <summary>
|
||
/// 设备名称
|
||
/// </summary>
|
||
public string deviceName;
|
||
/// <summary>
|
||
/// 设备类型
|
||
/// </summary>
|
||
public string deviceType;
|
||
/// <summary>
|
||
/// 设备小类
|
||
/// </summary>
|
||
public string deviceSubType;
|
||
/// <summary>
|
||
/// 楼层索引
|
||
/// </summary>
|
||
public string floor;
|
||
/// <summary>
|
||
/// 设备坐标X
|
||
/// </summary>
|
||
public string deviceX;
|
||
/// <summary>
|
||
/// 设备坐标Y
|
||
/// </summary>
|
||
public string deviceY;
|
||
/// <summary>
|
||
/// 设备坐标Z
|
||
/// </summary>
|
||
public string deviceZ;
|
||
/// <summary>
|
||
/// 设备角度
|
||
/// </summary>
|
||
public string deviceAngle;
|
||
/// <summary>
|
||
/// 设备序号
|
||
/// </summary>
|
||
public string deviceSeq;
|
||
}
|