ModelItem.cs 13 KB

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