SRSectorLayout.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. public static void CalLayout(ModelList materialObls, GameObject par)
  135. {
  136. BaseTemPlate text = new BaseTemPlate();
  137. ModelItem textmat = null;
  138. List<BaseTemPlate> noText = new List<BaseTemPlate>();
  139. List<ObjectValue> objectValues = new List<ObjectValue>();
  140. for (int i = 0; i < materialObls.materialList.Count; i++)
  141. {
  142. var mat = materialObls.materialList[i];
  143. GameObject go = null;
  144. switch (mat.type)
  145. {
  146. case (int)MaterialType.None:
  147. break;
  148. case (int)MaterialType.Image:
  149. var pImage = ResMgr.Instance.Load<GameObject>("Template/Prefab/Image");
  150. go = Instantiate(pImage, par.transform);
  151. var imageitem = go.AddComponent<TemplateImage>();
  152. go.SetActive(true);
  153. // imageitem.SetData(mat, materialObls.updateTime);
  154. noText.Add(imageitem);
  155. break;
  156. case (int)MaterialType.Video:
  157. var pVideo = ResMgr.Instance.Load<GameObject>("Template/Prefab/Video");
  158. go = Instantiate(pVideo, par.transform);
  159. var videoitem = go.AddComponent<TemplateVideo>();
  160. go.SetActive(true);
  161. //videoitem.SetData(mat, materialObls.updateTime);
  162. noText.Add(videoitem);
  163. break;
  164. case (int)MaterialType.Text:
  165. textmat = mat;
  166. var pText = ResMgr.Instance.Load<GameObject>("Template/Prefab/Text");
  167. go = Instantiate(pText, par.transform);
  168. var textitem = go.AddComponent<TemplateText>();
  169. go.SetActive(true);
  170. // textitem.SetData(mat, materialObls.updateTime);
  171. text = textitem;
  172. go.SetActive(false);
  173. break;
  174. default:
  175. break;
  176. }
  177. if (go != null)
  178. {
  179. go.name = string.IsNullOrWhiteSpace(mat.name) ? par.name + "-" + i.ToString() : mat.name;
  180. go.AddComponent<EditorEventHandler>().Type = (MaterialType)(mat.type);
  181. if (materialObls.select)
  182. {
  183. go.transform.localPosition = mat.objectTransform.nowPos;
  184. go.transform.localEulerAngles = mat.objectTransform.nowRot;
  185. go.transform.localScale = mat.objectTransform.nowScale;
  186. }
  187. else
  188. {
  189. go.transform.localEulerAngles = Vector3.zero;
  190. go.transform.localScale = new Vector3(1, 1, 1);
  191. if (mat.type != (int)MaterialType.Text)
  192. {
  193. go.transform.localPosition = Vector3.zero;
  194. }
  195. else
  196. {
  197. //Set text position
  198. if (mat.textPosition == "top")
  199. {
  200. go.transform.localPosition = new Vector3(0, 0.3f, 0);
  201. }
  202. else if (mat.textPosition == "bottom")
  203. {
  204. go.transform.localPosition = new Vector3(0, -0.3f, 0);
  205. }
  206. }
  207. mat.objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  208. }
  209. ObjectValue objectValue = new ObjectValue(i, materialObls.id, mat.name, go);
  210. objectValues.Add(objectValue);
  211. mat.prefabModel = go;
  212. }
  213. }
  214. #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回
  215. if (!materialObls.select)
  216. {
  217. var count = noText.Count;
  218. switch (count)
  219. {
  220. case 3:
  221. Column_3(noText, par);
  222. break;
  223. case 4:
  224. Column_4(noText, par);
  225. break;
  226. case 5:
  227. Column_5(noText, par);
  228. break;
  229. default:
  230. break;
  231. }
  232. //第一次排列后,需调整初始坐标和当前坐标保持一致
  233. for (int i = 0; i < objectValues.Count; i++)
  234. {
  235. objectValues[i].InitTransform();
  236. objectValues[i].Object.SetActive(true);
  237. var go = objectValues[i].Object;
  238. materialObls.materialList[objectValues[i].ID].objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  239. }
  240. }
  241. if (textmat != null)
  242. {
  243. text.gameObject.SetActive(true);
  244. }
  245. #endregion
  246. }
  247. /// <summary>
  248. /// 根据数量排列图片或文字
  249. /// </summary>
  250. /// <param name="data">图片和视频列表</param>
  251. private static void Column_3(List<BaseTemPlate> data, GameObject game)
  252. {
  253. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  254. scgrid.IsIgnoreInactiveObj = true;
  255. scgrid.LayoutType = LayoutTypes.Vertical;
  256. scgrid.Rows = 1;
  257. scgrid.SpaceX = 0.7f;
  258. scgrid.RefreshInfo();
  259. Destroy(scgrid);
  260. data[0].transform.localPosition += new Vector3(0, 0, -0.18f);
  261. data[0].transform.localRotation = Quaternion.Euler(0, -30, 0);
  262. data[2].transform.localPosition += new Vector3(0, 0, -0.18f);
  263. data[2].transform.localRotation = Quaternion.Euler(0, 30, 0);
  264. }
  265. /// <summary>
  266. /// 根据数量排列图片或文字
  267. /// </summary>
  268. /// <param name="data">图片和视频列表</param>
  269. private static void Column_4(List<BaseTemPlate> data, GameObject game)
  270. {
  271. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  272. scgrid.IsIgnoreInactiveObj = true;
  273. scgrid.LayoutType = LayoutTypes.Vertical;
  274. scgrid.Rows = 1;
  275. scgrid.SpaceX = 0.7f;
  276. scgrid.RefreshInfo();
  277. Destroy(scgrid);
  278. data[0].transform.localPosition += new Vector3(0, 0, -0.35f);
  279. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  280. data[3].transform.localPosition += new Vector3(0, 0, -0.35f);
  281. data[3].transform.localRotation = Quaternion.Euler(0, 45, 0);
  282. }
  283. /// <summary>
  284. /// 根据数量排列图片或文字
  285. /// </summary>
  286. /// <param name="data">图片和视频列表</param>
  287. private static void Column_5(List<BaseTemPlate> data, GameObject game)
  288. {
  289. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  290. scgrid.IsIgnoreInactiveObj = true;
  291. scgrid.LayoutType = LayoutTypes.Vertical;
  292. scgrid.Rows = 1;
  293. scgrid.SpaceX = 0.7f;
  294. scgrid.RefreshInfo();
  295. Destroy(scgrid);
  296. data[0].transform.localPosition += new Vector3(0.15f, 0, -0.65f);
  297. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  298. data[4].transform.localPosition += new Vector3(-0.15f, 0, -0.65f);
  299. data[4].transform.localRotation = Quaternion.Euler(0, 45, 0);
  300. data[1].transform.localPosition += new Vector3(0, 0, -0.22f);
  301. data[1].transform.localRotation = Quaternion.Euler(0, -30, 0);
  302. data[3].transform.localPosition += new Vector3(0, 0, -0.22f);
  303. data[3].transform.localRotation = Quaternion.Euler(0, 30, 0);
  304. }
  305. /// <summary>
  306. /// 根据数量排列图片或文字
  307. /// </summary>
  308. /// <param name="data">图片和视频列表</param>
  309. private static void Column_6(List<BaseTemPlate> data, GameObject game)
  310. {
  311. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  312. scgrid.IsIgnoreInactiveObj = true;
  313. scgrid.LayoutType = LayoutTypes.Vertical;
  314. scgrid.Rows = 1;
  315. scgrid.SpaceX = 0.7f;
  316. scgrid.RefreshInfo();
  317. Destroy(scgrid);
  318. data[0].transform.localPosition += new Vector3(0.15f, 0, -0.65f);
  319. data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
  320. data[5].transform.localPosition += new Vector3(-0.15f, 0, -0.65f);
  321. data[5].transform.localRotation = Quaternion.Euler(0, 45, 0);
  322. data[1].transform.localPosition += new Vector3(0, 0, -0.22f);
  323. data[1].transform.localRotation = Quaternion.Euler(0, -30, 0);
  324. data[4].transform.localPosition += new Vector3(0, 0, -0.22f);
  325. data[4].transform.localRotation = Quaternion.Euler(0, 30, 0);
  326. }
  327. }