SRTitleLayout.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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)
  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.3f, 0);
  85. }
  86. else if (mat.textPosition == "bottom")
  87. {
  88. go.transform.localPosition = new Vector3(0, -0.3f, 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. }
  110. if (textmat != null)
  111. {
  112. text.gameObject.SetActive(true);
  113. }
  114. #endregion
  115. return objectValues;
  116. }
  117. /// <summary>
  118. /// 排列顺序
  119. /// </summary>
  120. /// <param name="data"></param>
  121. /// <param name="game"></param>
  122. public static void Column(List<BaseTemPlate> data, GameObject game)
  123. {
  124. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  125. scgrid.IsIgnoreInactiveObj = true;
  126. scgrid.LayoutType = LayoutTypes.Vertical;
  127. scgrid.Rows = 1;
  128. scgrid.SpaceX = 0.7f;
  129. scgrid.RefreshInfo();
  130. Destroy(scgrid);
  131. }
  132. }