123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- using SC.XR.Unity;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// 单排扇形
- /// </summary>
- public class SRSectorLayout : MonoBehaviour
- {
- /// <summary>
- /// 根据素材数量对物体进行布局组合
- /// 忽略文字类型
- /// </summary>
- /// <param name="materialObls">素材</param>
- /// <param name="par">素材父物体</param>
- /// <returns></returns>
- public static List<ObjectValue> CalLayout(MaterialObl materialObls, GameObject par, out MaterialObl newmaterial)
- {
- newmaterial = materialObls;
- BaseTemPlate text = new BaseTemPlate();
- MaterialObjValue textmat = null;
- List<BaseTemPlate> noText = new List<BaseTemPlate>();
- List<ObjectValue> objectValues = new List<ObjectValue>();
- for (int i = 0; i < materialObls.materialList.Count; i++)
- {
- var mat = materialObls.materialList[i];
- GameObject go = null;
- switch (mat.type)
- {
- case (int)MaterialType.None:
- break;
- case (int)MaterialType.Image:
- var pImage = ResMgr.Instance.Load<GameObject>("Image");
- go = Instantiate(pImage, par.transform);
- var imageitem = go.AddComponent<TemplateImage>();
- go.SetActive(true);
- imageitem.SetData(mat, materialObls.updateTime);
- noText.Add(imageitem);
- break;
- case (int)MaterialType.Video:
- var pVideo = ResMgr.Instance.Load<GameObject>("Video");
- go = Instantiate(pVideo, par.transform);
- var videoitem = go.AddComponent<TemplateVideo>();
- videoitem.autoplay = materialObls.autoplay;
- go.SetActive(true);
- videoitem.SetData(mat, materialObls.updateTime);
- noText.Add(videoitem);
- break;
- case (int)MaterialType.Text:
- textmat = mat;
- var pText = ResMgr.Instance.Load<GameObject>("Text");
- go = Instantiate(pText, par.transform);
- var textitem = go.AddComponent<TemplateText>();
- go.SetActive(true);
- textitem.SetData(mat, materialObls.updateTime);
- text = textitem;
- go.SetActive(false);
- break;
- default:
- break;
- }
- if (go != null)
- {
- go.name = string.IsNullOrWhiteSpace(mat.name) ? par.name + "-" + i.ToString() : mat.name;
- go.AddComponent<EditorEventHandler>().Type = (MaterialType)(mat.type);
- if (materialObls.select)
- {
- go.transform.localPosition = mat.ObjectTransform.nowPos;
- go.transform.localEulerAngles = mat.ObjectTransform.nowRot;
- go.transform.localScale = mat.ObjectTransform.nowScale;
- }
- else
- {
- go.transform.localEulerAngles = Vector3.zero;
- go.transform.localScale = new Vector3(1, 1, 1);
- if (mat.type != (int)MaterialType.Text)
- {
- go.transform.localPosition = Vector3.zero;
- }
- else
- {
- //Set text position
- if (mat.textPosition == "top")
- {
- go.transform.localPosition = new Vector3(0, 0.6f, 0);
- }
- else if (mat.textPosition == "bottom")
- {
- go.transform.localPosition = new Vector3(0, -0.6f, 0);
- }
- }
- mat.ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
- }
- ObjectValue objectValue = new ObjectValue(i, materialObls.id, mat.name, go);
- objectValues.Add(objectValue);
- }
- }
- #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回
- if (!materialObls.select)
- {
- var count = noText.Count;
- switch (count)
- {
- case 3:
- Column_3(noText, par);
- break;
- case 4:
- Column_4(noText, par);
- break;
- case 5:
- Column_5(noText, par);
- break;
- case 6:
- Column_6(noText, par);
- break;
- default:
- break;
- }
- //第一次排列后,需调整初始坐标和当前坐标保持一致
- for (int i = 0; i < objectValues.Count; i++)
- {
- objectValues[i].InitTransform();
- objectValues[i].Object.SetActive(true);
- var go = objectValues[i].Object;
- newmaterial.materialList[objectValues[i].ID].ObjectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
- }
- }
- if (textmat != null)
- {
- text.gameObject.SetActive(true);
- }
- #endregion
- return objectValues;
- }
- public static IEnumerator CalLayout(ModelList materialObls, GameObject par)
- {
-
- BaseTemPlate text = new BaseTemPlate();
- ModelItem textmat = null;
- List<BaseTemPlate> noText = new List<BaseTemPlate>();
- List<ObjectValue> objectValues = new List<ObjectValue>();
- for (int i = 0; i < materialObls.materialList.Count; i++)
- {
- var mat = materialObls.materialList[i];
- GameObject go = null;
- switch (mat.type)
- {
- case (int)MaterialType.None:
- break;
- case (int)MaterialType.Image:
- var pImage = ResMgr.Instance.LoadAsync<GameObject>("Template/Prefab/Image");
- yield return pImage;
- go = Instantiate(pImage.asset as GameObject, par.transform);
- var imageitem = go.AddComponent<TemplateImage>();
- go.SetActive(true);
- // imageitem.SetData(mat, materialObls.updateTime);
- noText.Add(imageitem);
- break;
- case (int)MaterialType.Video:
- var pVideo = ResMgr.Instance.LoadAsync<GameObject>("Template/Prefab/Video");
- yield return pVideo;
- go = Instantiate(pVideo.asset as GameObject, par.transform);
- var videoitem = go.AddComponent<TemplateVideo>();
- videoitem.autoplay = materialObls.autoplay;
- go.SetActive(true);
- //videoitem.SetData(mat, materialObls.updateTime);
- noText.Add(videoitem);
- break;
- case (int)MaterialType.Text:
- textmat = mat;
- var pText = ResMgr.Instance.LoadAsync<GameObject>("Template/Prefab/Text");
- yield return pText;
- go = Instantiate(pText.asset as GameObject, par.transform);
- var textitem = go.AddComponent<TemplateText>();
- go.SetActive(true);
- // textitem.SetData(mat, materialObls.updateTime);
- text = textitem;
- go.SetActive(false);
- break;
- default:
- break;
- }
- if (go != null)
- {
- go.name = string.IsNullOrWhiteSpace(mat.name) ? par.name + "-" + i.ToString() : mat.name;
- go.AddComponent<EditorEventHandler>().Type = (MaterialType)(mat.type);
- if (materialObls.select)
- {
- go.transform.localPosition = mat.objectTransform.nowPos;
- go.transform.localEulerAngles = mat.objectTransform.nowRot;
- go.transform.localScale = mat.objectTransform.nowScale;
- }
- else
- {
- go.transform.localEulerAngles = Vector3.zero;
- go.transform.localScale = new Vector3(1, 1, 1);
- if (mat.type != (int)MaterialType.Text)
- {
- go.transform.localPosition = Vector3.zero;
- }
- else
- {
- //Set text position
- if (mat.textPosition == "top")
- {
- go.transform.localPosition = new Vector3(0, 0.3f, 0);
- }
- else if (mat.textPosition == "bottom")
- {
- go.transform.localPosition = new Vector3(0, -0.3f, 0);
- }
- }
- mat.objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
- }
- ObjectValue objectValue = new ObjectValue(i, materialObls.id, mat.name, go);
- objectValues.Add(objectValue);
- mat.prefabModel = go;
- // mat.initFrist();
- }
- }
- #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回
- if (!materialObls.select)
- {
- var count = noText.Count;
- switch (count)
- {
- case 3:
- Column_3(noText, par);
- break;
- case 4:
- Column_4(noText, par);
- break;
- case 5:
- Column_5(noText, par);
- break;
- default:
- break;
- }
- //第一次排列后,需调整初始坐标和当前坐标保持一致
- for (int i = 0; i < objectValues.Count; i++)
- {
- objectValues[i].InitTransform();
- objectValues[i].Object.SetActive(true);
- var go = objectValues[i].Object;
- materialObls.materialList[objectValues[i].ID].objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale);
- }
- }
- if (textmat != null)
- {
- text.gameObject.SetActive(true);
- }
- #endregion
-
- }
- /// <summary>
- /// 根据数量排列图片或文字
- /// </summary>
- /// <param name="data">图片和视频列表</param>
- private static void Column_3(List<BaseTemPlate> data, GameObject game)
- {
- var scgrid = game.AddComponent<SCGridLayoutGroup>();
- scgrid.IsIgnoreInactiveObj = true;
- scgrid.LayoutType = LayoutTypes.Vertical;
- scgrid.Rows = 1;
- scgrid.SpaceX = 0.7f;
- scgrid.RefreshInfo();
- Destroy(scgrid);
- data[0].transform.localPosition += new Vector3(0, 0, -0.18f);
- data[0].transform.localRotation = Quaternion.Euler(0, -30, 0);
- data[2].transform.localPosition += new Vector3(0, 0, -0.18f);
- data[2].transform.localRotation = Quaternion.Euler(0, 30, 0);
- }
- /// <summary>
- /// 根据数量排列图片或文字
- /// </summary>
- /// <param name="data">图片和视频列表</param>
- private static void Column_4(List<BaseTemPlate> data, GameObject game)
- {
- var scgrid = game.AddComponent<SCGridLayoutGroup>();
- scgrid.IsIgnoreInactiveObj = true;
- scgrid.LayoutType = LayoutTypes.Vertical;
- scgrid.Rows = 1;
- scgrid.SpaceX = 0.7f;
- scgrid.RefreshInfo();
- Destroy(scgrid);
- data[0].transform.localPosition += new Vector3(0, 0, -0.35f);
- data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
- data[3].transform.localPosition += new Vector3(0, 0, -0.35f);
- data[3].transform.localRotation = Quaternion.Euler(0, 45, 0);
- }
- /// <summary>
- /// 根据数量排列图片或文字
- /// </summary>
- /// <param name="data">图片和视频列表</param>
- private static void Column_5(List<BaseTemPlate> data, GameObject game)
- {
- var scgrid = game.AddComponent<SCGridLayoutGroup>();
- scgrid.IsIgnoreInactiveObj = true;
- scgrid.LayoutType = LayoutTypes.Vertical;
- scgrid.Rows = 1;
- scgrid.SpaceX = 0.7f;
- scgrid.RefreshInfo();
- Destroy(scgrid);
- data[0].transform.localPosition += new Vector3(0.15f, 0, -0.65f);
- data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
- data[4].transform.localPosition += new Vector3(-0.15f, 0, -0.65f);
- data[4].transform.localRotation = Quaternion.Euler(0, 45, 0);
- data[1].transform.localPosition += new Vector3(0, 0, -0.22f);
- data[1].transform.localRotation = Quaternion.Euler(0, -30, 0);
- data[3].transform.localPosition += new Vector3(0, 0, -0.22f);
- data[3].transform.localRotation = Quaternion.Euler(0, 30, 0);
- }
- /// <summary>
- /// 根据数量排列图片或文字
- /// </summary>
- /// <param name="data">图片和视频列表</param>
- private static void Column_6(List<BaseTemPlate> data, GameObject game)
- {
- var scgrid = game.AddComponent<SCGridLayoutGroup>();
- scgrid.IsIgnoreInactiveObj = true;
- scgrid.LayoutType = LayoutTypes.Vertical;
- scgrid.Rows = 1;
- scgrid.SpaceX = 0.7f;
- scgrid.RefreshInfo();
- Destroy(scgrid);
- data[0].transform.localPosition += new Vector3(0.15f, 0, -0.65f);
- data[0].transform.localRotation = Quaternion.Euler(0, -45, 0);
- data[5].transform.localPosition += new Vector3(-0.15f, 0, -0.65f);
- data[5].transform.localRotation = Quaternion.Euler(0, 45, 0);
- data[1].transform.localPosition += new Vector3(0, 0, -0.22f);
- data[1].transform.localRotation = Quaternion.Euler(0, -30, 0);
- data[4].transform.localPosition += new Vector3(0, 0, -0.22f);
- data[4].transform.localRotation = Quaternion.Euler(0, 30, 0);
- }
- }
|