DRTitleLayout.cs 12 KB

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