DRTitleLayout.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using SC.XR.Unity;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. /// <summary>
  6. /// 双排平铺
  7. /// </summary>
  8. public class DRTitleLayout : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 根据素材数量对物体进行布局组合
  12. /// 忽略文字类型
  13. /// </summary>
  14. /// <param name="materialObls">素材</param>
  15. /// <param name="par">素材父物体</param>
  16. /// <returns></returns>
  17. public static List<ObjectValue> CalLayout(MaterialObl materialObls, GameObject par, out MaterialObl newmaterial)
  18. {
  19. newmaterial = materialObls;
  20. BaseTemPlate text = new BaseTemPlate();
  21. MaterialObjValue textmat = null;
  22. List<BaseTemPlate> noText = new List<BaseTemPlate>();
  23. List<ObjectValue> objectValues = new List<ObjectValue>();
  24. if (materialObls.materialList != null || materialObls.materialList.Count >0)
  25. for (int i = 0; i < materialObls.materialList.Count; i++)
  26. {
  27. var mat = materialObls.materialList[i];
  28. GameObject go = null;
  29. switch (mat.type)
  30. {
  31. case (int)MaterialType.None:
  32. break;
  33. case (int)MaterialType.Image:
  34. var pImage = ResMgr.Instance.Load<GameObject>("Image");
  35. go = Instantiate(pImage, par.transform);
  36. var imageitem = go.AddComponent<TemplateImage>();
  37. go.SetActive(true);
  38. imageitem.SetData(mat, materialObls.updateTime);
  39. noText.Add(imageitem);
  40. break;
  41. case (int)MaterialType.Video:
  42. var pVideo = ResMgr.Instance.Load<GameObject>("Video");
  43. go = Instantiate(pVideo, par.transform);
  44. var videoitem = go.AddComponent<TemplateVideo>();
  45. videoitem.autoplay = materialObls.autoplay;
  46. go.SetActive(true);
  47. videoitem.SetData(mat, materialObls.updateTime);
  48. noText.Add(videoitem);
  49. break;
  50. case (int)MaterialType.Text:
  51. textmat = mat;
  52. var pText = ResMgr.Instance.Load<GameObject>("Text");
  53. go = Instantiate(pText, par.transform);
  54. var textitem = go.AddComponent<TemplateText>();
  55. go.SetActive(true);
  56. textitem.SetData(mat, materialObls.updateTime);
  57. text = textitem;
  58. go.SetActive(false);
  59. break;
  60. default:
  61. break;
  62. }
  63. if (go != null)
  64. {
  65. go.name = string.IsNullOrWhiteSpace(mat.name) ? par.name + "-" + i.ToString() : mat.name;
  66. go.AddComponent<EditorEventHandler>().Type = (MaterialType)(mat.type);
  67. if (materialObls.select)
  68. {
  69. go.transform.localPosition = mat.ObjectTransform.nowPos;
  70. go.transform.localEulerAngles = mat.ObjectTransform.nowRot;
  71. go.transform.localScale = mat.ObjectTransform.nowScale;
  72. }
  73. else
  74. {
  75. go.transform.localEulerAngles = Vector3.zero;
  76. go.transform.localScale = new Vector3(1, 1, 1);
  77. if (mat.type != (int)MaterialType.Text)
  78. {
  79. go.transform.localPosition = Vector3.zero;
  80. }
  81. else
  82. {
  83. //Set text position
  84. if (mat.textPosition == "top")
  85. {
  86. go.transform.localPosition = new Vector3(0, 1f, 0);
  87. }
  88. else if (mat.textPosition == "bottom")
  89. {
  90. go.transform.localPosition = new Vector3(0, -1f, 0);
  91. }
  92. }
  93. mat.ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  94. }
  95. ObjectValue objectValue = new ObjectValue(i, materialObls.id, mat.name, go);
  96. objectValues.Add(objectValue);
  97. }
  98. }
  99. #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回
  100. if (!materialObls.select)
  101. {
  102. var count = noText.Count;
  103. switch (count)
  104. {
  105. case 2:
  106. Column_2(noText, 2, par);
  107. break;
  108. case 3:
  109. Column_3(noText);
  110. break;
  111. case 4:
  112. Column_2(noText, 4, par);
  113. break;
  114. case 5:
  115. Column_5(noText, par);
  116. break;
  117. case 6:
  118. Column_2(noText, 6, par);
  119. break;
  120. default:
  121. break;
  122. }
  123. //第一次排列后,需调整初始坐标和当前坐标保持一致
  124. for (int i = 0; i < objectValues.Count; i++)
  125. {
  126. objectValues[i].InitTransform();
  127. objectValues[i].Object.SetActive(true);
  128. var go = objectValues[i].Object;
  129. newmaterial.materialList[objectValues[i].ID].ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  130. }
  131. }
  132. if (textmat != null)
  133. {
  134. text.gameObject.SetActive(true);
  135. }
  136. #endregion
  137. return objectValues;
  138. }
  139. public static IEnumerator CalLayout(ModelList materialObls, GameObject par)
  140. {
  141. BaseTemPlate text = new BaseTemPlate();
  142. ModelItem textmat = null;
  143. List<BaseTemPlate> noText = new List<BaseTemPlate>();
  144. List<ObjectValue> objectValues = new List<ObjectValue>();
  145. if (materialObls.materialList != null || materialObls.materialList.Count > 0)
  146. for (int i = 0; i < materialObls.materialList.Count; i++)
  147. {
  148. var mat = materialObls.materialList[i];
  149. GameObject go = null;
  150. switch (mat.type)
  151. {
  152. case (int)MaterialType.None:
  153. break;
  154. case (int)MaterialType.Image:
  155. var pImage = ResMgr.Instance.LoadAsync<GameObject>("Template/Prefab/Image");
  156. yield return pImage;
  157. go = Instantiate(pImage.asset as GameObject, par.transform);
  158. var imageitem = go.AddComponent<TemplateImage>();
  159. go.SetActive(true);
  160. // imageitem.SetData(mat, materialObls.updateTime);
  161. noText.Add(imageitem);
  162. break;
  163. case (int)MaterialType.Video:
  164. var pVideo = ResMgr.Instance.LoadAsync<GameObject>("Template/Prefab/Video");
  165. yield return pVideo;
  166. go = Instantiate(pVideo.asset as GameObject, par.transform);
  167. var videoitem = go.AddComponent<TemplateVideo>();
  168. videoitem.autoplay = materialObls.autoplay;
  169. go.SetActive(true);
  170. // videoitem.SetData(mat, materialObls.updateTime);
  171. noText.Add(videoitem);
  172. break;
  173. case (int)MaterialType.Text:
  174. textmat = mat;
  175. var pText = ResMgr.Instance.LoadAsync<GameObject>("Template/Prefab/Text");
  176. yield return pText;
  177. go = Instantiate(pText.asset as GameObject, par.transform);
  178. var textitem = go.AddComponent<TemplateText>();
  179. go.SetActive(true);
  180. // textitem.SetData(mat, materialObls.updateTime);
  181. text = textitem;
  182. go.SetActive(false);
  183. break;
  184. default:
  185. break;
  186. }
  187. if (go != null)
  188. {
  189. go.name = string.IsNullOrWhiteSpace(mat.name) ? par.name + "-" + i.ToString() : mat.name;
  190. go.AddComponent<EditorEventHandler>().Type = (MaterialType)(mat.type);
  191. if (materialObls.select)
  192. {
  193. go.transform.localPosition = mat.objectTransform.nowPos;
  194. go.transform.localEulerAngles = mat.objectTransform.nowRot;
  195. go.transform.localScale = mat.objectTransform.nowScale;
  196. }
  197. else
  198. {
  199. go.transform.localEulerAngles = Vector3.zero;
  200. go.transform.localScale = new Vector3(1, 1, 1);
  201. if (mat.type != (int)MaterialType.Text)
  202. {
  203. go.transform.localPosition = Vector3.zero;
  204. }
  205. else
  206. {
  207. //Set text position
  208. if (mat.textPosition == "top")
  209. {
  210. go.transform.localPosition = new Vector3(0, 0.3f, 0);
  211. }
  212. else if (mat.textPosition == "bottom")
  213. {
  214. go.transform.localPosition = new Vector3(0, -0.3f, 0);
  215. }
  216. }
  217. mat.objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  218. }
  219. ObjectValue objectValue = new ObjectValue(i, materialObls.id, mat.name, go);
  220. objectValues.Add(objectValue);
  221. mat.prefabModel = go;
  222. // mat.initFrist();
  223. }
  224. }
  225. #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回
  226. if (!materialObls.select)
  227. {
  228. var count = noText.Count;
  229. switch (count)
  230. {
  231. case 2:
  232. Column_2(noText, 2, par);
  233. break;
  234. case 3:
  235. Column_3(noText);
  236. break;
  237. case 4:
  238. Column_2(noText, 4, par);
  239. break;
  240. case 5:
  241. Column_5(noText, par);
  242. break;
  243. case 6:
  244. Column_2(noText, 6, par);
  245. break;
  246. default:
  247. break;
  248. }
  249. //第一次排列后,需调整初始坐标和当前坐标保持一致
  250. for (int i = 0; i < objectValues.Count; i++)
  251. {
  252. objectValues[i].InitTransform();
  253. objectValues[i].Object.SetActive(true);
  254. var go = objectValues[i].Object;
  255. materialObls.materialList[objectValues[i].ID].objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  256. }
  257. }
  258. if (textmat != null)
  259. {
  260. text.gameObject.SetActive(true);
  261. }
  262. #endregion
  263. }
  264. private static void Column_2(List<BaseTemPlate> data, int colum, GameObject game)
  265. {
  266. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  267. scgrid.IsIgnoreInactiveObj = true;
  268. scgrid.LayoutType = LayoutTypes.Horizontal;
  269. scgrid.Columns = colum / 2;
  270. scgrid.SpaceX = 0.7f;
  271. scgrid.SpaceY = 0.7f;
  272. scgrid.RefreshInfo();
  273. Destroy(scgrid);
  274. }
  275. private static void Column_3(List<BaseTemPlate> data)
  276. {
  277. data[0].transform.localPosition = new Vector3(0, 0.24f, 0f);
  278. data[1].transform.localPosition = new Vector3(-0.333f, -0.331f, 0f);
  279. data[2].transform.localPosition = new Vector3(0.333f, -0.331f, 0f);
  280. }
  281. private static void Column_5(List<BaseTemPlate> data, GameObject game)
  282. {
  283. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  284. scgrid.IsIgnoreInactiveObj = true;
  285. scgrid.LayoutType = LayoutTypes.Horizontal;
  286. scgrid.Columns = 3;
  287. scgrid.SpaceX = 0.7f;
  288. scgrid.SpaceY = 0.7f;
  289. scgrid.RefreshInfo();
  290. Destroy(scgrid);
  291. data[3].transform.localPosition += new Vector3(0.357f, 0, 0f);
  292. data[4].transform.localPosition += new Vector3(0.357f, 0, 0f);
  293. }
  294. }