MaterialShow.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using QFramework;
  6. using QFramework.MREditor;
  7. public class MaterialShow : MonoBehaviour
  8. {
  9. public MaterialObjValue value;
  10. public string type;
  11. public int updateTime;
  12. public string spoitName;
  13. private void Start()
  14. {
  15. switch ((MaterialType)int.Parse(value.type))
  16. {
  17. case MaterialType.NULL:
  18. break;
  19. case MaterialType.Image:
  20. case MaterialType.Video:
  21. case MaterialType.Model:
  22. if (value.DownloadPath.IsNotNullAndEmpty() && value.DownloadPath != value.localSavePath)
  23. {
  24. MsgHandler.AddListener(value.DownloadPath, HandleMsg);
  25. LoadManager.Instance.LoadMaterial(value, updateTime);
  26. }
  27. else if (value.DownloadPath.IsNotNullAndEmpty() && value.DownloadPath == value.localSavePath)
  28. {
  29. // LoadLocalMaterial();
  30. StartCoroutine(LoadLocalMaterial(0.1f));
  31. }
  32. break;
  33. case MaterialType.Text:
  34. //string str = value.textContent;
  35. //object loaderObj = str;
  36. //LoadMaterial(loaderObj);
  37. // LoadLocalMaterial();
  38. StartCoroutine(LoadLocalMaterial(0.1f));
  39. break;
  40. default:
  41. break;
  42. }
  43. }
  44. /// <summary>
  45. /// 返回素材
  46. /// </summary>
  47. /// <param name="msg"></param>
  48. private void HandleMsg(Msg msg)
  49. {
  50. try
  51. {
  52. Debug.Log("HandleMsg");
  53. // LoadMaterial(msg.Value);
  54. StartCoroutine(LoadMaterial(msg.Value, 0.1f));
  55. }
  56. catch (System.Exception e)
  57. {
  58. ErrorLogPanel.Instance.Show(" 返回生成素材出现错误 " + e.Message );
  59. }
  60. }
  61. int num = 0;
  62. /// <summary>
  63. /// 加载素材
  64. /// </summary>
  65. /// <param name="loaderObj"> 返回加载物体 </param>
  66. public void LoadMaterial(object loaderObj)
  67. {
  68. Debug.Log(value.name + " 下载素材@@@@@@@@@@@@@" + num++);
  69. var loader = ResLoader.Allocate();
  70. switch ((MaterialType)int.Parse(value.type))
  71. {
  72. case MaterialType.Text:
  73. string path = "Resources/Template/Prefab/Text1";
  74. //if (int.Parse(value.TextBJ) == 1)
  75. //{
  76. // path = "Resources/Template/Prefab/Text";
  77. //}
  78. switch (value.TextBJ)
  79. {
  80. case "1":
  81. path = "Resources/Template/Prefab/Text1";
  82. break;
  83. case "2":
  84. path = "Resources/Template/Prefab/Text2";
  85. break;
  86. case "3":
  87. path = "Resources/Template/Prefab/Text3";
  88. break;
  89. default:
  90. break;
  91. }
  92. var mText = loader.LoadSync<GameObject>(path);
  93. GameObject objText = (GameObject)mText.Instantiate();
  94. objText.GetComponent<SetTextV>().SetText(loaderObj.ToString());
  95. objText.transform.SetParent(transform);
  96. objText.name = value.name;
  97. objText.transform.localPosition = Vector3.zero;
  98. objText.transform.localEulerAngles = Vector3.zero;
  99. // objText.transform.Identity();
  100. if (value.ObjectTransform.nowScale == Vector3.zero)
  101. {
  102. if (value.textPosition == "bottom")
  103. {
  104. transform.localPosition = new Vector3(0, -0.5f, 0);
  105. Debug.Log("bottom" + transform.localPosition);
  106. }
  107. else if (value.textPosition == "top")
  108. {
  109. transform.localPosition = new Vector3(0, 0.5f, 0);
  110. Debug.Log("top" + transform.localPosition);
  111. }
  112. transform.localEulerAngles = Vector3.zero;
  113. transform.localScale = Vector3.one;
  114. }
  115. else
  116. {
  117. // SettingTransform(value.ObjectTransform);
  118. }
  119. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objText.GetComponent<SelectBJImage>());
  120. break;
  121. case MaterialType.Image:
  122. var image = loader.LoadSync<GameObject>("Resources/Template/Prefab/Image");
  123. GameObject objImage = image.Instantiate();
  124. objImage.GetComponent<SetImageV>().SetImage((Sprite)loaderObj);
  125. objImage.transform.SetParent(transform);
  126. objImage.name = value.name;
  127. objImage.transform.LocalIdentity();
  128. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent<SelectBJImage>());
  129. break;
  130. case MaterialType.Video:
  131. var video = loader.LoadSync<GameObject>("Resources/Template/Prefab/Video");
  132. GameObject objVideo = video.Instantiate();
  133. objVideo.GetComponent<VideoControl>().VideoURL = loaderObj.ToString();
  134. objVideo.transform.SetParent(transform);
  135. objVideo.name = value.name;
  136. objVideo.transform.LocalIdentity();
  137. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent<SelectBJImage>());
  138. break;
  139. case MaterialType.Model:
  140. List<GameObject> listobj = (List<GameObject>)loaderObj;
  141. GameObject model = listobj[0];
  142. GameObject objModel = model.Instantiate();
  143. objModel.transform.SetParent(transform);
  144. objModel.name = value.name;
  145. objModel.transform.LocalIdentity();
  146. break;
  147. default:
  148. break;
  149. }
  150. MsgHandler.RemoveListener(value.DownloadPath, HandleMsg);
  151. SettingTransform(value.ObjectTransform);
  152. }
  153. //private void SettingTransform(MaterialObjValue value)
  154. //{
  155. // if (value.nowScale == Vector3.zero)
  156. // {
  157. // transform.localPosition = value.startPos;
  158. // transform.localEulerAngles = value.startRot;
  159. // transform.localScale = value.startScale;
  160. // }
  161. // else
  162. // {
  163. // transform.localPosition = value.nowPos;
  164. // transform.localEulerAngles = value.nowRot;
  165. // transform.localScale = value.nowScale;
  166. // }
  167. //}
  168. IEnumerator LoadMaterial(object loaderObj , float times)
  169. {
  170. MsgHandler.RemoveListener(value.DownloadPath, HandleMsg);
  171. yield return new WaitForEndOfFrame();
  172. Debug.Log(value.name + " 下载素材@@@@@@@@@@@@@" + num++);
  173. var loader = ResLoader.Allocate();
  174. switch ((MaterialType)int.Parse(value.type))
  175. {
  176. case MaterialType.Text:
  177. string path = "Resources/Template/Prefab/Text1";
  178. //if (int.Parse(value.TextBJ) == 1)
  179. //{
  180. // path = "Resources/Template/Prefab/Text";
  181. //}
  182. switch (value.TextBJ)
  183. {
  184. case "1":
  185. path = "Resources/Template/Prefab/Text1";
  186. break;
  187. case "2":
  188. path = "Resources/Template/Prefab/Text2";
  189. break;
  190. case "3":
  191. path = "Resources/Template/Prefab/Text3";
  192. break;
  193. default:
  194. break;
  195. }
  196. var mText = loader.LoadSync<GameObject>(path);
  197. GameObject objText = (GameObject)mText.Instantiate();
  198. objText.GetComponent<SetTextV>().SetText(loaderObj.ToString());
  199. objText.transform.SetParent(transform);
  200. objText.name = value.name;
  201. objText.transform.localPosition = Vector3.zero;
  202. objText.transform.localEulerAngles = Vector3.zero;
  203. // objText.transform.Identity();
  204. if (value.ObjectTransform.nowScale == Vector3.zero)
  205. {
  206. if (value.textPosition == "bottom")
  207. {
  208. transform.localPosition = new Vector3(0, -0.5f, 0);
  209. Debug.Log("bottom" + transform.localPosition);
  210. }
  211. else if (value.textPosition == "top")
  212. {
  213. transform.localPosition = new Vector3(0, 0.5f, 0);
  214. Debug.Log("top" + transform.localPosition);
  215. }
  216. transform.localEulerAngles = Vector3.zero;
  217. transform.localScale = Vector3.one;
  218. }
  219. else
  220. {
  221. // SettingTransform(value.ObjectTransform);
  222. }
  223. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objText.GetComponent<SelectBJImage>());
  224. break;
  225. case MaterialType.Image:
  226. var image = loader.LoadSync<GameObject>("Resources/Template/Prefab/Image");
  227. GameObject objImage = image.Instantiate();
  228. objImage.GetComponent<SetImageV>().SetImage((Sprite)loaderObj);
  229. objImage.transform.SetParent(transform);
  230. objImage.name = value.name;
  231. objImage.transform.LocalIdentity();
  232. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent<SelectBJImage>());
  233. break;
  234. case MaterialType.Video:
  235. var video = loader.LoadSync<GameObject>("Resources/Template/Prefab/Video");
  236. GameObject objVideo = video.Instantiate();
  237. objVideo.GetComponent<VideoControl>().VideoURL = loaderObj.ToString();
  238. objVideo.transform.SetParent(transform);
  239. objVideo.name = value.name;
  240. objVideo.transform.LocalIdentity();
  241. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent<SelectBJImage>());
  242. break;
  243. case MaterialType.Model:
  244. List<GameObject> listobj = (List<GameObject>)loaderObj;
  245. GameObject model = listobj[0];
  246. GameObject objModel = null;
  247. if(model.name == "Saloon")
  248. {
  249. objModel = GameManager.Instance.Saloon;
  250. }
  251. else
  252. {
  253. objModel = model.Instantiate();
  254. }
  255. objModel.SetActive(true);
  256. objModel.transform.SetParent(transform);
  257. objModel.name = value.name;
  258. objModel.transform.LocalIdentity();
  259. break;
  260. default:
  261. break;
  262. }
  263. SettingTransform(value.ObjectTransform);
  264. }
  265. /// <summary>
  266. /// 加载本地素材
  267. /// </summary>
  268. public void LoadLocalMaterial()
  269. {
  270. try
  271. {
  272. Debug.Log(value.name + " 本地素材@@@@@@@@@@@@@");
  273. var loader = ResLoader.Allocate();
  274. switch ((MaterialType)int.Parse(value.type))
  275. {
  276. case MaterialType.Text:
  277. string path = "Resources/Template/Prefab/Text";
  278. switch (value.TextBJ)
  279. {
  280. case "1":
  281. path = "Resources/Template/Prefab/Text1";
  282. break;
  283. case "2":
  284. path = "Resources/Template/Prefab/Text2";
  285. break;
  286. case "3":
  287. path = "Resources/Template/Prefab/Text3";
  288. break;
  289. default:
  290. break;
  291. }
  292. var mText = loader.LoadSync<GameObject>(path);
  293. GameObject objText = (GameObject)mText.Instantiate();
  294. objText.GetComponent<SetTextV>().SetText(value.textContent);
  295. objText.transform.SetParent(transform);
  296. objText.name = value.name;
  297. objText.transform.localPosition = Vector3.zero;
  298. objText.transform.localEulerAngles = Vector3.zero;
  299. if (value.ObjectTransform.nowScale == Vector3.zero)
  300. {
  301. if (value.textPosition == "bottom")
  302. {
  303. transform.localPosition = new Vector3(0, -0.5f, 0);
  304. Debug.Log("bottom" + transform.localPosition);
  305. }
  306. else if (value.textPosition == "top")
  307. {
  308. transform.localPosition = new Vector3(0, 0.5f, 0);
  309. Debug.Log("top" + transform.localPosition);
  310. }
  311. transform.localEulerAngles = value.ObjectTransform.startRot;
  312. if (value.ObjectTransform.startScale != Vector3.zero)
  313. transform.localScale = value.ObjectTransform.startScale;
  314. else
  315. transform.localScale = Vector3.one;
  316. }
  317. else
  318. {
  319. SettingTransform(value.ObjectTransform);
  320. }
  321. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objText.GetComponent<SelectBJImage>());
  322. break;
  323. case MaterialType.Image:
  324. var image = loader.LoadSync<GameObject>("Resources/Template/Prefab/Image");
  325. GameObject objImage = image.Instantiate();
  326. objImage.GetComponent<SetImageV>().SetImage(loader.LoadSync<Sprite>(value.DownloadPath));
  327. objImage.transform.SetParent(transform);
  328. objImage.name = value.name;
  329. objImage.transform.LocalIdentity();
  330. SettingTransform(value.ObjectTransform);
  331. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent<SelectBJImage>());
  332. break;
  333. case MaterialType.Video:
  334. var video = loader.LoadSync<GameObject>("Resources/Template/Prefab/Video");
  335. GameObject objVideo = video.Instantiate();
  336. objVideo.GetComponent<VideoControl>().VideoURL = value.DownloadPath;
  337. objVideo.transform.SetParent(transform);
  338. objVideo.name = value.name;
  339. objVideo.transform.LocalIdentity();
  340. SettingTransform(value.ObjectTransform);
  341. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent<SelectBJImage>());
  342. break;
  343. case MaterialType.Model:
  344. GameObject model = loader.LoadSync<GameObject>(value.DownloadPath);
  345. GameObject objModel = model.Instantiate();
  346. objModel.transform.SetParent(transform);
  347. objModel.name = value.name;
  348. objModel.transform.LocalIdentity();
  349. SettingTransform(value.ObjectTransform);
  350. break;
  351. default:
  352. break;
  353. }
  354. }
  355. catch (System.Exception)
  356. {
  357. ErrorLogPanel.Instance.Show(" 加载本地素材出现错误 ");
  358. }
  359. }
  360. IEnumerator LoadLocalMaterial(float times)
  361. {
  362. yield return new WaitForEndOfFrame();
  363. try
  364. {
  365. Debug.Log(value.name + " 本地素材@@@@@@@@@@@@@");
  366. var loader = ResLoader.Allocate();
  367. switch ((MaterialType)int.Parse(value.type))
  368. {
  369. case MaterialType.Text:
  370. string path = "Resources/Template/Prefab/Text";
  371. switch (value.TextBJ)
  372. {
  373. case "1":
  374. path = "Resources/Template/Prefab/Text1";
  375. break;
  376. case "2":
  377. path = "Resources/Template/Prefab/Text2";
  378. break;
  379. case "3":
  380. path = "Resources/Template/Prefab/Text3";
  381. break;
  382. default:
  383. break;
  384. }
  385. var mText = loader.LoadSync<GameObject>(path);
  386. GameObject objText = (GameObject)mText.Instantiate();
  387. objText.GetComponent<SetTextV>().SetText(value.textContent);
  388. objText.transform.SetParent(transform);
  389. objText.name = value.name;
  390. objText.transform.localPosition = Vector3.zero;
  391. objText.transform.localEulerAngles = Vector3.zero;
  392. if (value.ObjectTransform.nowScale == Vector3.zero)
  393. {
  394. if (value.textPosition == "bottom")
  395. {
  396. transform.localPosition = new Vector3(0, -0.5f, 0);
  397. Debug.Log("bottom" + transform.localPosition);
  398. }
  399. else if (value.textPosition == "top")
  400. {
  401. transform.localPosition = new Vector3(0, 0.5f, 0);
  402. Debug.Log("top" + transform.localPosition);
  403. }
  404. transform.localEulerAngles = value.ObjectTransform.startRot;
  405. if (value.ObjectTransform.startScale != Vector3.zero)
  406. transform.localScale = value.ObjectTransform.startScale;
  407. else
  408. transform.localScale = Vector3.one;
  409. }
  410. else
  411. {
  412. SettingTransform(value.ObjectTransform);
  413. }
  414. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objText.GetComponent<SelectBJImage>());
  415. break;
  416. case MaterialType.Image:
  417. var image = loader.LoadSync<GameObject>("Resources/Template/Prefab/Image");
  418. GameObject objImage = image.Instantiate();
  419. objImage.GetComponent<SetImageV>().SetImage(loader.LoadSync<Sprite>(value.DownloadPath));
  420. objImage.transform.SetParent(transform);
  421. objImage.name = value.name;
  422. objImage.transform.LocalIdentity();
  423. SettingTransform(value.ObjectTransform);
  424. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent<SelectBJImage>());
  425. break;
  426. case MaterialType.Video:
  427. var video = loader.LoadSync<GameObject>("Resources/Template/Prefab/Video");
  428. GameObject objVideo = video.Instantiate();
  429. objVideo.GetComponent<VideoControl>().VideoURL = value.DownloadPath;
  430. objVideo.transform.SetParent(transform);
  431. objVideo.name = value.name;
  432. objVideo.transform.LocalIdentity();
  433. SettingTransform(value.ObjectTransform);
  434. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent<SelectBJImage>());
  435. break;
  436. case MaterialType.Model:
  437. GameObject model = loader.LoadSync<GameObject>(value.DownloadPath);
  438. GameObject objModel = model.Instantiate();
  439. objModel.SetActive(true);
  440. objModel.transform.SetParent(transform);
  441. objModel.name = value.name;
  442. objModel.transform.LocalIdentity();
  443. SettingTransform(value.ObjectTransform);
  444. break;
  445. default:
  446. break;
  447. }
  448. }
  449. catch (System.Exception)
  450. {
  451. ErrorLogPanel.Instance.Show(" 加载本地素材出现错误 ");
  452. }
  453. }
  454. /// <summary>
  455. /// 设置Transform
  456. /// </summary>
  457. /// <param name="value"></param>
  458. private void SettingTransform(ObjectTransform value)
  459. {
  460. if (value.nowScale == Vector3.zero)
  461. {
  462. if (value.startScale == Vector3.zero)
  463. value.startScale = Vector3.one;
  464. transform.localPosition = value.startPos;
  465. transform.localEulerAngles = value.startRot;
  466. transform.localScale = value.startScale;
  467. Debug.Log("Start " + value.startPos.x);
  468. }
  469. else
  470. {
  471. transform.localPosition = value.nowPos;
  472. transform.localEulerAngles = value.nowRot;
  473. transform.localScale = value.nowScale;
  474. Debug.Log("now " + value.nowPos.x);
  475. }
  476. }
  477. }