ModelList.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. using SC.XR.Unity.Module_InputSystem;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. using XRTool.Util;
  8. public class ModelList: System.ICloneable
  9. {
  10. public int id { get; set; }
  11. public bool select { get; set; }
  12. public string name { get; set; }
  13. public int type { get; set; }
  14. public string describe { get; set; }
  15. public List<ModelItem> materialList { get; set; }
  16. public int updateTime { get; set; }
  17. public string typesetting { get; set; }
  18. public bool autoplay { get; set; }
  19. public ObjectTransform objectTransform { get; set; }
  20. public Transform materalLibrary;
  21. public SpotsItem spotsItem;
  22. public GameObject _model;
  23. //组合模型数据
  24. public GameObject Model
  25. {
  26. get
  27. {
  28. if (_model == null)
  29. {
  30. init(false);
  31. }
  32. return _model;
  33. }
  34. }
  35. public void init(bool isAdd)
  36. {
  37. if (_model==null)
  38. {
  39. _model = new GameObject(name);
  40. _model.transform.parent = materalLibrary;
  41. // InitObjectTransform();
  42. _model.SetActive(false);
  43. // initModelObj();
  44. }
  45. /*
  46. if (!isAdd&& spotsItem!=null)
  47. {
  48. //_model = new GameObject("进入场景----" + name);
  49. _model.transform.parent = spotsItem.SpotsObj.transform;
  50. //InitObjectTransform();
  51. // initModelObj();
  52. }*/
  53. //for (int i = 0; i < materialList.Count; i++)
  54. //{
  55. // materialList[i].init();
  56. //}
  57. /*
  58. if (isAdd)
  59. {
  60. for (int i = 0; i < materialList.Count; i++)
  61. {
  62. materialList[i].init();
  63. }
  64. _model.transform.parent = spotsItem.SpotsObj.transform;
  65. //initModelObj();
  66. }*/
  67. }
  68. //public void Init()
  69. //{
  70. // _model = new GameObject(name);
  71. // gameObject.SetActive(false);
  72. // InitObjectTransform();
  73. // _model.transform.parent = materalLibrary;
  74. // _model.transform.localPosition = getInfoPos();
  75. // _model.transform.localEulerAngles = getInfoEul();
  76. //}
  77. Vector3 getInfoPos()
  78. {
  79. if(objectTransform==null)
  80. {
  81. return Vector3.zero;
  82. }
  83. //return Vector3.zero;
  84. if (objectTransform.nowPos == Vector3.zero)
  85. return objectTransform.startPos;
  86. else
  87. return objectTransform.nowPos;
  88. }
  89. Vector3 getInfoEul()
  90. {
  91. if (objectTransform == null)
  92. {
  93. return Vector3.zero;
  94. }
  95. if (objectTransform.nowRot == Vector3.zero)
  96. return objectTransform.startRot;
  97. else
  98. return objectTransform.nowRot;
  99. // return Vector3.zero;
  100. }
  101. Vector3 getInfoScale()
  102. {
  103. if (objectTransform.nowScale == Vector3.zero)
  104. return objectTransform.startScale;
  105. else
  106. return objectTransform.nowScale;
  107. }
  108. bool iscreate = false;
  109. private void InitObjectTransform(Action callback)
  110. {
  111. if(iscreate)
  112. {
  113. callback.Invoke();
  114. return;
  115. }
  116. iscreate = true;
  117. if (objectTransform==null)
  118. {
  119. objectTransform = new ObjectTransform();
  120. objectTransform.startPos = Vector3.zero;
  121. objectTransform.nowPos = Vector3.zero;
  122. objectTransform.startRot = Vector3.zero;
  123. objectTransform.nowRot = Vector3.zero;
  124. objectTransform.startScale = Vector3.one;
  125. objectTransform.nowScale = Vector3.zero;
  126. }
  127. // 处理组合素材
  128. switch (type)
  129. {
  130. case (int)MaterialType.None:
  131. break;
  132. case (int)MaterialType.Image:
  133. case (int)MaterialType.Video:
  134. case (int)MaterialType.Model:
  135. case (int)MaterialType.Text:
  136. case (int)MaterialType.LocaImage:
  137. GameManager.Instance.StartCoroutine(CreateSuCaiObj(this, Model, callback));
  138. break;
  139. case (int)MaterialType.Image_Video_Text:
  140. GameManager.Instance.StartCoroutine(CreateImage_Video_Text(this, Model,callback));
  141. break;
  142. case (int)MaterialType.Model_Text:
  143. GameManager.Instance.StartCoroutine(CreateModel_Text(this, Model, callback));
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. /// <summary>
  150. /// 创建 1图片 2视频 3模型 4文字
  151. /// </summary>
  152. /// <param name="type">素材类型</param>
  153. /// <param name="material">素材数据</param>
  154. /// <param name="spoit">景点</param>
  155. /// <returns></returns>
  156. private IEnumerator CreateSuCaiObj(ModelList material, GameObject spoit, Action callback)
  157. {
  158. List<ObjectValue> objects = new List<ObjectValue>();
  159. List<ModelItem> objs = material.materialList;
  160. for (int i = 0; i < objs.Count; i++)
  161. {
  162. var mat = objs[i];
  163. GameObject go = null;
  164. switch (mat.type)
  165. {
  166. case (int)MaterialType.None:
  167. break;
  168. case (int)MaterialType.Image:
  169. var pImage = ResMgr.Instance.LoadAsync<GameObject>("Image");
  170. yield return pImage;
  171. go = GameObject.Instantiate(pImage.asset as GameObject, spoit.transform);
  172. go.SetActive(true);
  173. go.AddComponent<TemplateImage>();
  174. //var imageitem = go.AddComponent<TemplateImage>();
  175. //imageitem.SetData(mat, material.updateTime);
  176. break;
  177. case (int)MaterialType.Video:
  178. var pVideo = ResMgr.Instance.LoadAsync<GameObject>("Video");
  179. yield return pVideo;
  180. go = GameObject.Instantiate(pVideo.asset as GameObject, spoit.transform);
  181. go.SetActive(true);
  182. TemplateVideo video = go.AddComponent<TemplateVideo>();
  183. video.localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(objs[i].DownloadPath);
  184. video.autoplay = material.autoplay;
  185. //var videoitem = go.AddComponent<TemplateVideo>();
  186. //videoitem.SetData(mat, material.updateTime);
  187. break;
  188. case (int)MaterialType.Model:
  189. var pModel = ResMgr.Instance.LoadAsync<GameObject>("Model");
  190. yield return pModel;
  191. go = GameObject.Instantiate(pModel.asset as GameObject, spoit.transform);
  192. go.SetActive(true);
  193. go.AddComponent<TemplateModel>();
  194. //var modelitem = go.AddComponent<TemplateModel>();
  195. //modelitem.SetData(mat, (int)Time.deltaTime);
  196. //Debug.Log(modelitem.Data.localSavePath);
  197. break;
  198. case (int)MaterialType.Text:
  199. var pText = ResMgr.Instance.LoadAsync<GameObject>("Text");
  200. yield return pText;
  201. go = GameObject.Instantiate(pText.asset as GameObject, spoit.transform);
  202. go.SetActive(true);
  203. go.AddComponent<TemplateText>();
  204. //var textitem = go.AddComponent<TemplateText>();
  205. //textitem.SetData(mat, material.updateTime);
  206. break;
  207. case (int)MaterialType.LocaImage:
  208. var pLocaImage = ResMgr.Instance.LoadAsync<GameObject>("Image");
  209. yield return pLocaImage;
  210. go = GameObject.Instantiate(pLocaImage.asset as GameObject, spoit.transform);
  211. go.SetActive(true);
  212. go.AddComponent<TemplateImage>();
  213. //var locaImageitem = go.AddComponent<TemplateImage>();
  214. //locaImageitem.SetLocaImageData(mat);
  215. break;
  216. default:
  217. break;
  218. }
  219. if (go != null)
  220. {
  221. go.name = string.IsNullOrWhiteSpace(mat.name) ? spoit.name + "-" + i.ToString() : mat.name;
  222. mat.name = go.name;
  223. go.AddComponent<EditorEventHandler>().Type = (MaterialType)(mat.type);
  224. if (material.select)
  225. {
  226. go.transform.localPosition = mat.objectTransform.nowPos;
  227. go.transform.localEulerAngles = mat.objectTransform.nowRot;
  228. go.transform.localScale = mat.objectTransform.nowScale;
  229. }
  230. else
  231. {
  232. go.transform.localEulerAngles = Vector3.zero;
  233. go.transform.localScale = new Vector3(1, 1, 1);
  234. if (mat.type != (int)MaterialType.Text)
  235. {
  236. go.transform.localPosition = Vector3.zero;
  237. }
  238. else
  239. {
  240. //Set text position
  241. if (mat.textPosition == "top")
  242. {
  243. go.transform.localPosition = new Vector3(0, 0.3f, 0);
  244. }
  245. else if (mat.textPosition == "bottom")
  246. {
  247. go.transform.localPosition = new Vector3(0, -0.3f, 0);
  248. }
  249. }
  250. //mat.objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  251. }
  252. mat.prefabModel = go;
  253. callback.Invoke();
  254. // mat.initFrist();
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// 创建 6模型+文字
  260. /// </summary>
  261. /// <param name="material"></param>
  262. /// <param name="spoit"></param>
  263. /// <returns></returns>
  264. private IEnumerator CreateModel_Text(ModelList material, GameObject spoit,Action callback)
  265. {
  266. List<ObjectValue> objects = new List<ObjectValue>();
  267. List<ModelItem> objs = material.materialList;
  268. for (int i = 0; i < objs.Count; i++)
  269. {
  270. var mat = objs[i];
  271. GameObject go = null;
  272. switch (mat.type)
  273. {
  274. case (int)MaterialType.None:
  275. break;
  276. case (int)MaterialType.Model:
  277. var pModel = ResMgr.Instance.LoadAsync<GameObject>("Model");
  278. yield return pModel;
  279. go = GameObject.Instantiate(pModel.asset as GameObject, spoit.transform);
  280. go.SetActive(true);
  281. go.AddComponent<TemplateModel>();
  282. //var modelitem = go.AddComponent<TemplateModel>();
  283. //modelitem.SetData(mat, material.updateTime);
  284. break;
  285. case (int)MaterialType.Text:
  286. var pText = ResMgr.Instance.LoadAsync<GameObject>("Text");
  287. yield return pText;
  288. go = GameObject.Instantiate(pText.asset as GameObject, spoit.transform);
  289. go.SetActive(true);
  290. go.AddComponent<TemplateText>();
  291. //var textitem = go.AddComponent<TemplateText>();
  292. //textitem.SetData(mat, material.updateTime);
  293. break;
  294. default:
  295. break;
  296. }
  297. if (go != null)
  298. {
  299. go.name = string.IsNullOrWhiteSpace(mat.name) ? spoit.name + "-" + i.ToString() : mat.name;
  300. go.AddComponent<EditorEventHandler>().Type = (MaterialType)(mat.type);
  301. if (material.select)
  302. {
  303. go.transform.localPosition = mat.objectTransform.nowPos;
  304. go.transform.localEulerAngles = mat.objectTransform.nowRot;
  305. go.transform.localScale = mat.objectTransform.nowScale;
  306. }
  307. else
  308. {
  309. go.transform.localEulerAngles = Vector3.zero;
  310. go.transform.localScale = new Vector3(1, 1, 1);
  311. if (mat.type != (int)MaterialType.Text)
  312. {
  313. go.transform.localPosition = Vector3.zero;
  314. }
  315. else
  316. {
  317. //Set text position
  318. if (mat.textPosition == "top")
  319. {
  320. go.transform.localPosition = new Vector3(0, 0.3f, 0);
  321. }
  322. else if (mat.textPosition == "bottom")
  323. {
  324. go.transform.localPosition = new Vector3(0, -0.3f, 0);
  325. }
  326. else if (mat.textPosition == "left")
  327. {
  328. go.transform.localPosition = new Vector3(-0.3f, 0, 0);
  329. }
  330. else if (mat.textPosition == "right")
  331. {
  332. go.transform.localPosition = new Vector3(0.3f, 0f, 0);
  333. }
  334. }
  335. // mat.objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  336. }
  337. ObjectValue objectValue = CreateMatObjectValue(i, material.id, go, mat);
  338. objects.Add(objectValue);
  339. mat.prefabModel = go;
  340. callback.Invoke();
  341. // mat.initFrist();
  342. }
  343. }
  344. }
  345. /// <summary>
  346. /// 创建 5图片+文字/视频+文字
  347. /// </summary>
  348. private IEnumerator CreateImage_Video_Text(ModelList material, GameObject spoit, Action callback)
  349. {
  350. yield return GameManager.Instance.StartCoroutine(SRTitleLayout.CalLayout(material, spoit, callback));
  351. /*
  352. List<ObjectValue> objects = new List<ObjectValue>();
  353. //switch (material.typesetting)
  354. //{
  355. // case "1":
  356. // SRTitleLayout.CalLayout(material, spoit);
  357. // break;
  358. // case "2":
  359. // SRSectorLayout.CalLayout(material, spoit);
  360. // break;
  361. // case "3":
  362. // DRTitleLayout.CalLayout(material, spoit);
  363. // break;
  364. // case "4":
  365. // DRFanLayout.CalLayout(material, spoit);
  366. // break;
  367. // case "5":
  368. // SRTitleLayout.CalLayout(material, spoit);
  369. // break;
  370. // case "自定义":
  371. // break;
  372. // default:
  373. // break;
  374. //}
  375. switch (material.typesetting)
  376. {
  377. case "1":
  378. yield return GameManager.Instance.StartCoroutine(SRTitleLayout.CalLayout(material, spoit));
  379. break;
  380. case "2":
  381. yield return GameManager.Instance.StartCoroutine(SRTitleLayout.CalLayout(material, spoit));
  382. break;
  383. case "3":
  384. yield return GameManager.Instance.StartCoroutine(SRTitleLayout.CalLayout(material, spoit));
  385. break;
  386. case "4":
  387. yield return GameManager.Instance.StartCoroutine(SRSectorLayout.CalLayout(material, spoit));
  388. break;
  389. case "5":
  390. yield return GameManager.Instance.StartCoroutine(DRTitleLayout.CalLayout(material, spoit));
  391. break;
  392. case "6":
  393. yield return GameManager.Instance.StartCoroutine(DRTitleLayout.CalLayout(material, spoit));
  394. break;
  395. case "7":
  396. yield return GameManager.Instance.StartCoroutine(DRTitleLayout.CalLayout(material, spoit));
  397. break;
  398. case "8":
  399. yield return GameManager.Instance.StartCoroutine(DRFanLayout.CalLayout(material, spoit));
  400. break;
  401. default:
  402. break;
  403. }
  404. */
  405. }
  406. /// <summary>
  407. /// 创建物体ObjectValue,并赋值位置信息
  408. /// </summary>
  409. /// <param name="id"></param>
  410. /// <param name="parentid"></param>
  411. /// <param name="go"></param>
  412. /// <param name="mat"></param>
  413. /// <returns></returns>
  414. private ObjectValue CreateMatObjectValue(int id, int parentid, GameObject go, ModelItem mat)
  415. {
  416. go.name = string.IsNullOrWhiteSpace(mat.name) ? go.name : mat.name;
  417. go.AddComponent<EditorEventHandler>().Type = (MaterialType)(mat.type);
  418. //为物体位置信息赋值
  419. if (mat.objectTransform != null)
  420. {
  421. go.transform.localPosition = mat.objectTransform.nowPos;
  422. go.transform.localEulerAngles = mat.objectTransform.nowRot;
  423. go.transform.localScale = mat.objectTransform.nowScale;
  424. }
  425. else
  426. {
  427. go.transform.localPosition = Vector3.zero;
  428. go.transform.localEulerAngles = Vector3.zero;
  429. go.transform.localScale = new Vector3(1, 1, 1);
  430. }
  431. ObjectValue objectValue = new ObjectValue(id, parentid, mat.name, go, go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  432. return objectValue;
  433. }
  434. public object Clone()
  435. {
  436. ModelList clonedObject = (ModelList)this.MemberwiseClone();
  437. return clonedObject;
  438. }
  439. public void Clear()
  440. {
  441. iscreate = false;
  442. if (materialList != null)
  443. {
  444. for (int i = 0; i < materialList.Count; i++)
  445. {
  446. materialList[i].Clear();
  447. Debug.Log("Clear SpotsList materialList[i]" + i);
  448. }
  449. }
  450. //GameManager.Instance.destroyGo(_model);
  451. //_model = null;
  452. Debug.Log("Clear SpotsList materialList[i] Clear");
  453. }
  454. public void initModelObj()
  455. {
  456. InitObjectTransform(()=> {
  457. if (getInfoPos() != Vector3.zero)
  458. {
  459. _model.transform.localPosition = getInfoPos();
  460. }
  461. if (getInfoEul() != Vector3.zero)
  462. {
  463. _model.transform.localEulerAngles = getInfoEul();
  464. }
  465. if (getInfoScale() != Vector3.zero)
  466. {
  467. _model.transform.localScale = getInfoScale();
  468. }
  469. if (materialList != null)
  470. {
  471. for (int i = 0; i < materialList.Count; i++)
  472. {
  473. Debug.Log("initModelObj===> " + materialList[i].id);
  474. materialList[i].initModelObj();
  475. }
  476. }
  477. });
  478. // _model.transform.localPosition = getInfoPos();
  479. // _model.transform.localEulerAngles = getInfoEul();
  480. }
  481. }