ModelItem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 UnityEngine.UI;
  8. using XRTool.Util;
  9. public enum ModelType
  10. {
  11. Image,
  12. Video,
  13. ABModel,
  14. Text,
  15. scene = -1,
  16. vuforial = -2,
  17. }
  18. public class ModelItem : System.ICloneable
  19. {
  20. public bool autoPlay;
  21. public int updateTime { get; set; }
  22. //WEB端排版
  23. public string template { get; set; }
  24. //模型所在组合素材内
  25. public GameObject modelList;
  26. public GameObject prefabModel;
  27. public GameObject _model;
  28. //模型数据
  29. public GameObject Model
  30. {
  31. get {
  32. if (_model == null)
  33. {
  34. _model = new GameObject(name);
  35. _model.transform.parent = modelList.transform;
  36. // _model.transform.localPosition = getInfoPos();
  37. // _model.transform.localEulerAngles = getInfoEul();
  38. Debug.Log(" ModelName>>>> " + name + " Type " + type);
  39. }
  40. return _model; }
  41. }
  42. public void init()
  43. {
  44. Debug.LogError("ModelItem Init");
  45. if (modelType != ModelType.Text)
  46. LoadManager.Instance.load(this, (float f) =>
  47. {
  48. //显示加载
  49. }, (byte[] obj) =>
  50. {
  51. });
  52. // else
  53. // SetData(null);
  54. }
  55. //模型地址
  56. public string url;
  57. //模型类型
  58. public ModelType modelType;
  59. ////模型原始信息
  60. //public string info;
  61. //模型版本
  62. public string Version { get; set; }
  63. Vector3 getInfoPos()
  64. {
  65. Debug.Log("DGJ2 ====> " + name + "_" + prefabModel.transform.localPosition + "_" + prefabModel.transform.name);
  66. if (prefabModel.transform.localPosition != Vector3.zero)
  67. {
  68. YangShiTextItem ysi = prefabModel.GetComponent<YangShiTextItem>();
  69. if (ysi&&((prefabModel.transform.localPosition.y == ysi.buttomf)|| (prefabModel.transform.localPosition.y == ysi.topf)) && prefabModel.transform.localPosition.x == 0 && prefabModel.transform.localPosition.z == 0)
  70. {
  71. Debug.Log("objectTransform.nowPos===>" + objectTransform.nowPos);
  72. }else
  73. {
  74. return prefabModel.transform.localPosition;
  75. }
  76. }
  77. if (prefabModel == null || objectTransform.nowPos==null)
  78. return Vector3.zero;
  79. Debug.Log("获取modelItem坐标==》"+prefabModel.name+ objectTransform.nowPos);
  80. //return Vector3.zero;
  81. if (objectTransform.nowPos == Vector3.zero&& prefabModel.transform.localPosition!=Vector3.zero)
  82. return objectTransform.startPos;
  83. else
  84. return objectTransform.nowPos;
  85. }
  86. Vector3 getInfoEul()
  87. {
  88. if (prefabModel.transform.localEulerAngles != Vector3.zero)
  89. {
  90. return prefabModel.transform.localEulerAngles;
  91. }
  92. if (prefabModel == null || objectTransform.nowPos == null)
  93. return Vector3.zero;
  94. if (objectTransform.nowRot == Vector3.zero && prefabModel.transform.localEulerAngles != Vector3.zero)
  95. return objectTransform.startRot;
  96. else
  97. return objectTransform.nowRot;
  98. // return Vector3.zero;
  99. }
  100. Vector3 getInfoScale()
  101. {
  102. Debug.Log("DGJ2 ==getInfoScale==> " + name + "_" + prefabModel.transform.localScale + "_" + prefabModel.transform.name);
  103. if (prefabModel.transform.localScale != Vector3.zero&& prefabModel.transform.localScale != Vector3.one)
  104. {
  105. return prefabModel.transform.localScale;
  106. }
  107. if ( objectTransform.nowPos == null)
  108. return Vector3.one;
  109. if (objectTransform.nowScale == Vector3.zero)
  110. return Vector3.one;
  111. else
  112. return objectTransform.nowScale;
  113. }
  114. //模型id
  115. public string uid { get; set; }
  116. //模型name
  117. public string name { get; set; }
  118. /// <summary>
  119. /// 模型类型
  120. /// </summary>
  121. public int type { get; set; }
  122. /// <summary>
  123. /// 文本内容
  124. /// </summary>
  125. public string textContent { get; set; }
  126. /// <summary>
  127. /// 文本框背景
  128. /// </summary>
  129. public string TextBJ { get; set; }
  130. public string textPosition { get; set; }
  131. /// <summary>
  132. /// 下载地址
  133. /// </summary>
  134. public string DownloadPath { get; set; }
  135. /// <summary>
  136. /// 本地保存地址
  137. /// </summary>
  138. public string localSavePath { get; set; }
  139. /// <summary>
  140. /// 坐标
  141. /// </summary>
  142. public ObjectTransform objectTransform { get; set; }
  143. public int fileId { get; set; }
  144. public int id { get; set; }
  145. Timer itemI;
  146. public void init(GameObject modelList)
  147. {
  148. switch (type)
  149. {
  150. case 1: // 图片
  151. modelType = ModelType.Image;
  152. break;
  153. case 2: // 视频
  154. modelType = ModelType.Video;
  155. break;
  156. case 3: // 模型
  157. modelType = ModelType.ABModel;
  158. break;
  159. case 4: // 文字
  160. modelType = ModelType.Text;
  161. break;
  162. default:
  163. Debug.LogError(name + " 素材结构类型异常!!!");
  164. break;
  165. }
  166. if (type == 0)
  167. return;
  168. url = DownloadPath;
  169. Debug.Log(url);
  170. this.modelList = modelList;
  171. if (Version == null)
  172. Version = Application.version.ToString();
  173. init();
  174. // if (Model != null)
  175. // Debug.Log(" 添加模型 " + name);
  176. }
  177. public object Clone()
  178. {
  179. ModelItem clonedObject = (ModelItem)this.MemberwiseClone();
  180. return clonedObject;
  181. }
  182. public byte[] abByte;
  183. public DownLoadMaterial dmdown;
  184. public static bool isLoad;
  185. public static Queue<ModelItem> loadAB = new Queue<ModelItem>();
  186. public void SetData(byte[] data, DownLoadMaterial dm)
  187. {
  188. if(data ==null && modelType!= ModelType.Video)
  189. {
  190. Debug.LogError(name + " 未下载完成 !!!!!");
  191. }
  192. abByte = data;
  193. }
  194. bool isload=false;
  195. public IEnumerator loadmodel()
  196. {
  197. DownLoadItem dloadi = LoadManager.Instance.getdownloaditem(this);
  198. Debug.Log("getdownloaditem=====>" + dloadi+"_"+ isload+"_"+id);
  199. if (dloadi&& !isload)
  200. {
  201. isload = true;
  202. while (dloadi.downLoadData== null)
  203. {
  204. yield return null;
  205. }
  206. abByte = dloadi.downLoadData;
  207. dmdown = dloadi.data;
  208. Debug.Log("HJJTEST ModelType " + modelType);
  209. switch (modelType)
  210. {
  211. case ModelType.Image:
  212. Texture2D texture = new Texture2D(1, 1);
  213. OpenCVForUnity.CoreModule.Mat _mat = null;
  214. GameManager.Instance.AddThead(() =>
  215. {
  216. Debug.Log("加載完成222222222222222222");
  217. _mat = GameManager.zoomByteByOpenCV(abByte);
  218. }, () =>
  219. {
  220. Debug.Log("加載完成111111111111");
  221. texture = GameManager.TextureByMat(_mat);
  222. Debug.Log(" LoadManager " + texture.width + "_" + texture.height);
  223. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
  224. TemplateImage tlimg = prefabModel.GetComponent<TemplateImage>();
  225. Texture2D tex = texture;
  226. if (tex.height > tex.width)
  227. {
  228. float bl = ((float)tex.height / (float)tex.width);
  229. tlimg.transform.Find("Canvas").GetComponent<RectTransform>().sizeDelta = new Vector2(500 / bl, 500);
  230. }
  231. else
  232. {
  233. float bl = ((float)tex.width / (float)tex.height);
  234. tlimg.transform.Find("Canvas").GetComponent<RectTransform>().sizeDelta = new Vector2(500, 500 / bl);
  235. }
  236. tlimg.M_Image.sprite = sprite;
  237. });
  238. break;
  239. case ModelType.Video:
  240. Debug.Log(" DownloadPath video " );
  241. Debug.Log(" DownloadPath video " + prefabModel.name);
  242. Debug.Log(" DownloadPath video " + Application.persistentDataPath + "/Material/" + Path.GetFileName(DownloadPath));
  243. prefabModel.GetComponent<TemplateVideo>().localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(DownloadPath);
  244. prefabModel.GetComponent<TemplateVideo>().autoplay = autoPlay;
  245. // prefabModel.GetComponent<TemplateVideo>().SetData(HttpAction.baseurvideo + DownloadPath);
  246. prefabModel.GetComponent<TemplateVideo>().SetData(Application.persistentDataPath + "/Material/" + Path.GetFileName(DownloadPath));
  247. break;
  248. case ModelType.ABModel:
  249. prefabModel.GetComponent<TemplateModel>().Data = dmdown;
  250. loadAB.Enqueue(this);
  251. /*
  252. AssetBundle assetBundle = AssetBundle.LoadFromMemory(data);
  253. Debug.Log(data.Length);
  254. prefabModel.GetComponent<TemplateModel>().LoadModel(assetBundle);*/
  255. break;
  256. case ModelType.Text:
  257. Debug.Log("HJJTEST ModelType.Text " + DownloadPath);
  258. prefabModel.GetComponent<TemplateText>().SetData(textContent, TextBJ);
  259. break;
  260. default:
  261. break;
  262. }
  263. }
  264. }
  265. public void Clear()
  266. {
  267. switch (modelType)
  268. {
  269. case ModelType.Image:
  270. if(prefabModel)
  271. {
  272. Debug.Log("Clear SpotsList modelitem prefabModel" + prefabModel.name);
  273. TemplateImage tlimg = prefabModel.GetComponent<TemplateImage>();
  274. GameManager.Instance.destroyGo(tlimg.M_Image.sprite.texture);
  275. Debug.Log("Clear SpotsList modelitem prefabModel"+ prefabModel.name);
  276. }
  277. break;
  278. case ModelType.Video:
  279. break;
  280. case ModelType.ABModel:
  281. break;
  282. case ModelType.Text:
  283. break;
  284. default:
  285. break;
  286. }
  287. GameManager.Instance.destroyGo(prefabModel);
  288. prefabModel = null;
  289. isload = false;
  290. Debug.Log("Clear SpotsList modelitem[i]");
  291. }
  292. public void initModelObj()
  293. {
  294. GameManager.Instance.StartCoroutine(loadmodel());
  295. if (getInfoPos() != Vector3.zero)
  296. {
  297. prefabModel.transform.localPosition = getInfoPos();
  298. }
  299. if (getInfoEul() != Vector3.zero)
  300. {
  301. prefabModel.transform.localEulerAngles = getInfoEul();
  302. }
  303. if (getInfoScale() != Vector3.zero)
  304. {
  305. prefabModel.transform.localScale = getInfoScale();
  306. }
  307. // prefabModel.transform.localPosition = getInfoPos();
  308. // prefabModel.transform.localEulerAngles = getInfoEul();
  309. switch (modelType)
  310. {
  311. case ModelType.Text:
  312. prefabModel.GetComponent<TemplateText>().SetData(textContent, TextBJ);
  313. YangShiTextItem ysi = prefabModel.GetComponent<YangShiTextItem>();
  314. float topf = 0.4f;
  315. float buttomf = -0.4f;
  316. if (ysi!=null)
  317. {
  318. topf = ysi.topf;
  319. buttomf = ysi.buttomf;
  320. }
  321. //Set text position
  322. if (textPosition == "top")
  323. {
  324. if (objectTransform.nowPos == Vector3.zero && prefabModel.transform.localPosition!= new Vector3(0, topf, 0))
  325. {
  326. prefabModel.transform.localPosition = new Vector3(0, topf, 0);
  327. }
  328. }
  329. else if (textPosition == "bottom")
  330. {
  331. if (objectTransform.nowPos == Vector3.zero && prefabModel.transform.localPosition != new Vector3(0, buttomf, 0))
  332. {
  333. prefabModel.transform.localPosition = new Vector3(0, buttomf, 0);
  334. }
  335. }
  336. else if (textPosition == "left")
  337. {
  338. if (objectTransform.nowPos == Vector3.zero && prefabModel.transform.localPosition != new Vector3(0, 0.3f, 0))
  339. {
  340. prefabModel.transform.localPosition = new Vector3(-0.3f, 0, 0);
  341. }
  342. }
  343. else if (textPosition == "right")
  344. {
  345. if (objectTransform.nowPos == Vector3.zero && prefabModel.transform.localPosition != new Vector3(0, 0.3f, 0))
  346. {
  347. prefabModel.transform.localPosition = new Vector3(0.3f, 0f, 0);
  348. }
  349. }
  350. Debug.Log(" ddddddddddddddddddddd"+prefabModel.name+"_" + textPosition);
  351. break;
  352. default:
  353. break;
  354. }
  355. }
  356. }