SeengeneXDKSystem.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using UnityEngine.Networking;
  10. using UnityEditor.SceneManagement;
  11. using Application = UnityEngine.Application;
  12. using Seengene.XDKUnityPluginCloud;
  13. using Newtonsoft.Json.Linq;
  14. public class SeengeneXDKSystem : EditorWindow {
  15. static GameObject m_NeedCreatedGameObject;
  16. static Material m_CustomMaterial;
  17. static SceneAsset m_Scene;
  18. static Dictionary<string, EditorDownloadObject> dicDown;
  19. static bool ifDownloadFinish = true;
  20. static Action _callback;
  21. static long mapID = 1;
  22. static string companyName;
  23. static string companyID;
  24. static string programeName;
  25. static string programeID;
  26. static MapInfo mapInfo = null;
  27. private static SeengeneXDKSystem m_Instance;
  28. public static SeengeneXDKSystem Instance {
  29. get { return m_Instance; }
  30. private set { m_Instance = value; }
  31. }
  32. [MenuItem("Window/Seengene XDK")]
  33. static void CreatWindows() {
  34. m_Instance = (SeengeneXDKSystem)EditorWindow.GetWindow(typeof(SeengeneXDKSystem));
  35. m_Instance.minSize = new Vector2(600, 420);
  36. m_Instance.titleContent = new GUIContent("Seengene XDK");
  37. m_Instance.autoRepaintOnSceneChange = true;
  38. Texture titleTexture = AssetDatabase.LoadAssetAtPath<Texture>(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_TitleIcon));
  39. if (titleTexture != null)
  40. m_Instance.titleContent.image = titleTexture;
  41. if (!Directory.Exists(Application.streamingAssetsPath)) {
  42. Directory.CreateDirectory(Application.streamingAssetsPath);
  43. }
  44. string directoryScale = EditorConfigs.Path_Scale.Replace(Path.GetFileName(EditorConfigs.Path_Scale), "");
  45. if (!Directory.Exists(directoryScale)) {
  46. Directory.CreateDirectory(directoryScale);
  47. }
  48. string directoryPLY = EditorConfigs.Path_Ply.Replace(Path.GetFileName(EditorConfigs.Path_Ply), "");
  49. if (!Directory.Exists(directoryPLY)) {
  50. Directory.CreateDirectory(directoryPLY);
  51. }
  52. string directoryPrefabs = EditorConfigs.Path_PrefabPly.Replace(Path.GetFileName(EditorConfigs.Path_PrefabPly), "");
  53. if (!Directory.Exists(directoryPrefabs)) {
  54. Directory.CreateDirectory(directoryPrefabs);
  55. }
  56. AssetDatabase.Refresh();
  57. }
  58. SeengeneXDKSystem() {
  59. if (m_Instance == null)
  60. m_Instance = this;
  61. SetDefault();
  62. }
  63. static void SetDefault() {
  64. mapID = 1;
  65. mapInfo = null;
  66. companyName = "";
  67. companyID = "";
  68. programeName = "";
  69. programeID = "";
  70. }
  71. void OnGUI() {
  72. GUILayout.BeginVertical("Box");
  73. GUI_MapInfo();
  74. GUILayout.BeginHorizontal("Box");
  75. if (GUILayout.Button("1. Get Map Infomation")) {
  76. if (!IfMapFilesExist())
  77. DownloadMapInfo();
  78. }
  79. if (IfMapInfoValid()) {
  80. if (GUILayout.Button("2. Download Map Files")) {
  81. if (!IfMapFilesExist())
  82. DownLoadMap();
  83. }
  84. if (IfMapInfoValid() && ifDownloadFinish && IfMapFilesExist()) {
  85. GUILayout.Space(20);
  86. GUILayout.Label("下载进度:100 % 100", GUILayout.Width(150));
  87. } else {
  88. if (dicDown != null && dicDown.Count > 0) {
  89. GUILayout.Space(20);
  90. GUILayout.Label(string.Format("下载进度:{0} 100", Get_Progress().ToString("00 %")), GUILayout.Width(150));
  91. }
  92. }
  93. }
  94. GUILayout.EndHorizontal();
  95. if (IfMapInfoValid() && ifDownloadFinish && IfMapFilesExist()) {
  96. GUI_SceneSetUp();
  97. }
  98. GUILayout.EndVertical();
  99. }
  100. private void GUI_MapInfo() {
  101. GUILayout.BeginHorizontal("Box"); //开始一个水平布局
  102. GUILayout.Space(10);
  103. GUI.skin.label.fontSize = 16;
  104. GUI.skin.label.alignment = TextAnchor.MiddleCenter;
  105. GUILayout.Label("Map Config");
  106. GUILayout.Space(10);
  107. GUILayout.EndHorizontal(); //结束一个水平布局
  108. GUILayout.BeginHorizontal("Box");
  109. GUI.skin.label.fontSize = 11;
  110. GUI.skin.label.alignment = TextAnchor.MiddleLeft;
  111. GUILayout.Label("Map ID:", GUILayout.Width(120));
  112. if (!File.Exists(EditorConfigs.LocalMapInfoJson)) {
  113. mapID = EditorGUILayout.LongField(mapID, GUILayout.MaxHeight(16));
  114. } else {
  115. EditorGUILayout.LabelField(mapID.ToString(), GUILayout.MaxHeight(16));
  116. if (GUILayout.Button("- RESET ALL -", GUILayout.Width(130))) {
  117. RestoreFactory();
  118. }
  119. }
  120. GUILayout.EndHorizontal();
  121. GUILayout.Space(10);
  122. GUILayout.BeginHorizontal("Box");
  123. GUI.skin.label.fontSize = 11;
  124. GUILayout.Label("Company:", GUILayout.Width(120));
  125. EditorGUILayout.LabelField(companyName);
  126. GUILayout.EndHorizontal();
  127. GUILayout.Space(10);
  128. GUILayout.BeginHorizontal("Box");
  129. GUILayout.Label("CompanyID:", GUILayout.Width(120));
  130. EditorGUILayout.LabelField(companyID);
  131. GUILayout.EndHorizontal();
  132. GUILayout.Space(10);
  133. GUILayout.BeginHorizontal("Box");
  134. GUILayout.Label("Programe:", GUILayout.Width(120));
  135. EditorGUILayout.LabelField(programeName);
  136. GUILayout.EndHorizontal();
  137. GUILayout.Space(10);
  138. GUILayout.BeginHorizontal("Box");
  139. GUILayout.Label("ProgrameID:", GUILayout.Width(120));
  140. EditorGUILayout.LabelField(programeID);
  141. GUILayout.EndHorizontal();
  142. GUILayout.Space(10);
  143. }
  144. private void GUI_SceneSetUp() {
  145. GUILayout.Space(10);
  146. GUILayout.BeginHorizontal("Box");
  147. GUI.skin.label.fontSize = 16;
  148. GUI.skin.label.alignment = TextAnchor.MiddleCenter;
  149. //GUILayout.Space(15);
  150. GUILayout.Label("Scene Setup");
  151. GUILayout.EndHorizontal();
  152. GUILayout.BeginVertical("Box");
  153. GUI.skin.label.fontSize = 11;
  154. GUI.skin.label.alignment = TextAnchor.UpperLeft;
  155. //绘制当前正在编辑的场景
  156. GUILayout.Space(10);
  157. m_Scene = (SceneAsset)EditorGUILayout.ObjectField("Source Scene", m_Scene, typeof(SceneAsset), true);
  158. if (File.Exists(EditorConfigs.Path_Scene)) {
  159. m_Scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Scene));
  160. }
  161. //绘制对象
  162. GUILayout.Space(10);
  163. m_NeedCreatedGameObject = (GameObject)EditorGUILayout.ObjectField("Source Object", m_NeedCreatedGameObject, typeof(GameObject), true);
  164. if (File.Exists(EditorConfigs.Path_Ply)) {
  165. m_NeedCreatedGameObject = AssetDatabase.LoadAssetAtPath<GameObject>(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Ply));
  166. }
  167. // 点云自定义材质
  168. GUILayout.Space(10);
  169. m_CustomMaterial = (Material)EditorGUILayout.ObjectField("Custom Material", m_CustomMaterial, typeof(Material), true);
  170. if (File.Exists(EditorConfigs.Path_CustomPLYMaterial)) {
  171. m_CustomMaterial = AssetDatabase.LoadAssetAtPath<Material>(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_CustomPLYMaterial));
  172. }
  173. GUILayout.Space(6);
  174. if (m_Scene != null && m_NeedCreatedGameObject != null && m_CustomMaterial != null) {
  175. if (GUILayout.Button("3. Setup Scene")) {
  176. SceneSetupObject.ChangeCoordinate(m_NeedCreatedGameObject, m_CustomMaterial);
  177. SeengeneXDKSystem.Instance.ShowNotification(new GUIContent("Setup Scene Complete!"));
  178. }
  179. }
  180. GUILayout.Space(6);
  181. GUILayout.EndVertical();
  182. }
  183. private void DownloadMapInfo() {
  184. mapInfo = null;
  185. string url = string.Format("http://{0}:{1}/{2}{3}", EditorConfigs.IP, EditorConfigs.Port, EditorConfigs.GetMapInfoURL, mapID);
  186. Debug.LogFormat("Start to Get MapInformation: {0}", url);
  187. this.StartCoroutine(EnumDownloadMapInfo(delegate (string strInfo) {
  188. try {
  189. Debug.LogFormat("Map Information: {0}", strInfo);
  190. JObject jo = JObject.Parse(strInfo);
  191. //Debug.LogFormat("msg:{0}, code:{1}, data:{2}", jo["msg"], jo["code"], jo["data"]);
  192. if (jo["code"].ToString() != "200") {
  193. Debug.LogErrorFormat("Map Infomation Error, code:{0}, msg:{1}", jo["code"], jo["msg"]);
  194. } else if (string.IsNullOrEmpty(jo["data"].ToString()) || jo["data"].ToString() == "error") {
  195. Debug.LogErrorFormat("Map Infomation Error, code:{0}, msg:{1},data:{2}", jo["code"], jo["msg"], jo["data"]);
  196. } else {
  197. mapInfo = JsonConvert.DeserializeObject<MapInfo>(strInfo);
  198. if (mapInfo != null) {
  199. companyName = mapInfo.data.companyName;
  200. companyID = mapInfo.data.companyId;
  201. programeName = mapInfo.data.programName;
  202. programeID = mapInfo.data.programId;
  203. }
  204. }
  205. } catch (Exception e) {
  206. Debug.LogErrorFormat("Map Infomation Anylizition Error, MapInfor: {0}, error: {1}", strInfo, e);
  207. SetDefault();
  208. }
  209. }, url));
  210. }
  211. private void DownLoadMap() {
  212. Dictionary<string, string> downLoadURLDic = null;
  213. downLoadURLDic = new Dictionary<string, string>();
  214. if (!string.IsNullOrEmpty(mapInfo.data.dense_model_unity_url)) {
  215. downLoadURLDic.Add("PLY", mapInfo.data.dense_model_unity_url);
  216. }
  217. if (!string.IsNullOrEmpty(mapInfo.data.scaleUrl)) {
  218. downLoadURLDic.Add("SCALE", mapInfo.data.scaleUrl);
  219. }
  220. //if (!string.IsNullOrEmpty(mapInfo.data.map_v2_bin_url)) {
  221. // downLoadURLDic.Add("BIN", mapInfo.data.map_v2_bin_url);
  222. //}
  223. if (downLoadURLDic != null) {
  224. DownLoad(downLoadURLDic, delegate () {
  225. SeengeneXDKSystem.Instance.ShowNotification(new GUIContent("Download Complete!"));
  226. if (IfMapInfoValid() && !File.Exists(EditorConfigs.LocalMapInfoJson))
  227. File.WriteAllText(EditorConfigs.LocalMapInfoJson, JsonConvert.SerializeObject(mapInfo));
  228. //刷新
  229. AssetDatabase.Refresh();
  230. });
  231. }
  232. }
  233. private bool IfMapInfoValid() {
  234. return mapInfo != null && mapInfo.code == 200 && mapInfo.data != null;
  235. }
  236. private bool IfMapFilesExist() {
  237. bool ifPlyExists = File.Exists(EditorConfigs.Path_Ply);
  238. bool ifScaleExists = File.Exists(EditorConfigs.Path_Scale);
  239. return ifPlyExists && ifScaleExists;
  240. }
  241. private void RestoreFactory() {
  242. if (File.Exists(EditorConfigs.LocalMapInfoJson)) {
  243. AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.LocalMapInfoJson));
  244. }
  245. if (File.Exists(EditorConfigs.Path_Scale)) {
  246. AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Scale));
  247. }
  248. if (File.Exists(EditorConfigs.Path_Ply)) {
  249. AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Ply));
  250. }
  251. if (File.Exists(EditorConfigs.Path_PrefabPly)) {
  252. AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_PrefabPly));
  253. }
  254. SetDefault();
  255. SceneSetupObject.DeleteMapRootDemo();
  256. AssetDatabase.Refresh();
  257. }
  258. //请求地图数据信息
  259. IEnumerator EnumDownloadMapInfo(Action<string> callBack, string url) {
  260. WWWForm form = new WWWForm();
  261. UnityWebRequest webRequest = UnityWebRequest.Post(url, form);
  262. yield return webRequest.SendWebRequest();
  263. if (webRequest.isHttpError || webRequest.isNetworkError) {
  264. Debug.LogError(webRequest.error);
  265. SetDefault();
  266. } else {
  267. string mes = webRequest.downloadHandler.text;
  268. callBack(mes);
  269. }
  270. }
  271. private void DownLoad(Dictionary<string, string> downLoadURLDic, System.Action call_Back) {
  272. ifDownloadFinish = false;
  273. dicDown = new Dictionary<string, EditorDownloadObject>();
  274. if (downLoadURLDic == null) {
  275. ifDownloadFinish = true;
  276. }
  277. _callback = call_Back;
  278. foreach (var kvURL in downLoadURLDic) {
  279. string destLocalPath = string.Empty;
  280. switch (kvURL.Key) {
  281. case "PLY":
  282. destLocalPath = EditorConfigs.Path_Ply;
  283. break;
  284. case "SCALE":
  285. destLocalPath = EditorConfigs.Path_Scale;
  286. break;
  287. default:
  288. break;
  289. }
  290. EditorDownloadObject dlObj = new EditorDownloadObject();
  291. dicDown.Add(destLocalPath, dlObj);
  292. dlObj.DownLoad(kvURL.Value, destLocalPath, delegate {
  293. dicDown.Remove(destLocalPath);
  294. Debug.LogFormat("Download Complete: {0}, remain:{1}", destLocalPath, dicDown.Count);
  295. if (dicDown != null && dicDown.Count == 0)
  296. ifDownloadFinish = true;
  297. });
  298. }
  299. if (dicDown != null && dicDown.Count == 0) {
  300. SeengeneXDKSystem.Instance.ShowNotification(new GUIContent("Download Complete"));
  301. ifDownloadFinish = true;
  302. }
  303. }
  304. void Update() {
  305. if (ifDownloadFinish == false) {
  306. if (dicDown != null && dicDown.Count > 0) {
  307. string info = "";
  308. info = Path.GetFileName(dicDown.ToList().First().Key);
  309. float progress = Get_Progress();
  310. //Debug.LogFormat("下载中: {0}, {1}", info, progress);
  311. EditorUtility.DisplayProgressBar("Downloadig Map Files", info, progress);
  312. //if (progress <= 1) {
  313. // Repaint();
  314. //}
  315. }
  316. }
  317. if (dicDown != null && dicDown.Count == 0 && ifDownloadFinish) {
  318. dicDown = null;
  319. AssetDatabase.Refresh();
  320. EditorUtility.ClearProgressBar();
  321. _callback();
  322. }
  323. }
  324. private void OnInspectorUpdate() {
  325. Repaint();
  326. }
  327. public float Get_Progress() {
  328. float sum = 0;
  329. if (dicDown != null && dicDown.Count > 0) {
  330. foreach (var item in dicDown.Values) {
  331. sum += item.progress;
  332. }
  333. return sum / dicDown.Count;
  334. }
  335. return 1;
  336. }
  337. private void OnEnable() {
  338. CheckMapInfoAndFiles();
  339. }
  340. private void OnDisable() {
  341. this.StopAllCoroutines();
  342. if (IfMapInfoValid() && !File.Exists(EditorConfigs.LocalMapInfoJson) && File.Exists(EditorConfigs.LocalMapInfoJson))
  343. File.WriteAllText(EditorConfigs.LocalMapInfoJson, JsonConvert.SerializeObject(mapInfo));
  344. EditorUtility.ClearProgressBar();
  345. AssetDatabase.Refresh();
  346. if (dicDown != null && dicDown.Count > 0) {
  347. foreach (var item in dicDown.Values) {
  348. item.Dispose();
  349. }
  350. dicDown.Clear();
  351. }
  352. }
  353. private void CheckMapInfoAndFiles() {
  354. if (!File.Exists(EditorConfigs.LocalMapInfoJson)) {
  355. if (File.Exists(EditorConfigs.Path_Scale)) {
  356. AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Scale));
  357. }
  358. if (File.Exists(EditorConfigs.Path_Ply)) {
  359. bool success = AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Ply));
  360. if (!success) AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_Ply));
  361. }
  362. if (File.Exists(EditorConfigs.Path_PrefabPly)) {
  363. AssetDatabase.DeleteAsset(XDKTools.GetRelativePathFromFullPath(EditorConfigs.Path_PrefabPly));
  364. }
  365. } else {
  366. LoadCurrentMapInfo();
  367. }
  368. }
  369. private void LoadCurrentMapInfo() {
  370. if (File.Exists(EditorConfigs.LocalMapInfoJson)) {
  371. string strInfo = File.ReadAllText(EditorConfigs.LocalMapInfoJson);
  372. mapInfo = JsonConvert.DeserializeObject<MapInfo>(strInfo);
  373. if (IfMapInfoValid()) {
  374. mapID = mapInfo.data.id;
  375. companyName = mapInfo.data.companyName;
  376. companyID = mapInfo.data.companyId;
  377. programeName = mapInfo.data.programName;
  378. programeID = mapInfo.data.programId;
  379. }
  380. }
  381. }
  382. }