DRFanLayout.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using SC.XR.Unity;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. /// <summary>
  7. /// 双排扇形
  8. /// </summary>
  9. public class DRFanLayout : MonoBehaviour
  10. {
  11. public static List<ObjectValue> CalLayout(MaterialObl materialObls, GameObject par, out MaterialObl newmaterial)
  12. {
  13. newmaterial = materialObls;
  14. BaseTemPlate text = new BaseTemPlate();
  15. MaterialObjValue textmat = null;
  16. List<BaseTemPlate> noText = new List<BaseTemPlate>();
  17. List<ObjectValue> objectValues = new List<ObjectValue>();
  18. for (int i = 0; i < materialObls.materialList.Count; i++)
  19. {
  20. var mat = materialObls.materialList[i];
  21. GameObject go = null;
  22. switch (int.Parse(mat.type))
  23. {
  24. case (int)MaterialType.None:
  25. break;
  26. case (int)MaterialType.Image:
  27. var pImage = ResMgr.Instance.Load<GameObject>("Template/Prefab/Image");
  28. go = Instantiate(pImage, par.transform);
  29. var imageitem = go.AddComponent<TemplateImage>();
  30. go.SetActive(true);
  31. imageitem.SetData(mat, materialObls.updateTime);
  32. noText.Add(imageitem);
  33. break;
  34. case (int)MaterialType.Video:
  35. var pVideo = ResMgr.Instance.Load<GameObject>("Template/Prefab/Video");
  36. go = Instantiate(pVideo, par.transform);
  37. var videoitem = go.AddComponent<TemplateVideo>();
  38. go.SetActive(true);
  39. videoitem.SetData(mat, materialObls.updateTime);
  40. noText.Add(videoitem);
  41. break;
  42. case (int)MaterialType.Text:
  43. textmat = mat;
  44. var pText = ResMgr.Instance.Load<GameObject>("Template/Prefab/Text");
  45. go = Instantiate(pText, par.transform);
  46. var textitem = go.AddComponent<TemplateText>();
  47. go.SetActive(true);
  48. textitem.SetData(mat, materialObls.updateTime);
  49. text = textitem;
  50. go.SetActive(false);
  51. break;
  52. default:
  53. break;
  54. }
  55. if (go != null)
  56. {
  57. go.name = string.IsNullOrWhiteSpace(mat.name) ? par.name + "-" + i.ToString() : mat.name;
  58. go.AddComponent<EditorEventHandler>().Type = (MaterialType)(int.Parse(mat.type));
  59. if (materialObls.select)
  60. {
  61. go.transform.localPosition = mat.ObjectTransform.nowPos;
  62. go.transform.localEulerAngles = mat.ObjectTransform.nowRot;
  63. go.transform.localScale = mat.ObjectTransform.nowScale;
  64. }
  65. else
  66. {
  67. go.transform.localEulerAngles = Vector3.zero;
  68. go.transform.localScale = new Vector3(1, 1, 1);
  69. if (int.Parse(mat.type) != (int)MaterialType.Text)
  70. {
  71. go.transform.localPosition = Vector3.zero;
  72. }
  73. else
  74. {
  75. //Set text position
  76. if (mat.textPosition == "top")
  77. {
  78. go.transform.localPosition = new Vector3(0, 1f, 0);
  79. }
  80. else if (mat.textPosition == "bottom")
  81. {
  82. go.transform.localPosition = new Vector3(0, -1f, 0);
  83. }
  84. }
  85. mat.ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  86. }
  87. ObjectValue objectValue = new ObjectValue(i, materialObls.id, mat.name, go);
  88. objectValues.Add(objectValue);
  89. }
  90. }
  91. #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回
  92. if (!materialObls.select)
  93. {
  94. Column(noText, par);
  95. //第一次排列后,需调整初始坐标和当前坐标保持一致
  96. for (int i = 0; i < objectValues.Count; i++)
  97. {
  98. objectValues[i].InitTransform();
  99. objectValues[i].Object.SetActive(true);
  100. var go = objectValues[i].Object;
  101. newmaterial.materialList[objectValues[i].ID].ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  102. }
  103. }
  104. if (textmat != null)
  105. {
  106. text.gameObject.SetActive(true);
  107. }
  108. #endregion
  109. return objectValues;
  110. }
  111. public static void CalLayout(ModelList materialObls, GameObject par)
  112. {
  113. BaseTemPlate text = new BaseTemPlate();
  114. ModelItem textmat = null;
  115. List<BaseTemPlate> noText = new List<BaseTemPlate>();
  116. List<ObjectValue> objectValues = new List<ObjectValue>();
  117. for (int i = 0; i < materialObls.materialList.Count; i++)
  118. {
  119. var mat = materialObls.materialList[i];
  120. GameObject go = null;
  121. switch (mat.type)
  122. {
  123. case (int)MaterialType.None:
  124. break;
  125. case (int)MaterialType.Image:
  126. var pImage = ResMgr.Instance.Load<GameObject>("Template/Prefab/Image");
  127. go = Instantiate(pImage, par.transform);
  128. var imageitem = go.AddComponent<TemplateImage>();
  129. go.SetActive(true);
  130. // imageitem.SetData(mat, materialObls.updateTime);
  131. noText.Add(imageitem);
  132. break;
  133. case (int)MaterialType.Video:
  134. var pVideo = ResMgr.Instance.Load<GameObject>("Template/Prefab/Video");
  135. go = Instantiate(pVideo, par.transform);
  136. var videoitem = go.AddComponent<TemplateVideo>();
  137. go.SetActive(true);
  138. // videoitem.SetData(mat, materialObls.updateTime);
  139. noText.Add(videoitem);
  140. break;
  141. case (int)MaterialType.Text:
  142. textmat = mat;
  143. var pText = ResMgr.Instance.Load<GameObject>("Template/Prefab/Text");
  144. go = Instantiate(pText, par.transform);
  145. var textitem = go.AddComponent<TemplateText>();
  146. go.SetActive(true);
  147. // textitem.SetData(mat, materialObls.updateTime);
  148. text = textitem;
  149. go.SetActive(false);
  150. break;
  151. default:
  152. break;
  153. }
  154. if (go != null)
  155. {
  156. go.name = string.IsNullOrWhiteSpace(mat.name) ? par.name + "-" + i.ToString() : mat.name;
  157. go.AddComponent<EditorEventHandler>().Type = (MaterialType)(mat.type);
  158. if (materialObls.select)
  159. {
  160. go.transform.localPosition = mat.objectTransform.nowPos;
  161. go.transform.localEulerAngles = mat.objectTransform.nowRot;
  162. go.transform.localScale = mat.objectTransform.nowScale;
  163. }
  164. else
  165. {
  166. go.transform.localEulerAngles = Vector3.zero;
  167. go.transform.localScale = new Vector3(1, 1, 1);
  168. if (mat.type != (int)MaterialType.Text)
  169. {
  170. go.transform.localPosition = Vector3.zero;
  171. }
  172. else
  173. {
  174. //Set text position
  175. if (mat.textPosition == "top")
  176. {
  177. go.transform.localPosition = new Vector3(0, 0.3f, 0);
  178. }
  179. else if (mat.textPosition == "bottom")
  180. {
  181. go.transform.localPosition = new Vector3(0, -0.3f, 0);
  182. }
  183. }
  184. mat.objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  185. }
  186. ObjectValue objectValue = new ObjectValue(i, materialObls.id, mat.name, go);
  187. objectValues.Add(objectValue);
  188. mat.prefabModel = go;
  189. }
  190. }
  191. #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回
  192. if (!materialObls.select)
  193. {
  194. Column(noText, par);
  195. //第一次排列后,需调整初始坐标和当前坐标保持一致
  196. for (int i = 0; i < objectValues.Count; i++)
  197. {
  198. objectValues[i].InitTransform();
  199. objectValues[i].Object.SetActive(true);
  200. var go = objectValues[i].Object;
  201. materialObls.materialList[objectValues[i].ID].objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  202. }
  203. }
  204. if (textmat != null)
  205. {
  206. text.gameObject.SetActive(true);
  207. }
  208. #endregion
  209. }
  210. /// <summary>
  211. /// 根据数量排列图片或文字
  212. /// </summary>
  213. /// <param name="data">图片和视频列表</param>
  214. private static void Column(List<BaseTemPlate> data, GameObject game)
  215. {
  216. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  217. scgrid.IsIgnoreInactiveObj = true;
  218. scgrid.LayoutType = LayoutTypes.Horizontal;
  219. scgrid.Columns = 3;
  220. scgrid.SpaceX = 0.7f;
  221. scgrid.SpaceY = 0.7f;
  222. scgrid.RefreshInfo();
  223. Destroy(scgrid);
  224. data[0].transform.localPosition += new Vector3(0, 0, -0.2f);
  225. data[0].transform.localRotation = Quaternion.Euler(0, -30, 0);
  226. data[2].transform.localPosition += new Vector3(0, 0, -0.2f);
  227. data[2].transform.localRotation = Quaternion.Euler(0, 30, 0);
  228. data[3].transform.localPosition += new Vector3(0, 0, -0.2f);
  229. data[3].transform.localRotation = Quaternion.Euler(0, -30, 0);
  230. if (data.Count < 6)
  231. Debug.LogWarning("双排扇形不足六个元素图片或视频");
  232. else
  233. {
  234. data[5].transform.localPosition += new Vector3(0, 0, -0.2f);
  235. data[5].transform.localRotation = Quaternion.Euler(0, 30, 0);
  236. }
  237. }
  238. }