123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- using Newtonsoft.Json;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEditor.SceneManagement;
- using Application = UnityEngine.Application;
- using Seengene.XDKUnityPluginCloud;
- using Newtonsoft.Json.Linq;
- public class SeengeneXDKSystem : EditorWindow {
- static GameObject m_NeedCreatedGameObject;
- static Material m_CustomMaterial;
- static SceneAsset m_Scene;
- static Dictionary<string, EditorDownloadObject> dicDown;
- static bool ifDownloadFinish = true;
- static Action _callback;
- static long mapID = 1;
- static string companyName;
- static string companyID;
- static string programeName;
- static string programeID;
- static MapInfo mapInfo = null;
- private static SeengeneXDKSystem m_Instance;
- public static SeengeneXDKSystem Instance {
- get { return m_Instance; }
- private set { m_Instance = value; }
- }
- [MenuItem("Window/Seengene XDK")]
- static void CreatWindows() {
- m_Instance = (SeengeneXDKSystem)EditorWindow.GetWindow(typeof(SeengeneXDKSystem));
- m_Instance.minSize = new Vector2(600, 420);
- m_Instance.titleContent = new GUIContent("Seengene XDK");
- m_Instance.autoRepaintOnSceneChange = true;
- Texture titleTexture = AssetDatabase.LoadAssetAtPath<Texture>(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_TitleIcon));
- if (titleTexture != null)
- m_Instance.titleContent.image = titleTexture;
- if (!Directory.Exists(Application.streamingAssetsPath)) {
- Directory.CreateDirectory(Application.streamingAssetsPath);
- }
- string directoryScale = EditorConfigs.Path_Scale.Replace(Path.GetFileName(EditorConfigs.Path_Scale), "");
- if (!Directory.Exists(directoryScale)) {
- Directory.CreateDirectory(directoryScale);
- }
- string directoryPLY = EditorConfigs.Path_Ply.Replace(Path.GetFileName(EditorConfigs.Path_Ply), "");
- if (!Directory.Exists(directoryPLY)) {
- Directory.CreateDirectory(directoryPLY);
- }
- string directoryPrefabs = EditorConfigs.Path_PrefabPly.Replace(Path.GetFileName(EditorConfigs.Path_PrefabPly), "");
- if (!Directory.Exists(directoryPrefabs)) {
- Directory.CreateDirectory(directoryPrefabs);
- }
- AssetDatabase.Refresh();
- }
- SeengeneXDKSystem() {
- if (m_Instance == null)
- m_Instance = this;
- SetDefault();
- }
- static void SetDefault() {
- mapID = 1;
- mapInfo = null;
- companyName = "";
- companyID = "";
- programeName = "";
- programeID = "";
- }
- void OnGUI() {
- GUILayout.BeginVertical("Box");
- GUI_MapInfo();
- GUILayout.BeginHorizontal("Box");
- if (GUILayout.Button("1. Get Map Infomation")) {
- if (!IfMapFilesExist())
- DownloadMapInfo();
- }
- if (IfMapInfoValid()) {
- if (GUILayout.Button("2. Download Map Files")) {
- if (!IfMapFilesExist())
- DownLoadMap();
- }
- if (IfMapInfoValid() && ifDownloadFinish && IfMapFilesExist()) {
- GUILayout.Space(20);
- GUILayout.Label("下载进度:100 % 100", GUILayout.Width(150));
- } else {
- if (dicDown != null && dicDown.Count > 0) {
- GUILayout.Space(20);
- GUILayout.Label(string.Format("下载进度:{0} 100", Get_Progress().ToString("00 %")), GUILayout.Width(150));
- }
- }
- }
- GUILayout.EndHorizontal();
- if (IfMapInfoValid() && ifDownloadFinish && IfMapFilesExist()) {
- GUI_SceneSetUp();
- }
- GUILayout.EndVertical();
- }
- private void GUI_MapInfo() {
- GUILayout.BeginHorizontal("Box"); //开始一个水平布局
- GUILayout.Space(10);
- GUI.skin.label.fontSize = 16;
- GUI.skin.label.alignment = TextAnchor.MiddleCenter;
- GUILayout.Label("Map Config");
- GUILayout.Space(10);
- GUILayout.EndHorizontal(); //结束一个水平布局
- GUILayout.BeginHorizontal("Box");
- GUI.skin.label.fontSize = 11;
- GUI.skin.label.alignment = TextAnchor.MiddleLeft;
- GUILayout.Label("Map ID:", GUILayout.Width(120));
- if (!File.Exists(EditorConfigs.LocalMapInfoJson)) {
- mapID = EditorGUILayout.LongField(mapID, GUILayout.MaxHeight(16));
- } else {
- EditorGUILayout.LabelField(mapID.ToString(), GUILayout.MaxHeight(16));
- if (GUILayout.Button("- RESET ALL -", GUILayout.Width(130))) {
- RestoreFactory();
- }
- }
- GUILayout.EndHorizontal();
- GUILayout.Space(10);
- GUILayout.BeginHorizontal("Box");
- GUI.skin.label.fontSize = 11;
- GUILayout.Label("Company:", GUILayout.Width(120));
- EditorGUILayout.LabelField(companyName);
- GUILayout.EndHorizontal();
- GUILayout.Space(10);
- GUILayout.BeginHorizontal("Box");
- GUILayout.Label("CompanyID:", GUILayout.Width(120));
- EditorGUILayout.LabelField(companyID);
- GUILayout.EndHorizontal();
- GUILayout.Space(10);
- GUILayout.BeginHorizontal("Box");
- GUILayout.Label("Programe:", GUILayout.Width(120));
- EditorGUILayout.LabelField(programeName);
- GUILayout.EndHorizontal();
- GUILayout.Space(10);
- GUILayout.BeginHorizontal("Box");
- GUILayout.Label("ProgrameID:", GUILayout.Width(120));
- EditorGUILayout.LabelField(programeID);
- GUILayout.EndHorizontal();
- GUILayout.Space(10);
- }
- private void GUI_SceneSetUp() {
- GUILayout.Space(10);
- GUILayout.BeginHorizontal("Box");
- GUI.skin.label.fontSize = 16;
- GUI.skin.label.alignment = TextAnchor.MiddleCenter;
- //GUILayout.Space(15);
- GUILayout.Label("Scene Setup");
- GUILayout.EndHorizontal();
- GUILayout.BeginVertical("Box");
- GUI.skin.label.fontSize = 11;
- GUI.skin.label.alignment = TextAnchor.UpperLeft;
- //绘制当前正在编辑的场景
- GUILayout.Space(10);
- m_Scene = (SceneAsset)EditorGUILayout.ObjectField("Source Scene", m_Scene, typeof(SceneAsset), true);
- if (File.Exists(EditorConfigs.Path_Scene)) {
- m_Scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Scene));
- }
- //绘制对象
- GUILayout.Space(10);
- m_NeedCreatedGameObject = (GameObject)EditorGUILayout.ObjectField("Source Object", m_NeedCreatedGameObject, typeof(GameObject), true);
- if (File.Exists(EditorConfigs.Path_Ply)) {
- m_NeedCreatedGameObject = AssetDatabase.LoadAssetAtPath<GameObject>(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Ply));
- }
- // 点云自定义材质
- GUILayout.Space(10);
- m_CustomMaterial = (Material)EditorGUILayout.ObjectField("Custom Material", m_CustomMaterial, typeof(Material), true);
- if (File.Exists(EditorConfigs.Path_CustomPLYMaterial)) {
- m_CustomMaterial = AssetDatabase.LoadAssetAtPath<Material>(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_CustomPLYMaterial));
- }
- GUILayout.Space(6);
- if (m_Scene != null && m_NeedCreatedGameObject != null && m_CustomMaterial != null) {
- if (GUILayout.Button("3. Setup Scene")) {
- SceneSetupObject.ChangeCoordinate(m_NeedCreatedGameObject, m_CustomMaterial);
- SeengeneXDKSystem.Instance.ShowNotification(new GUIContent("Setup Scene Complete!"));
- }
- }
- GUILayout.Space(6);
- GUILayout.EndVertical();
- }
- private void DownloadMapInfo() {
- mapInfo = null;
- string url = string.Format("http://{0}:{1}/{2}{3}", EditorConfigs.IP, EditorConfigs.Port, EditorConfigs.GetMapInfoURL, mapID);
- Debug.LogFormat("Start to Get MapInformation: {0}", url);
- this.StartCoroutine(EnumDownloadMapInfo(delegate (string strInfo) {
- try {
- Debug.LogFormat("Map Information: {0}", strInfo);
- JObject jo = JObject.Parse(strInfo);
- //Debug.LogFormat("msg:{0}, code:{1}, data:{2}", jo["msg"], jo["code"], jo["data"]);
- if (jo["code"].ToString() != "200") {
- Debug.LogErrorFormat("Map Infomation Error, code:{0}, msg:{1}", jo["code"], jo["msg"]);
- } else if (string.IsNullOrEmpty(jo["data"].ToString()) || jo["data"].ToString() == "error") {
- Debug.LogErrorFormat("Map Infomation Error, code:{0}, msg:{1},data:{2}", jo["code"], jo["msg"], jo["data"]);
- } else {
- mapInfo = JsonConvert.DeserializeObject<MapInfo>(strInfo);
- if (mapInfo != null) {
- companyName = mapInfo.data.companyName;
- companyID = mapInfo.data.companyId;
- programeName = mapInfo.data.programName;
- programeID = mapInfo.data.programId;
- }
- }
- } catch (Exception e) {
- Debug.LogErrorFormat("Map Infomation Anylizition Error, MapInfor: {0}, error: {1}", strInfo, e);
- SetDefault();
- }
- }, url));
- }
- private void DownLoadMap() {
- Dictionary<string, string> downLoadURLDic = null;
- downLoadURLDic = new Dictionary<string, string>();
- if (!string.IsNullOrEmpty(mapInfo.data.dense_model_unity_url)) {
- downLoadURLDic.Add("PLY", mapInfo.data.dense_model_unity_url);
- }
- if (!string.IsNullOrEmpty(mapInfo.data.scaleUrl)) {
- downLoadURLDic.Add("SCALE", mapInfo.data.scaleUrl);
- }
- //if (!string.IsNullOrEmpty(mapInfo.data.map_v2_bin_url)) {
- // downLoadURLDic.Add("BIN", mapInfo.data.map_v2_bin_url);
- //}
- if (downLoadURLDic != null) {
- DownLoad(downLoadURLDic, delegate () {
- SeengeneXDKSystem.Instance.ShowNotification(new GUIContent("Download Complete!"));
- if (IfMapInfoValid() && !File.Exists(EditorConfigs.LocalMapInfoJson))
- File.WriteAllText(EditorConfigs.LocalMapInfoJson, JsonConvert.SerializeObject(mapInfo));
- //刷新
- AssetDatabase.Refresh();
- });
- }
- }
- private bool IfMapInfoValid() {
- return mapInfo != null && mapInfo.code == 200 && mapInfo.data != null;
- }
- private bool IfMapFilesExist() {
- bool ifPlyExists = File.Exists(EditorConfigs.Path_Ply);
- bool ifScaleExists = File.Exists(EditorConfigs.Path_Scale);
- return ifPlyExists && ifScaleExists;
- }
- private void RestoreFactory() {
- if (File.Exists(EditorConfigs.LocalMapInfoJson)) {
- AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.LocalMapInfoJson));
- }
- if (File.Exists(EditorConfigs.Path_Scale)) {
- AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Scale));
- }
- if (File.Exists(EditorConfigs.Path_Ply)) {
- AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Ply));
- }
- if (File.Exists(EditorConfigs.Path_PrefabPly)) {
- AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_PrefabPly));
- }
- SetDefault();
- SceneSetupObject.DeleteMapRootDemo();
- AssetDatabase.Refresh();
- }
- //请求地图数据信息
- IEnumerator EnumDownloadMapInfo(Action<string> callBack, string url) {
- WWWForm form = new WWWForm();
- UnityWebRequest webRequest = UnityWebRequest.Post(url, form);
- yield return webRequest.SendWebRequest();
- if (webRequest.isHttpError || webRequest.isNetworkError) {
- Debug.LogError(webRequest.error);
- SetDefault();
- } else {
- string mes = webRequest.downloadHandler.text;
- callBack(mes);
- }
- }
- private void DownLoad(Dictionary<string, string> downLoadURLDic, System.Action call_Back) {
- ifDownloadFinish = false;
- dicDown = new Dictionary<string, EditorDownloadObject>();
- if (downLoadURLDic == null) {
- ifDownloadFinish = true;
- }
- _callback = call_Back;
- foreach (var kvURL in downLoadURLDic) {
- string destLocalPath = string.Empty;
- switch (kvURL.Key) {
- case "PLY":
- destLocalPath = EditorConfigs.Path_Ply;
- break;
- case "SCALE":
- destLocalPath = EditorConfigs.Path_Scale;
- break;
- default:
- break;
- }
- EditorDownloadObject dlObj = new EditorDownloadObject();
- dicDown.Add(destLocalPath, dlObj);
- dlObj.DownLoad(kvURL.Value, destLocalPath, delegate {
- dicDown.Remove(destLocalPath);
- Debug.LogFormat("Download Complete: {0}, remain:{1}", destLocalPath, dicDown.Count);
- if (dicDown != null && dicDown.Count == 0)
- ifDownloadFinish = true;
- });
- }
- if (dicDown != null && dicDown.Count == 0) {
- SeengeneXDKSystem.Instance.ShowNotification(new GUIContent("Download Complete"));
- ifDownloadFinish = true;
- }
- }
- void Update() {
- if (ifDownloadFinish == false) {
- if (dicDown != null && dicDown.Count > 0) {
- string info = "";
- info = Path.GetFileName(dicDown.ToList().First().Key);
- float progress = Get_Progress();
- //Debug.LogFormat("下载中: {0}, {1}", info, progress);
- EditorUtility.DisplayProgressBar("Downloadig Map Files", info, progress);
- //if (progress <= 1) {
- // Repaint();
- //}
- }
- }
- if (dicDown != null && dicDown.Count == 0 && ifDownloadFinish) {
- dicDown = null;
- AssetDatabase.Refresh();
- EditorUtility.ClearProgressBar();
- _callback();
- }
- }
- private void OnInspectorUpdate() {
- Repaint();
- }
- public float Get_Progress() {
- float sum = 0;
- if (dicDown != null && dicDown.Count > 0) {
- foreach (var item in dicDown.Values) {
- sum += item.progress;
- }
- return sum / dicDown.Count;
- }
- return 1;
- }
- private void OnEnable() {
- CheckMapInfoAndFiles();
- }
- private void OnDisable() {
- this.StopAllCoroutines();
- if (IfMapInfoValid() && !File.Exists(EditorConfigs.LocalMapInfoJson) && File.Exists(EditorConfigs.LocalMapInfoJson))
- File.WriteAllText(EditorConfigs.LocalMapInfoJson, JsonConvert.SerializeObject(mapInfo));
- EditorUtility.ClearProgressBar();
- AssetDatabase.Refresh();
- if (dicDown != null && dicDown.Count > 0) {
- foreach (var item in dicDown.Values) {
- item.Dispose();
- }
- dicDown.Clear();
- }
- }
- private void CheckMapInfoAndFiles() {
- if (!File.Exists(EditorConfigs.LocalMapInfoJson)) {
- if (File.Exists(EditorConfigs.Path_Scale)) {
- AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Scale));
- }
- if (File.Exists(EditorConfigs.Path_Ply)) {
- bool success = AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Ply));
- if (!success) AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Ply));
- }
- if (File.Exists(EditorConfigs.Path_PrefabPly)) {
- AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_PrefabPly));
- }
- } else {
- LoadCurrentMapInfo();
- }
- }
- private void LoadCurrentMapInfo() {
- if (File.Exists(EditorConfigs.LocalMapInfoJson)) {
- string strInfo = File.ReadAllText(EditorConfigs.LocalMapInfoJson);
- mapInfo = JsonConvert.DeserializeObject<MapInfo>(strInfo);
- if (IfMapInfoValid()) {
- mapID = mapInfo.data.id;
- companyName = mapInfo.data.companyName;
- companyID = mapInfo.data.companyId;
- programeName = mapInfo.data.programName;
- programeID = mapInfo.data.programId;
- }
- }
- }
- }
|