SRSectorLayout.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 (mat.type)
  29. {
  30. case (int)MaterialType.None:
  31. break;
  32. case (int)MaterialType.Image:
  33. var pImage = ResMgr.Instance.Load<GameObject>("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>("Video");
  42. go = Instantiate(pVideo, par.transform);
  43. var videoitem = go.AddComponent<TemplateVideo>();
  44. videoitem.autoplay = materialObls.autoplay;
  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>("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)(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 (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.6f, 0);
  86. }
  87. else if (mat.textPosition == "bottom")
  88. {
  89. go.transform.localPosition = new Vector3(0, -0.6f, 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 3:
  105. Column_3(noText, par);
  106. break;
  107. case 4:
  108. Column_4(noText, par);
  109. break;
  110. case 5:
  111. Column_5(noText, par);
  112. break;
  113. case 6:
  114. Column_6(noText, par);
  115. break;
  116. default:
  117. break;
  118. }
  119. //第一次排列后,需调整初始坐标和当前坐标保持一致
  120. for (int i = 0; i < objectValues.Count; i++)
  121. {
  122. objectValues[i].InitTransform();
  123. objectValues[i].Object.SetActive(true);
  124. var go = objectValues[i].Object;
  125. newmaterial.materialList[objectValues[i].ID].ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  126. }
  127. }
  128. if (textmat != null)
  129. {
  130. text.gameObject.SetActive(true);
  131. }
  132. #endregion
  133. return objectValues;
  134. }
  135. public static IEnumerator CalLayout(ModelList materialObls, GameObject par)
  136. {
  137. BaseTemPlate text = new BaseTemPlate();
  138. ModelItem textmat = null;
  139. List<BaseTemPlate> noText = new List<BaseTemPlate>();
  140. List<ObjectValue> objectValues = new List<ObjectValue>();
  141. for (int i = 0; i < materialObls.materialList.Count; i++)
  142. {
  143. var mat = materialObls.materialList[i];
  144. GameObject go = null;
  145. switch (mat.type)
  146. {
  147. case (int)MaterialType.None:
  148. break;
  149. case (int)MaterialType.Image:
  150. var pImage = ResMgr.Instance.LoadAsync<GameObject>("Template/Prefab/Image");
  151. yield return pImage;
  152. go = Instantiate(pImage.asset as GameObject, par.transform);
  153. var imageitem = go.AddComponent<TemplateImage>();
  154. go.SetActive(true);
  155. // imageitem.SetData(mat, materialObls.updateTime);
  156. noText.Add(imageitem);
  157. break;
  158. case (int)MaterialType.Video:
  159. var pVideo = ResMgr.Instance.LoadAsync<GameObject>("Template/Prefab/Video");
  160. yield return pVideo;
  161. go = Instantiate(pVideo.asset as GameObject, par.transform);
  162. var videoitem = go.AddComponent<TemplateVideo>();
  163. videoitem.autoplay = materialObls.autoplay;
  164. go.SetActive(true);
  165. //videoitem.SetData(mat, materialObls.updateTime);
  166. noText.Add(videoitem);
  167. break;
  168. case (int)MaterialType.Text:
  169. textmat = mat;
  170. var pText = ResMgr.Instance.LoadAsync<GameObject>("Template/Prefab/Text");
  171. yield return pText;
  172. go = Instantiate(pText.asset as GameObject, 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. // mat.initFrist();
  218. }
  219. }
  220. #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回
  221. if (!materialObls.select)
  222. {
  223. var count = noText.Count;
  224. switch (count)
  225. {
  226. case 3:
  227. Column_3(noText, par);
  228. break;
  229. case 4:
  230. Column_4(noText, par);
  231. break;
  232. case 5:
  233. Column_5(noText, par);
  234. break;
  235. default:
  236. break;
  237. }
  238. //第一次排列后,需调整初始坐标和当前坐标保持一致
  239. for (int i = 0; i < objectValues.Count; i++)
  240. {
  241. objectValues[i].InitTransform();
  242. objectValues[i].Object.SetActive(true);
  243. var go = objectValues[i].Object;
  244. materialObls.materialList[objectValues[i].ID].objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  245. }
  246. }
  247. if (textmat != null)
  248. {
  249. text.gameObject.SetActive(true);
  250. }
  251. #endregion
  252. }
  253. /// <summary>
  254. /// 根据数量排列图片或文字
  255. /// </summary>
  256. /// <param name="data">图片和视频列表</param>
  257. private static void Column_3(List<BaseTemPlate> data, GameObject game)
  258. {
  259. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  260. scgrid.IsIgnoreInactiveObj = true;
  261. scgrid.LayoutType = LayoutTypes.Vertical;
  262. scgrid.Rows = 1;
  263. scgrid.SpaceX = 0.7f;
  264. scgrid.RefreshInfo();
  265. Destroy(scgrid);
  266. data[0].transform.localPosition += new Vector3(0, 0, -0.18f);
  267. data[0].transform.localRotation = Quaternion.Euler(0, -30, 0);
  268. data[2].transform.localPosition += new Vector3(0, 0, -0.18f);
  269. data[2].transform.localRotation = Quaternion.Euler(0, 30, 0);
  270. }
  271. /// <summary>
  272. /// 根据数量排列图片或文字
  273. /// </summary>
  274. /// <param name="data">图片和视频列表</param>
  275. private static void Column_4(List<BaseTemPlate> data, GameObject game)
  276. {
  277. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  278. scgrid.IsIgnoreInactiveObj = true;
  279. scgrid.LayoutType = LayoutTypes.Vertical;
  280. scgrid.Rows = 1;
  281. scgrid.SpaceX = 0.7f;
  282. scgrid.RefreshInfo();
  283. Destroy(scgrid);
  284. data[0].transform.localPosition += new Vector3(0, 0, -0.35f);
  285. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  286. data[3].transform.localPosition += new Vector3(0, 0, -0.35f);
  287. data[3].transform.localRotation = Quaternion.Euler(0, 45, 0);
  288. }
  289. /// <summary>
  290. /// 根据数量排列图片或文字
  291. /// </summary>
  292. /// <param name="data">图片和视频列表</param>
  293. private static void Column_5(List<BaseTemPlate> data, GameObject game)
  294. {
  295. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  296. scgrid.IsIgnoreInactiveObj = true;
  297. scgrid.LayoutType = LayoutTypes.Vertical;
  298. scgrid.Rows = 1;
  299. scgrid.SpaceX = 0.7f;
  300. scgrid.RefreshInfo();
  301. Destroy(scgrid);
  302. data[0].transform.localPosition += new Vector3(0.15f, 0, -0.65f);
  303. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  304. data[4].transform.localPosition += new Vector3(-0.15f, 0, -0.65f);
  305. data[4].transform.localRotation = Quaternion.Euler(0, 45, 0);
  306. data[1].transform.localPosition += new Vector3(0, 0, -0.22f);
  307. data[1].transform.localRotation = Quaternion.Euler(0, -30, 0);
  308. data[3].transform.localPosition += new Vector3(0, 0, -0.22f);
  309. data[3].transform.localRotation = Quaternion.Euler(0, 30, 0);
  310. }
  311. /// <summary>
  312. /// 根据数量排列图片或文字
  313. /// </summary>
  314. /// <param name="data">图片和视频列表</param>
  315. private static void Column_6(List<BaseTemPlate> data, GameObject game)
  316. {
  317. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  318. scgrid.IsIgnoreInactiveObj = true;
  319. scgrid.LayoutType = LayoutTypes.Vertical;
  320. scgrid.Rows = 1;
  321. scgrid.SpaceX = 0.7f;
  322. scgrid.RefreshInfo();
  323. Destroy(scgrid);
  324. data[0].transform.localPosition += new Vector3(0.15f, 0, -0.65f);
  325. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  326. data[5].transform.localPosition += new Vector3(-0.15f, 0, -0.65f);
  327. data[5].transform.localRotation = Quaternion.Euler(0, 45, 0);
  328. data[1].transform.localPosition += new Vector3(0, 0, -0.22f);
  329. data[1].transform.localRotation = Quaternion.Euler(0, -30, 0);
  330. data[4].transform.localPosition += new Vector3(0, 0, -0.22f);
  331. data[4].transform.localRotation = Quaternion.Euler(0, 30, 0);
  332. }
  333. }