MaterialShow.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. }
  31. break;
  32. case MaterialType.Text:
  33. //string str = value.textContent;
  34. //object loaderObj = str;
  35. //LoadMaterial(loaderObj);
  36. LoadLocalMaterial();
  37. break;
  38. default:
  39. break;
  40. }
  41. }
  42. /// <summary>
  43. /// 返回素材
  44. /// </summary>
  45. /// <param name="msg"></param>
  46. private void HandleMsg(Msg msg)
  47. {
  48. try
  49. {
  50. LoadMaterial(msg.Value);
  51. }
  52. catch (System.Exception e)
  53. {
  54. ErrorLogPanel.Instance.Show(" 返回生成素材出现错误 " + e.Message);
  55. }
  56. }
  57. int num = 0;
  58. /// <summary>
  59. /// 加载素材
  60. /// </summary>
  61. /// <param name="loaderObj"> 返回加载物体 </param>
  62. public void LoadMaterial(object loaderObj)
  63. {
  64. Debug.Log(value.name+" 下载素材@@@@@@@@@@@@@"+ num++);
  65. var loader = ResLoader.Allocate();
  66. switch ((MaterialType)int.Parse(value.type))
  67. {
  68. case MaterialType.Text:
  69. string path = "Resources/Template/Prefab/Text1";
  70. //if (int.Parse(value.TextBJ) == 1)
  71. //{
  72. // path = "Resources/Template/Prefab/Text";
  73. //}
  74. switch (value.TextBJ)
  75. {
  76. case "1":
  77. path = "Resources/Template/Prefab/Text1";
  78. break;
  79. case "2":
  80. path = "Resources/Template/Prefab/Text2";
  81. break;
  82. case "3":
  83. path = "Resources/Template/Prefab/Text3";
  84. break;
  85. default:
  86. break;
  87. }
  88. var mText = loader.LoadSync<GameObject>(path);
  89. GameObject objText = (GameObject)mText.Instantiate();
  90. objText.GetComponent<SetTextV>().SetText(loaderObj.ToString());
  91. objText.transform.SetParent(transform);
  92. objText.name = value.name;
  93. objText.transform.LocalIdentity();
  94. if (value.ObjectTransform.nowScale == Vector3.zero)
  95. {
  96. if (value.textPosition == "bottom")
  97. {
  98. transform.localPosition = new Vector3(0, -0.5f, 0);
  99. Debug.Log("bottom" + transform.localPosition);
  100. }
  101. else if (value.textPosition == "top")
  102. {
  103. transform.localPosition = new Vector3(0, 0.5f, 0);
  104. Debug.Log("top" + transform.localPosition);
  105. }
  106. transform.localEulerAngles = Vector3.zero;
  107. transform.localScale = Vector3.one;
  108. }
  109. else
  110. {
  111. // SettingTransform(value.ObjectTransform);
  112. }
  113. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName+value.name, objText.GetComponent<SelectBJImage>());
  114. break;
  115. case MaterialType.Image:
  116. var image = loader.LoadSync<GameObject>("Resources/Template/Prefab/Image");
  117. GameObject objImage = image.Instantiate();
  118. objImage.GetComponent<SetImageV>().SetImage((Sprite)loaderObj);
  119. objImage.transform.SetParent(transform);
  120. objImage.name = value.name;
  121. objImage.transform.LocalIdentity();
  122. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent<SelectBJImage>());
  123. break;
  124. case MaterialType.Video:
  125. var video = loader.LoadSync<GameObject>("Resources/Template/Prefab/Video");
  126. GameObject objVideo = video.Instantiate();
  127. objVideo.GetComponent<VideoControl>().VideoURL = loaderObj.ToString();
  128. objVideo.transform.SetParent(transform);
  129. objVideo.name = value.name;
  130. objVideo.transform.LocalIdentity();
  131. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent<SelectBJImage>());
  132. break;
  133. case MaterialType.Model:
  134. List<GameObject> listobj = (List<GameObject>)loaderObj;
  135. GameObject model = listobj[0];
  136. Debug.Log(model.name);
  137. GameObject objModel = null;
  138. if (model.name == "Saloon")
  139. {
  140. objModel = GameManager.Instance.Saloon;
  141. objModel.SetActive(true);
  142. }
  143. else
  144. {
  145. objModel = model.Instantiate();
  146. }
  147. if (model.name == "MainDargGames6")
  148. {
  149. GameManager.Instance.SZG.transform.SetParent(transform);
  150. GameManager.Instance.SZG.transform.LocalIdentity();
  151. }
  152. objModel.transform.SetParent(transform);
  153. objModel.name = value.name;
  154. objModel.transform.LocalIdentity();
  155. break;
  156. default:
  157. break;
  158. }
  159. MsgHandler.RemoveListener(value.DownloadPath, HandleMsg);
  160. SettingTransform(value.ObjectTransform);
  161. }
  162. //private void SettingTransform(MaterialObjValue value)
  163. //{
  164. // if (value.nowScale == Vector3.zero)
  165. // {
  166. // transform.localPosition = value.startPos;
  167. // transform.localEulerAngles = value.startRot;
  168. // transform.localScale = value.startScale;
  169. // }
  170. // else
  171. // {
  172. // transform.localPosition = value.nowPos;
  173. // transform.localEulerAngles = value.nowRot;
  174. // transform.localScale = value.nowScale;
  175. // }
  176. //}
  177. /// <summary>
  178. /// 加载本地素材
  179. /// </summary>
  180. public void LoadLocalMaterial()
  181. {
  182. try
  183. {
  184. Debug.Log(value.name + " 本地素材@@@@@@@@@@@@@");
  185. var loader = ResLoader.Allocate();
  186. switch ((MaterialType)int.Parse(value.type))
  187. {
  188. case MaterialType.Text:
  189. string path = "Resources/Template/Prefab/Text";
  190. switch (value.TextBJ)
  191. {
  192. case "1":
  193. path = "Resources/Template/Prefab/Text1";
  194. break;
  195. case "2":
  196. path = "Resources/Template/Prefab/Text2";
  197. break;
  198. case "3":
  199. path = "Resources/Template/Prefab/Text3";
  200. break;
  201. default:
  202. break;
  203. }
  204. var mText = loader.LoadSync<GameObject>(path);
  205. GameObject objText = (GameObject)mText.Instantiate();
  206. objText.GetComponent<SetTextV>().SetText(value.textContent);
  207. objText.transform.SetParent(transform);
  208. objText.name = value.name;
  209. objText.transform.LocalIdentity();
  210. if (value.ObjectTransform.nowScale == Vector3.zero)
  211. {
  212. if (value.textPosition == "bottom")
  213. {
  214. transform.localPosition = new Vector3(0, -0.5f, 0);
  215. Debug.Log("bottom" + transform.localPosition);
  216. }
  217. else if (value.textPosition == "top")
  218. {
  219. transform.localPosition = new Vector3(0, 0.5f, 0);
  220. Debug.Log("top" + transform.localPosition);
  221. }
  222. transform.localEulerAngles = value.ObjectTransform.startRot;
  223. if (value.ObjectTransform.startScale != Vector3.zero)
  224. transform.localScale = value.ObjectTransform.startScale;
  225. else
  226. transform.localScale = Vector3.one;
  227. }
  228. else
  229. {
  230. SettingTransform(value.ObjectTransform);
  231. // value.ObjectTransform.SetObjectTransform(transform);
  232. }
  233. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objText.GetComponent<SelectBJImage>());
  234. break;
  235. case MaterialType.Image:
  236. var image = loader.LoadSync<GameObject>("Resources/Template/Prefab/Image");
  237. GameObject objImage = image.Instantiate();
  238. objImage.GetComponent<SetImageV>().SetImage(loader.LoadSync<Sprite>(value.DownloadPath));
  239. objImage.transform.SetParent(transform);
  240. objImage.name = value.name;
  241. objImage.transform.LocalIdentity();
  242. SettingTransform(value.ObjectTransform);
  243. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent<SelectBJImage>());
  244. break;
  245. case MaterialType.Video:
  246. var video = loader.LoadSync<GameObject>("Resources/Template/Prefab/Video");
  247. GameObject objVideo = video.Instantiate();
  248. objVideo.GetComponent<VideoControl>().VideoURL = value.DownloadPath;
  249. objVideo.transform.SetParent(transform);
  250. objVideo.name = value.name;
  251. objVideo.transform.LocalIdentity();
  252. SettingTransform(value.ObjectTransform);
  253. UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent<SelectBJImage>());
  254. break;
  255. case MaterialType.Model:
  256. GameObject model = loader.LoadSync<GameObject>(value.DownloadPath);
  257. GameObject objModel = model.Instantiate();
  258. objModel.transform.SetParent(transform);
  259. objModel.name = value.name;
  260. objModel.transform.LocalIdentity();
  261. SettingTransform(value.ObjectTransform);
  262. break;
  263. default:
  264. break;
  265. }
  266. }
  267. catch (System.Exception)
  268. {
  269. ErrorLogPanel.Instance.Show(" 加载本地素材出现错误 ");
  270. }
  271. //SettingTransform(value.ObjectTransform);
  272. }
  273. /// <summary>
  274. /// 设置Transform
  275. /// </summary>
  276. /// <param name="value"></param>
  277. private void SettingTransform(ObjectTransform value)
  278. {
  279. if (value.nowScale == Vector3.zero)
  280. {
  281. if (value.startScale == Vector3.zero)
  282. value.startScale = Vector3.one;
  283. transform.localPosition = value.startPos;
  284. transform.localEulerAngles = value.startRot;
  285. transform.localScale = value.startScale;
  286. Debug.Log(value.nowScale.x + " Start " + value.startPos.x);
  287. GameManager.Instance.text4.text = value.nowScale.x.ToString()+" "+ this.value.name + " " + value.nowPos.x.ToString();
  288. }
  289. else
  290. {
  291. transform.localPosition = value.nowPos;
  292. transform.localEulerAngles = value.nowRot;
  293. transform.localScale = value.nowScale;
  294. Debug.Log("now " + value.nowPos.x);
  295. GameManager.Instance.text4.text = value.nowScale.x.ToString() + " " + this.value.name + " " + value.nowPos.x.ToString();
  296. }
  297. }
  298. }