SRSectorLayout.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.6f, 0);
  85. }
  86. else if (mat.textPosition == "bottom")
  87. {
  88. go.transform.localPosition = new Vector3(0, -0.6f, 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. case 6:
  113. Column_6(noText, par);
  114. break;
  115. default:
  116. break;
  117. }
  118. //第一次排列后,需调整初始坐标和当前坐标保持一致
  119. for (int i = 0; i < objectValues.Count; i++)
  120. {
  121. objectValues[i].InitTransform();
  122. objectValues[i].Object.SetActive(true);
  123. var go = objectValues[i].Object;
  124. newmaterial.materialList[objectValues[i].ID].ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  125. }
  126. }
  127. if (textmat != null)
  128. {
  129. text.gameObject.SetActive(true);
  130. }
  131. #endregion
  132. return objectValues;
  133. }
  134. /// <summary>
  135. /// 根据数量排列图片或文字
  136. /// </summary>
  137. /// <param name="data">图片和视频列表</param>
  138. private static void Column_3(List<BaseTemPlate> data, GameObject game)
  139. {
  140. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  141. scgrid.IsIgnoreInactiveObj = true;
  142. scgrid.LayoutType = LayoutTypes.Vertical;
  143. scgrid.Rows = 1;
  144. scgrid.SpaceX = 0.7f;
  145. scgrid.RefreshInfo();
  146. Destroy(scgrid);
  147. data[0].transform.localPosition += new Vector3(0, 0, -0.18f);
  148. data[0].transform.localRotation = Quaternion.Euler(0, -30, 0);
  149. data[2].transform.localPosition += new Vector3(0, 0, -0.18f);
  150. data[2].transform.localRotation = Quaternion.Euler(0, 30, 0);
  151. }
  152. /// <summary>
  153. /// 根据数量排列图片或文字
  154. /// </summary>
  155. /// <param name="data">图片和视频列表</param>
  156. private static void Column_4(List<BaseTemPlate> data, GameObject game)
  157. {
  158. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  159. scgrid.IsIgnoreInactiveObj = true;
  160. scgrid.LayoutType = LayoutTypes.Vertical;
  161. scgrid.Rows = 1;
  162. scgrid.SpaceX = 0.7f;
  163. scgrid.RefreshInfo();
  164. Destroy(scgrid);
  165. data[0].transform.localPosition += new Vector3(0, 0, -0.35f);
  166. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  167. data[3].transform.localPosition += new Vector3(0, 0, -0.35f);
  168. data[3].transform.localRotation = Quaternion.Euler(0, 45, 0);
  169. }
  170. /// <summary>
  171. /// 根据数量排列图片或文字
  172. /// </summary>
  173. /// <param name="data">图片和视频列表</param>
  174. private static void Column_5(List<BaseTemPlate> data, GameObject game)
  175. {
  176. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  177. scgrid.IsIgnoreInactiveObj = true;
  178. scgrid.LayoutType = LayoutTypes.Vertical;
  179. scgrid.Rows = 1;
  180. scgrid.SpaceX = 0.7f;
  181. scgrid.RefreshInfo();
  182. Destroy(scgrid);
  183. data[0].transform.localPosition += new Vector3(0.15f, 0, -0.65f);
  184. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  185. data[4].transform.localPosition += new Vector3(-0.15f, 0, -0.65f);
  186. data[4].transform.localRotation = Quaternion.Euler(0, 45, 0);
  187. data[1].transform.localPosition += new Vector3(0, 0, -0.22f);
  188. data[1].transform.localRotation = Quaternion.Euler(0, -30, 0);
  189. data[3].transform.localPosition += new Vector3(0, 0, -0.22f);
  190. data[3].transform.localRotation = Quaternion.Euler(0, 30, 0);
  191. }
  192. /// <summary>
  193. /// 根据数量排列图片或文字
  194. /// </summary>
  195. /// <param name="data">图片和视频列表</param>
  196. private static void Column_6(List<BaseTemPlate> data, GameObject game)
  197. {
  198. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  199. scgrid.IsIgnoreInactiveObj = true;
  200. scgrid.LayoutType = LayoutTypes.Vertical;
  201. scgrid.Rows = 1;
  202. scgrid.SpaceX = 0.7f;
  203. scgrid.RefreshInfo();
  204. Destroy(scgrid);
  205. data[0].transform.localPosition += new Vector3(0.15f, 0, -0.65f);
  206. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  207. data[5].transform.localPosition += new Vector3(-0.15f, 0, -0.65f);
  208. data[5].transform.localRotation = Quaternion.Euler(0, 45, 0);
  209. data[1].transform.localPosition += new Vector3(0, 0, -0.22f);
  210. data[1].transform.localRotation = Quaternion.Euler(0, -30, 0);
  211. data[4].transform.localPosition += new Vector3(0, 0, -0.22f);
  212. data[4].transform.localRotation = Quaternion.Euler(0, 30, 0);
  213. }
  214. }