DRFanLayout.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /// <summary>
  112. /// 根据数量排列图片或文字
  113. /// </summary>
  114. /// <param name="data">图片和视频列表</param>
  115. private static void Column(List<BaseTemPlate> data, GameObject game)
  116. {
  117. var scgrid = game.AddComponent<SCGridLayoutGroup>();
  118. scgrid.IsIgnoreInactiveObj = true;
  119. scgrid.LayoutType = LayoutTypes.Horizontal;
  120. scgrid.Columns = 3;
  121. scgrid.SpaceX = 0.7f;
  122. scgrid.SpaceY = 0.7f;
  123. scgrid.RefreshInfo();
  124. Destroy(scgrid);
  125. data[0].transform.localPosition += new Vector3(0, 0, -0.2f);
  126. data[0].transform.localRotation = Quaternion.Euler(0, -30, 0);
  127. data[2].transform.localPosition += new Vector3(0, 0, -0.2f);
  128. data[2].transform.localRotation = Quaternion.Euler(0, 30, 0);
  129. data[3].transform.localPosition += new Vector3(0, 0, -0.2f);
  130. data[3].transform.localRotation = Quaternion.Euler(0, -30, 0);
  131. if (data.Count < 6)
  132. Debug.LogWarning("双排扇形不足六个元素图片或视频");
  133. else
  134. {
  135. data[5].transform.localPosition += new Vector3(0, 0, -0.2f);
  136. data[5].transform.localRotation = Quaternion.Euler(0, 30, 0);
  137. }
  138. }
  139. }