using SC.XR.Unity.Module_InputSystem; using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using XRTool.Util; public class ModelList { public int id { get; set; } public bool select { get; set; } public string name { get; set; } public int type { get; set; } public string describe { get; set; } public List materialList { get; set; } public int updateTime { get; set; } public string typesetting { get; set; } public bool autoplay { get; set; } public ObjectTransform objectTransform { get; set; } public Transform materalLibrary; public SpotsItem spotsItem; public GameObject _model; //组合模型数据 public GameObject Model { get { if (_model == null) { init(false); } return _model; } } public void init(bool isAdd) { if (_model==null) { _model = new GameObject(name); _model.transform.parent = materalLibrary; // InitObjectTransform(); _model.SetActive(false); // initModelObj(); } /* if (!isAdd&& spotsItem!=null) { //_model = new GameObject("进入场景----" + name); _model.transform.parent = spotsItem.SpotsObj.transform; //InitObjectTransform(); // initModelObj(); }*/ //for (int i = 0; i < materialList.Count; i++) //{ // materialList[i].init(); //} /* if (isAdd) { for (int i = 0; i < materialList.Count; i++) { materialList[i].init(); } _model.transform.parent = spotsItem.SpotsObj.transform; //initModelObj(); }*/ } //public void Init() //{ // _model = new GameObject(name); // gameObject.SetActive(false); // InitObjectTransform(); // _model.transform.parent = materalLibrary; // _model.transform.localPosition = getInfoPos(); // _model.transform.localEulerAngles = getInfoEul(); //} Vector3 getInfoPos() { if(objectTransform==null) { return Vector3.zero; } //return Vector3.zero; if (objectTransform.nowPos == Vector3.zero) return objectTransform.startPos; else return objectTransform.nowPos; } Vector3 getInfoEul() { if (objectTransform == null) { return Vector3.zero; } if (objectTransform.nowRot == Vector3.zero) return objectTransform.startRot; else return objectTransform.nowRot; // return Vector3.zero; } Vector3 getInfoScale() { if (objectTransform.nowScale == Vector3.zero) return objectTransform.startScale; else return objectTransform.nowScale; } bool iscreate = false; private void InitObjectTransform(Action callback) { if(iscreate) { callback.Invoke(); return; } iscreate = true; if (objectTransform==null) { objectTransform = new ObjectTransform(); objectTransform.startPos = Vector3.zero; objectTransform.nowPos = Vector3.zero; objectTransform.startRot = Vector3.zero; objectTransform.nowRot = Vector3.zero; objectTransform.startScale = Vector3.one; objectTransform.nowScale = Vector3.zero; } // 处理组合素材 switch (type) { case (int)MaterialType.None: break; case (int)MaterialType.Image: case (int)MaterialType.Video: case (int)MaterialType.Model: case (int)MaterialType.Text: case (int)MaterialType.LocaImage: GameManager.Instance.StartCoroutine(CreateSuCaiObj(this, Model, callback)); break; case (int)MaterialType.Image_Video_Text: GameManager.Instance.StartCoroutine(CreateImage_Video_Text(this, Model,callback)); break; case (int)MaterialType.Model_Text: GameManager.Instance.StartCoroutine(CreateModel_Text(this, Model, callback)); break; default: break; } } /// /// 创建 1图片 2视频 3模型 4文字 /// /// 素材类型 /// 素材数据 /// 景点 /// private IEnumerator CreateSuCaiObj(ModelList material, GameObject spoit, Action callback) { List objects = new List(); List objs = material.materialList; for (int i = 0; i < objs.Count; i++) { var mat = objs[i]; GameObject go = null; switch (mat.type) { case (int)MaterialType.None: break; case (int)MaterialType.Image: var pImage = ResMgr.Instance.LoadAsync("Image"); yield return pImage; go = GameObject.Instantiate(pImage.asset as GameObject, spoit.transform); go.SetActive(true); go.AddComponent(); //var imageitem = go.AddComponent(); //imageitem.SetData(mat, material.updateTime); break; case (int)MaterialType.Video: var pVideo = ResMgr.Instance.LoadAsync("Video"); yield return pVideo; go = GameObject.Instantiate(pVideo.asset as GameObject, spoit.transform); go.SetActive(true); TemplateVideo video = go.AddComponent(); video.localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(objs[i].DownloadPath); video.autoplay = material.autoplay; //var videoitem = go.AddComponent(); //videoitem.SetData(mat, material.updateTime); break; case (int)MaterialType.Model: var pModel = ResMgr.Instance.LoadAsync("Model"); yield return pModel; go = GameObject.Instantiate(pModel.asset as GameObject, spoit.transform); go.SetActive(true); go.AddComponent(); //var modelitem = go.AddComponent(); //modelitem.SetData(mat, (int)Time.deltaTime); //Debug.Log(modelitem.Data.localSavePath); break; case (int)MaterialType.Text: var pText = ResMgr.Instance.LoadAsync("Text"); yield return pText; go = GameObject.Instantiate(pText.asset as GameObject, spoit.transform); go.SetActive(true); go.AddComponent(); //var textitem = go.AddComponent(); //textitem.SetData(mat, material.updateTime); break; case (int)MaterialType.LocaImage: var pLocaImage = ResMgr.Instance.LoadAsync("Image"); yield return pLocaImage; go = GameObject.Instantiate(pLocaImage.asset as GameObject, spoit.transform); go.SetActive(true); go.AddComponent(); //var locaImageitem = go.AddComponent(); //locaImageitem.SetLocaImageData(mat); break; default: break; } if (go != null) { go.name = string.IsNullOrWhiteSpace(mat.name) ? spoit.name + "-" + i.ToString() : mat.name; mat.name = go.name; go.AddComponent().Type = (MaterialType)(mat.type); if (material.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); } mat.prefabModel = go; callback.Invoke(); // mat.initFrist(); } } } /// /// 创建 6模型+文字 /// /// /// /// private IEnumerator CreateModel_Text(ModelList material, GameObject spoit,Action callback) { List objects = new List(); List objs = material.materialList; for (int i = 0; i < objs.Count; i++) { var mat = objs[i]; GameObject go = null; switch (mat.type) { case (int)MaterialType.None: break; case (int)MaterialType.Model: Debug.Log("添加模型===》"); var pModel = ResMgr.Instance.LoadAsync("Model"); yield return pModel; go = GameObject.Instantiate(pModel.asset as GameObject, spoit.transform); go.SetActive(true); go.AddComponent(); //var modelitem = go.AddComponent(); //modelitem.SetData(mat, material.updateTime); break; case (int)MaterialType.Text: Debug.Log("添加文字===》"); var pText = ResMgr.Instance.LoadAsync("Text"); yield return pText; go = GameObject.Instantiate(pText.asset as GameObject, spoit.transform); go.SetActive(true); go.AddComponent(); //var textitem = go.AddComponent(); //textitem.SetData(mat, material.updateTime); break; default: break; } if (go != null) { go.name = string.IsNullOrWhiteSpace(mat.name) ? spoit.name + "-" + i.ToString() : mat.name; go.AddComponent().Type = (MaterialType)(mat.type); if (material.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); } else if (mat.textPosition == "left") { go.transform.localPosition = new Vector3(-0.3f, 0, 0); } else if (mat.textPosition == "right") { go.transform.localPosition = new Vector3(0.3f, 0f, 0); } } // mat.objectTransform.SetStartValue(go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale); } ObjectValue objectValue = CreateMatObjectValue(i, material.id, go, mat); objects.Add(objectValue); mat.prefabModel = go; // mat.initFrist(); } } callback.Invoke(); } /// /// 创建 5图片+文字/视频+文字 /// private IEnumerator CreateImage_Video_Text(ModelList material, GameObject spoit, Action callback) { yield return GameManager.Instance.StartCoroutine(SRTitleLayout.CalLayout(material, spoit, callback)); /* List objects = new List(); //switch (material.typesetting) //{ // case "1": // SRTitleLayout.CalLayout(material, spoit); // break; // case "2": // SRSectorLayout.CalLayout(material, spoit); // break; // case "3": // DRTitleLayout.CalLayout(material, spoit); // break; // case "4": // DRFanLayout.CalLayout(material, spoit); // break; // case "5": // SRTitleLayout.CalLayout(material, spoit); // break; // case "自定义": // break; // default: // break; //} switch (material.typesetting) { case "1": yield return GameManager.Instance.StartCoroutine(SRTitleLayout.CalLayout(material, spoit)); break; case "2": yield return GameManager.Instance.StartCoroutine(SRTitleLayout.CalLayout(material, spoit)); break; case "3": yield return GameManager.Instance.StartCoroutine(SRTitleLayout.CalLayout(material, spoit)); break; case "4": yield return GameManager.Instance.StartCoroutine(SRSectorLayout.CalLayout(material, spoit)); break; case "5": yield return GameManager.Instance.StartCoroutine(DRTitleLayout.CalLayout(material, spoit)); break; case "6": yield return GameManager.Instance.StartCoroutine(DRTitleLayout.CalLayout(material, spoit)); break; case "7": yield return GameManager.Instance.StartCoroutine(DRTitleLayout.CalLayout(material, spoit)); break; case "8": yield return GameManager.Instance.StartCoroutine(DRFanLayout.CalLayout(material, spoit)); break; default: break; } */ } /// /// 创建物体ObjectValue,并赋值位置信息 /// /// /// /// /// /// private ObjectValue CreateMatObjectValue(int id, int parentid, GameObject go, ModelItem mat) { go.name = string.IsNullOrWhiteSpace(mat.name) ? go.name : mat.name; go.AddComponent().Type = (MaterialType)(mat.type); //为物体位置信息赋值 if (mat.objectTransform != null) { go.transform.localPosition = mat.objectTransform.nowPos; go.transform.localEulerAngles = mat.objectTransform.nowRot; go.transform.localScale = mat.objectTransform.nowScale; } else { go.transform.localPosition = Vector3.zero; go.transform.localEulerAngles = Vector3.zero; go.transform.localScale = new Vector3(1, 1, 1); } ObjectValue objectValue = new ObjectValue(id, parentid, mat.name, go, go.transform.localPosition, go.transform.localEulerAngles, go.transform.localScale); return objectValue; } public ModelList Clone() { ModelList mlist = new ModelList(); mlist.materialList = new List(); for (int i = 0; i < materialList.Count; i++) { mlist.materialList.Add(new ModelItem()); mlist.materialList[i].autoPlay = autoplay; mlist.materialList[i].abByte = materialList[i].abByte; mlist.materialList[i].dmdown = materialList[i].dmdown; mlist.materialList[i].DownloadPath = materialList[i].DownloadPath; mlist.materialList[i].fileId = materialList[i].fileId; mlist.materialList[i].id = materialList[i].id; mlist.materialList[i].localSavePath = materialList[i].localSavePath; mlist.materialList[i].modelList = materialList[i].modelList; mlist.materialList[i].modelType = materialList[i].modelType; mlist.materialList[i].name = materialList[i].name; mlist.materialList[i].objectTransform = materialList[i].objectTransform; mlist.materialList[i].prefabModel = materialList[i].prefabModel; mlist.materialList[i].template = materialList[i].template; mlist.materialList[i].TextBJ = materialList[i].TextBJ; mlist.materialList[i].textContent = materialList[i].textContent; mlist.materialList[i].textPosition = materialList[i].textPosition; mlist.materialList[i].type = materialList[i].type; mlist.materialList[i].uid = materialList[i].uid; mlist.materialList[i].updateTime = materialList[i].updateTime; mlist.materialList[i].url = materialList[i].url; mlist.materialList[i].Version = materialList[i].Version; mlist.materialList[i]._model = materialList[i]._model; } mlist.autoplay = autoplay; mlist.describe = describe; mlist.id = id; mlist.iscreate = iscreate; mlist.materalLibrary = materalLibrary; mlist.name = name; mlist.objectTransform = objectTransform; mlist.select = select; mlist.spotsItem = spotsItem; mlist.type = type; mlist.typesetting = typesetting; mlist.updateTime = updateTime; mlist._model = _model; /* ModelList clonedObject = (ModelList)this.MemberwiseClone(); for (int i = 0; i < clonedObject.materialList.Count; i++) { materialList[i].isload = true; Debug.Log("getdownloaditem=====>clonedObject" + materialList[i].isload + "_" + clonedObject.materialList[i].isload); clonedObject.materialList[i].isload = false; Debug.Log("getdownloaditem=====>clonedObject2" + materialList[i].isload + "_" + clonedObject.materialList[i].isload); }*/ return mlist; } public void Clear() { iscreate = false; if (materialList != null) { for (int i = 0; i < materialList.Count; i++) { materialList[i].Clear(); Debug.Log("Clear SpotsList materialList[i]" + i); } } //GameManager.Instance.destroyGo(_model); //_model = null; Debug.Log("Clear SpotsList materialList[i] Clear"); } public void initModelObj() { InitObjectTransform(()=> { if (getInfoPos() != Vector3.zero) { _model.transform.localPosition = getInfoPos(); } if (getInfoEul() != Vector3.zero) { _model.transform.localEulerAngles = getInfoEul(); } if (getInfoScale() != Vector3.zero) { _model.transform.localScale = getInfoScale(); } else { _model.transform.localScale = Vector3.one; } if (materialList != null) { for (int i = 0; i < materialList.Count; i++) { Debug.Log("initModelObj===> " + materialList[i].id); materialList[i].initModelObj(); } } }); // _model.transform.localPosition = getInfoPos(); // _model.transform.localEulerAngles = getInfoEul(); } }