123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- 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: System.ICloneable
- {
- 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<ModelItem> 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;
- }
- }
- /// <summary>
- /// 创建 1图片 2视频 3模型 4文字
- /// </summary>
- /// <param name="type">素材类型</param>
- /// <param name="material">素材数据</param>
- /// <param name="spoit">景点</param>
- /// <returns></returns>
- private IEnumerator CreateSuCaiObj(ModelList material, GameObject spoit, Action callback)
- {
- List<ObjectValue> objects = new List<ObjectValue>();
- List<ModelItem> 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<GameObject>("Image");
- yield return pImage;
- go = GameObject.Instantiate(pImage.asset as GameObject, spoit.transform);
- go.SetActive(true);
- go.AddComponent<TemplateImage>();
- //var imageitem = go.AddComponent<TemplateImage>();
- //imageitem.SetData(mat, material.updateTime);
- break;
- case (int)MaterialType.Video:
- var pVideo = ResMgr.Instance.LoadAsync<GameObject>("Video");
- yield return pVideo;
- go = GameObject.Instantiate(pVideo.asset as GameObject, spoit.transform);
- go.SetActive(true);
- TemplateVideo video = go.AddComponent<TemplateVideo>();
- video.localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(objs[i].DownloadPath);
- video.autoplay = material.autoplay;
- //var videoitem = go.AddComponent<TemplateVideo>();
- //videoitem.SetData(mat, material.updateTime);
- break;
- case (int)MaterialType.Model:
- var pModel = ResMgr.Instance.LoadAsync<GameObject>("Model");
- yield return pModel;
- go = GameObject.Instantiate(pModel.asset as GameObject, spoit.transform);
- go.SetActive(true);
- go.AddComponent<TemplateModel>();
- //var modelitem = go.AddComponent<TemplateModel>();
- //modelitem.SetData(mat, (int)Time.deltaTime);
- //Debug.Log(modelitem.Data.localSavePath);
- break;
- case (int)MaterialType.Text:
- var pText = ResMgr.Instance.LoadAsync<GameObject>("Text");
- yield return pText;
- go = GameObject.Instantiate(pText.asset as GameObject, spoit.transform);
- go.SetActive(true);
- go.AddComponent<TemplateText>();
- //var textitem = go.AddComponent<TemplateText>();
- //textitem.SetData(mat, material.updateTime);
- break;
- case (int)MaterialType.LocaImage:
- var pLocaImage = ResMgr.Instance.LoadAsync<GameObject>("Image");
- yield return pLocaImage;
- go = GameObject.Instantiate(pLocaImage.asset as GameObject, spoit.transform);
- go.SetActive(true);
- go.AddComponent<TemplateImage>();
- //var locaImageitem = go.AddComponent<TemplateImage>();
- //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<EditorEventHandler>().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();
- }
- }
- }
- /// <summary>
- /// 创建 6模型+文字
- /// </summary>
- /// <param name="material"></param>
- /// <param name="spoit"></param>
- /// <returns></returns>
- private IEnumerator CreateModel_Text(ModelList material, GameObject spoit,Action callback)
- {
- List<ObjectValue> objects = new List<ObjectValue>();
- List<ModelItem> 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:
- var pModel = ResMgr.Instance.LoadAsync<GameObject>("Model");
- yield return pModel;
- go = GameObject.Instantiate(pModel.asset as GameObject, spoit.transform);
- go.SetActive(true);
- go.AddComponent<TemplateModel>();
- //var modelitem = go.AddComponent<TemplateModel>();
- //modelitem.SetData(mat, material.updateTime);
- break;
- case (int)MaterialType.Text:
- var pText = ResMgr.Instance.LoadAsync<GameObject>("Text");
- yield return pText;
- go = GameObject.Instantiate(pText.asset as GameObject, spoit.transform);
- go.SetActive(true);
- go.AddComponent<TemplateText>();
- //var textitem = go.AddComponent<TemplateText>();
- //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<EditorEventHandler>().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;
- callback.Invoke();
- // mat.initFrist();
- }
-
- }
- }
- /// <summary>
- /// 创建 5图片+文字/视频+文字
- /// </summary>
- private IEnumerator CreateImage_Video_Text(ModelList material, GameObject spoit, Action callback)
- {
- yield return GameManager.Instance.StartCoroutine(SRTitleLayout.CalLayout(material, spoit, callback));
- /*
- List<ObjectValue> objects = new List<ObjectValue>();
- //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;
- }
- */
- }
- /// <summary>
- /// 创建物体ObjectValue,并赋值位置信息
- /// </summary>
- /// <param name="id"></param>
- /// <param name="parentid"></param>
- /// <param name="go"></param>
- /// <param name="mat"></param>
- /// <returns></returns>
- private ObjectValue CreateMatObjectValue(int id, int parentid, GameObject go, ModelItem mat)
- {
- go.name = string.IsNullOrWhiteSpace(mat.name) ? go.name : mat.name;
- go.AddComponent<EditorEventHandler>().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 object Clone()
- {
- ModelList clonedObject = (ModelList)this.MemberwiseClone();
- return clonedObject;
- }
- 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();
- }
- 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();
- }
- }
|