DRTitleLayout.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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, 0.3f, 0);
  86. }
  87. else if (mat.textPosition == "bottom")
  88. {
  89. go.transform.localPosition = new Vector3(0, -0.3f, 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. private static void Column_2(List<BaseTemPlate> data, int colum, GameObject game)
  139. {
  140. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  141. scgrid.IsIgnoreInactiveObj = true;
  142. scgrid.LayoutType = LayoutTypes.Horizontal;
  143. scgrid.Columns = colum / 2;
  144. scgrid.SpaceX = 0.7f;
  145. scgrid.SpaceY = 0.7f;
  146. scgrid.RefreshInfo();
  147. Destroy(scgrid);
  148. }
  149. private static void Column_3(List<BaseTemPlate> data)
  150. {
  151. data[0].transform.localPosition = new Vector3(0, 0.24f, 0f);
  152. data[1].transform.localPosition = new Vector3(-0.333f, -0.331f, 0f);
  153. data[2].transform.localPosition = new Vector3(0.333f, -0.331f, 0f);
  154. }
  155. private static void Column_5(List<BaseTemPlate> data, GameObject game)
  156. {
  157. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  158. scgrid.IsIgnoreInactiveObj = true;
  159. scgrid.LayoutType = LayoutTypes.Horizontal;
  160. scgrid.Columns = 3;
  161. scgrid.SpaceX = 0.7f;
  162. scgrid.SpaceY = 0.7f;
  163. scgrid.RefreshInfo();
  164. Destroy(scgrid);
  165. data[3].transform.localPosition += new Vector3(0.357f, 0, 0f);
  166. data[4].transform.localPosition += new Vector3(0.357f, 0, 0f);
  167. }
  168. }