SRSectorLayout.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 SRSectorLayout : 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. for (int i = 0; i < materialObls.materialList.Count; i++)
  25. {
  26. var mat = materialObls.materialList[i];
  27. GameObject go = null;
  28. switch (int.Parse(mat.type))
  29. {
  30. case (int)MaterialType.None:
  31. break;
  32. case (int)MaterialType.Image:
  33. var pImage = ResMgr.Instance.Load<GameObject>("Template/Prefab/Image");
  34. go = Instantiate(pImage, par.transform);
  35. var imageitem = go.AddComponent<TemplateImage>();
  36. go.SetActive(true);
  37. imageitem.SetData(mat, materialObls.updateTime);
  38. noText.Add(imageitem);
  39. break;
  40. case (int)MaterialType.Video:
  41. var pVideo = ResMgr.Instance.Load<GameObject>("Template/Prefab/Video");
  42. go = Instantiate(pVideo, par.transform);
  43. var videoitem = go.AddComponent<TemplateVideo>();
  44. go.SetActive(true);
  45. videoitem.SetData(mat, materialObls.updateTime);
  46. noText.Add(videoitem);
  47. break;
  48. case (int)MaterialType.Text:
  49. textmat = mat;
  50. var pText = ResMgr.Instance.Load<GameObject>("Template/Prefab/Text");
  51. go = Instantiate(pText, par.transform);
  52. var textitem = go.AddComponent<TemplateText>();
  53. go.SetActive(true);
  54. textitem.SetData(mat, materialObls.updateTime);
  55. text = textitem;
  56. go.SetActive(false);
  57. break;
  58. default:
  59. break;
  60. }
  61. if (go != null)
  62. {
  63. go.name = string.IsNullOrWhiteSpace(mat.name) ? par.name + "-" + i.ToString() : mat.name;
  64. go.AddComponent<EditorEventHandler>().Type = (MaterialType)(int.Parse(mat.type));
  65. if (materialObls.select)
  66. {
  67. go.transform.localPosition = mat.ObjectTransform.nowPos;
  68. go.transform.localEulerAngles = mat.ObjectTransform.nowRot;
  69. go.transform.localScale = mat.ObjectTransform.nowScale;
  70. }
  71. else
  72. {
  73. go.transform.localEulerAngles = Vector3.zero;
  74. go.transform.localScale = new Vector3(1, 1, 1);
  75. if (int.Parse(mat.type) != (int)MaterialType.Text)
  76. {
  77. go.transform.localPosition = Vector3.zero;
  78. }
  79. else
  80. {
  81. //Set text position
  82. if (mat.textPosition == "top")
  83. {
  84. go.transform.localPosition = new Vector3(0, 0.3f, 0);
  85. }
  86. else if (mat.textPosition == "bottom")
  87. {
  88. go.transform.localPosition = new Vector3(0, -0.3f, 0);
  89. }
  90. }
  91. mat.ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  92. }
  93. ObjectValue objectValue = new ObjectValue(i, materialObls.id, mat.name, go);
  94. objectValues.Add(objectValue);
  95. }
  96. }
  97. #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回
  98. if (!materialObls.select)
  99. {
  100. var count = noText.Count;
  101. switch (count)
  102. {
  103. case 3:
  104. Column_3(noText, par);
  105. break;
  106. case 4:
  107. Column_4(noText, par);
  108. break;
  109. case 5:
  110. Column_5(noText, par);
  111. break;
  112. default:
  113. break;
  114. }
  115. //第一次排列后,需调整初始坐标和当前坐标保持一致
  116. for (int i = 0; i < objectValues.Count; i++)
  117. {
  118. objectValues[i].InitTransform();
  119. objectValues[i].Object.SetActive(true);
  120. var go = objectValues[i].Object;
  121. newmaterial.materialList[objectValues[i].ID].ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  122. }
  123. }
  124. if (textmat != null)
  125. {
  126. text.gameObject.SetActive(true);
  127. }
  128. #endregion
  129. return objectValues;
  130. }
  131. /// <summary>
  132. /// 根据数量排列图片或文字
  133. /// </summary>
  134. /// <param name="data">图片和视频列表</param>
  135. private static void Column_3(List<BaseTemPlate> data, GameObject game)
  136. {
  137. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  138. scgrid.IsIgnoreInactiveObj = true;
  139. scgrid.LayoutType = LayoutTypes.Vertical;
  140. scgrid.Rows = 1;
  141. scgrid.SpaceX = 0.7f;
  142. scgrid.RefreshInfo();
  143. Destroy(scgrid);
  144. data[0].transform.localPosition += new Vector3(0, 0, -0.18f);
  145. data[0].transform.localRotation = Quaternion.Euler(0, -30, 0);
  146. data[2].transform.localPosition += new Vector3(0, 0, -0.18f);
  147. data[2].transform.localRotation = Quaternion.Euler(0, 30, 0);
  148. }
  149. /// <summary>
  150. /// 根据数量排列图片或文字
  151. /// </summary>
  152. /// <param name="data">图片和视频列表</param>
  153. private static void Column_4(List<BaseTemPlate> data, GameObject game)
  154. {
  155. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  156. scgrid.IsIgnoreInactiveObj = true;
  157. scgrid.LayoutType = LayoutTypes.Vertical;
  158. scgrid.Rows = 1;
  159. scgrid.SpaceX = 0.7f;
  160. scgrid.RefreshInfo();
  161. Destroy(scgrid);
  162. data[0].transform.localPosition += new Vector3(0, 0, -0.35f);
  163. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  164. data[3].transform.localPosition += new Vector3(0, 0, -0.35f);
  165. data[3].transform.localRotation = Quaternion.Euler(0, 45, 0);
  166. }
  167. /// <summary>
  168. /// 根据数量排列图片或文字
  169. /// </summary>
  170. /// <param name="data">图片和视频列表</param>
  171. private static void Column_5(List<BaseTemPlate> data, GameObject game)
  172. {
  173. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  174. scgrid.IsIgnoreInactiveObj = true;
  175. scgrid.LayoutType = LayoutTypes.Vertical;
  176. scgrid.Rows = 1;
  177. scgrid.SpaceX = 0.7f;
  178. scgrid.RefreshInfo();
  179. Destroy(scgrid);
  180. data[0].transform.localPosition += new Vector3(0.15f, 0, -0.65f);
  181. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  182. data[4].transform.localPosition += new Vector3(-0.15f, 0, -0.65f);
  183. data[4].transform.localRotation = Quaternion.Euler(0, 45, 0);
  184. data[1].transform.localPosition += new Vector3(0, 0, -0.22f);
  185. data[1].transform.localRotation = Quaternion.Euler(0, -30, 0);
  186. data[3].transform.localPosition += new Vector3(0, 0, -0.22f);
  187. data[3].transform.localRotation = Quaternion.Euler(0, 30, 0);
  188. }
  189. }