using SC.XR.Unity; using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 双排平铺 /// public class DRTitleLayout : MonoBehaviour { /// /// 根据素材数量对物体进行布局组合 /// 忽略文字类型 /// /// 素材 /// 素材父物体 /// public static List CalLayout(MaterialObl materialObls, GameObject par, out MaterialObl newmaterial) { newmaterial = materialObls; BaseTemPlate text = new BaseTemPlate(); MaterialObjValue textmat = null; List noText = new List(); List objectValues = new List(); if (materialObls.materialList != null || materialObls.materialList.Count >0) for (int i = 0; i < materialObls.materialList.Count; i++) { var mat = materialObls.materialList[i]; GameObject go = null; switch (int.Parse(mat.type)) { case (int)MaterialType.None: break; case (int)MaterialType.Image: var pImage = ResMgr.Instance.Load("Template/Prefab/Image"); go = Instantiate(pImage, par.transform); var imageitem = go.AddComponent(); go.SetActive(true); imageitem.SetData(mat, materialObls.updateTime); noText.Add(imageitem); break; case (int)MaterialType.Video: var pVideo = ResMgr.Instance.Load("Template/Prefab/Video"); go = Instantiate(pVideo, par.transform); var videoitem = go.AddComponent(); go.SetActive(true); videoitem.SetData(mat, materialObls.updateTime); noText.Add(videoitem); break; case (int)MaterialType.Text: textmat = mat; var pText = ResMgr.Instance.Load("Template/Prefab/Text"); go = Instantiate(pText, par.transform); var textitem = go.AddComponent(); 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().Type = (MaterialType)(int.Parse(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 (int.Parse(mat.type) != (int)MaterialType.Text) { go.transform.localPosition = Vector3.zero; } else { //Set text position if (mat.textPosition == "top") { go.transform.localPosition = new Vector3(0, 1f, 0); } else if (mat.textPosition == "bottom") { go.transform.localPosition = new Vector3(0, -1f, 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 2: Column_2(noText, 2, par); break; case 3: Column_3(noText); break; case 4: Column_2(noText, 4, par); break; case 5: Column_5(noText, par); break; case 6: Column_2(noText, 6, 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 void CalLayout(ModelList materialObls, GameObject par) { BaseTemPlate text = new BaseTemPlate(); ModelItem textmat = null; List noText = new List(); List objectValues = new List(); if (materialObls.materialList != null || materialObls.materialList.Count > 0) 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("Template/Prefab/Image"); go = Instantiate(pImage, par.transform); var imageitem = go.AddComponent(); go.SetActive(true); // imageitem.SetData(mat, materialObls.updateTime); noText.Add(imageitem); break; case (int)MaterialType.Video: var pVideo = ResMgr.Instance.Load("Template/Prefab/Video"); go = Instantiate(pVideo, par.transform); var videoitem = go.AddComponent(); go.SetActive(true); // videoitem.SetData(mat, materialObls.updateTime); noText.Add(videoitem); break; case (int)MaterialType.Text: textmat = mat; var pText = ResMgr.Instance.Load("Template/Prefab/Text"); go = Instantiate(pText, par.transform); var textitem = go.AddComponent(); 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().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; } } #region 需要判断是否是第一次编辑坐标,若是则执行下方代码,不是则直接返回 if (!materialObls.select) { var count = noText.Count; switch (count) { case 2: Column_2(noText, 2, par); break; case 3: Column_3(noText); break; case 4: Column_2(noText, 4, par); break; case 5: Column_5(noText, par); break; case 6: Column_2(noText, 6, 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 } private static void Column_2(List data, int colum, GameObject game) { var scgrid = game.AddComponent(); scgrid.IsIgnoreInactiveObj = true; scgrid.LayoutType = LayoutTypes.Horizontal; scgrid.Columns = colum / 2; scgrid.SpaceX = 0.7f; scgrid.SpaceY = 0.7f; scgrid.RefreshInfo(); Destroy(scgrid); } private static void Column_3(List data) { data[0].transform.localPosition = new Vector3(0, 0.24f, 0f); data[1].transform.localPosition = new Vector3(-0.333f, -0.331f, 0f); data[2].transform.localPosition = new Vector3(0.333f, -0.331f, 0f); } private static void Column_5(List data, GameObject game) { var scgrid = game.AddComponent(); scgrid.IsIgnoreInactiveObj = true; scgrid.LayoutType = LayoutTypes.Horizontal; scgrid.Columns = 3; scgrid.SpaceX = 0.7f; scgrid.SpaceY = 0.7f; scgrid.RefreshInfo(); Destroy(scgrid); data[3].transform.localPosition += new Vector3(0.357f, 0, 0f); data[4].transform.localPosition += new Vector3(0.357f, 0, 0f); } }