SRTitleLayout.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 SRTitleLayout : 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,string type ="1")
  18. {
  19. newmaterial = materialObls;
  20. BaseTemPlate text = new BaseTemPlate();
  21. List<BaseTemPlate> noText = new List<BaseTemPlate>();
  22. MaterialObjValue textmat = null;
  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. Column(noText, par);
  101. //第一次排列后,需调整初始坐标和当前坐标保持一致
  102. for (int i = 0; i < objectValues.Count; i++)
  103. {
  104. objectValues[i].InitTransform();
  105. objectValues[i].Object.SetActive(true);
  106. var go = objectValues[i].Object;
  107. newmaterial.materialList[objectValues[i].ID].ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  108. }
  109. if(type !="1") // 判断是不是巨幕
  110. {
  111. if (objectValues.Count > 0)
  112. {
  113. objectValues[0].Object.transform.parent.localScale=new Vector3(3,3,3);
  114. }
  115. }
  116. }
  117. if (textmat != null)
  118. {
  119. text.gameObject.SetActive(true);
  120. }
  121. #endregion
  122. return objectValues;
  123. }
  124. /// <summary>
  125. /// 根据素材数量对物体进行布局组合
  126. /// 忽略文字类型
  127. /// </summary>
  128. /// <param name="materialObls">素材</param>
  129. /// <param name="par">素材父物体,景点</param>
  130. /// <returns></returns>
  131. public static void CalLayout(ModelList materialObls, GameObject par)
  132. {
  133. BaseTemPlate text = new BaseTemPlate();
  134. List<BaseTemPlate> noText = new List<BaseTemPlate>();
  135. ModelItem textmat = null;
  136. List<ObjectValue> objectValues = new List<ObjectValue>();
  137. for (int i = 0; i < materialObls.materialList.Count; i++)
  138. {
  139. var mat = materialObls.materialList[i];
  140. GameObject go = null;
  141. switch (mat.type)
  142. {
  143. case (int)MaterialType.None:
  144. break;
  145. case (int)MaterialType.Image:
  146. var pImage = ResMgr.Instance.Load<GameObject>("Template/Prefab/Image");
  147. go = Instantiate(pImage, par.transform);
  148. var imageitem = go.AddComponent<TemplateImage>();
  149. go.SetActive(true);
  150. // imageitem.SetData(mat, materialObls.updateTime);
  151. noText.Add(imageitem);
  152. break;
  153. case (int)MaterialType.Video:
  154. var pVideo = ResMgr.Instance.Load<GameObject>("Template/Prefab/Video");
  155. go = Instantiate(pVideo, par.transform);
  156. var videoitem = go.AddComponent<TemplateVideo>();
  157. go.SetActive(true);
  158. // videoitem.SetData(mat, materialObls.updateTime);
  159. noText.Add(videoitem);
  160. break;
  161. case (int)MaterialType.Text:
  162. textmat = mat;
  163. var pText = ResMgr.Instance.Load<GameObject>("Template/Prefab/Text");
  164. go = Instantiate(pText, par.transform);
  165. var textitem = go.AddComponent<TemplateText>();
  166. go.SetActive(true);
  167. // textitem.SetData(mat, materialObls.updateTime);
  168. text = textitem;
  169. go.SetActive(false);
  170. break;
  171. default:
  172. break;
  173. }
  174. if (go != null)
  175. {
  176. go.name = string.IsNullOrWhiteSpace(mat.name) ? par.name + "-" + i.ToString() : mat.name;
  177. go.AddComponent<EditorEventHandler>().Type = (MaterialType)(mat.type);
  178. if (materialObls.select)
  179. {
  180. go.transform.localPosition = mat.objectTransform.nowPos;
  181. go.transform.localEulerAngles = mat.objectTransform.nowRot;
  182. go.transform.localScale = mat.objectTransform.nowScale;
  183. }
  184. else
  185. {
  186. go.transform.localEulerAngles = Vector3.zero;
  187. go.transform.localScale = new Vector3(1, 1, 1);
  188. if (mat.type != (int)MaterialType.Text)
  189. {
  190. go.transform.localPosition = Vector3.zero;
  191. }
  192. else
  193. {
  194. //Set text position
  195. if (mat.textPosition == "top")
  196. {
  197. go.transform.localPosition = new Vector3(0, 0.3f, 0);
  198. }
  199. else if (mat.textPosition == "bottom")
  200. {
  201. go.transform.localPosition = new Vector3(0, -0.3f, 0);
  202. }
  203. }
  204. mat.objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
  205. }
  206. mat.prefabModel = go;
  207. }
  208. }
  209. #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回
  210. if (!materialObls.select)
  211. {
  212. Column(noText, par);
  213. //第一次排列后,需调整初始坐标和当前坐标保持一致
  214. for (int i = 0; i < objectValues.Count; i++)
  215. {
  216. objectValues[i].InitTransform();
  217. objectValues[i].Object.SetActive(true);
  218. }
  219. }
  220. if (textmat != null)
  221. {
  222. text.gameObject.SetActive(true);
  223. }
  224. #endregion
  225. }
  226. /// <summary>
  227. /// 排列顺序
  228. /// </summary>
  229. /// <param name="data"></param>
  230. /// <param name="game"></param>
  231. public static void Column(List<BaseTemPlate> data, GameObject game)
  232. {
  233. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  234. scgrid.IsIgnoreInactiveObj = true;
  235. scgrid.LayoutType = LayoutTypes.Vertical;
  236. scgrid.Rows = 1;
  237. scgrid.SpaceX = 0.7f;
  238. scgrid.RefreshInfo();
  239. Destroy(scgrid);
  240. }
  241. }