123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using QFramework;
- using QFramework.MREditor;
- public class MaterialShow : MonoBehaviour
- {
- public MaterialObjValue value;
- public string type;
- public int updateTime;
- public string spoitName;
- private void Start()
- {
-
- switch ((MaterialType)int.Parse(value.type))
- {
- case MaterialType.NULL:
- break;
- case MaterialType.Image:
- case MaterialType.Video:
- case MaterialType.Model:
- if (value.DownloadPath.IsNotNullAndEmpty() && value.DownloadPath != value.localSavePath)
- {
- MsgHandler.AddListener(value.DownloadPath, HandleMsg);
- LoadManager.Instance.LoadMaterial(value, updateTime);
- }
- else if (value.DownloadPath.IsNotNullAndEmpty() && value.DownloadPath == value.localSavePath)
- {
- // LoadLocalMaterial();
- StartCoroutine(LoadLocalMaterial(0.1f));
- }
- break;
- case MaterialType.Text:
- //string str = value.textContent;
- //object loaderObj = str;
- //LoadMaterial(loaderObj);
- // LoadLocalMaterial();
- StartCoroutine(LoadLocalMaterial(0.1f));
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 返回素材
- /// </summary>
- /// <param name="msg"></param>
- private void HandleMsg(Msg msg)
- {
- try
- {
- Debug.Log("HandleMsg");
- // LoadMaterial(msg.Value);
- StartCoroutine(LoadMaterial(msg.Value, 0.1f));
- }
- catch (System.Exception e)
- {
- ErrorLogPanel.Instance.Show(" 返回生成素材出现错误 " + e.Message );
- }
- }
- int num = 0;
- /// <summary>
- /// 加载素材
- /// </summary>
- /// <param name="loaderObj"> 返回加载物体 </param>
- public void LoadMaterial(object loaderObj)
- {
- Debug.Log(value.name + " 下载素材@@@@@@@@@@@@@" + num++);
- var loader = ResLoader.Allocate();
- switch ((MaterialType)int.Parse(value.type))
- {
- case MaterialType.Text:
- string path = "Resources/Template/Prefab/Text1";
- //if (int.Parse(value.TextBJ) == 1)
- //{
- // path = "Resources/Template/Prefab/Text";
- //}
- switch (value.TextBJ)
- {
- case "1":
- path = "Resources/Template/Prefab/Text1";
- break;
- case "2":
- path = "Resources/Template/Prefab/Text2";
- break;
- case "3":
- path = "Resources/Template/Prefab/Text3";
- break;
- default:
- break;
- }
- var mText = loader.LoadSync<GameObject>(path);
- GameObject objText = (GameObject)mText.Instantiate();
- objText.GetComponent<SetTextV>().SetText(loaderObj.ToString());
- objText.transform.SetParent(transform);
- objText.name = value.name;
- objText.transform.localPosition = Vector3.zero;
- objText.transform.localEulerAngles = Vector3.zero;
- // objText.transform.Identity();
- if (value.ObjectTransform.nowScale == Vector3.zero)
- {
- if (value.textPosition == "bottom")
- {
- transform.localPosition = new Vector3(0, -0.5f, 0);
- Debug.Log("bottom" + transform.localPosition);
- }
- else if (value.textPosition == "top")
- {
- transform.localPosition = new Vector3(0, 0.5f, 0);
- Debug.Log("top" + transform.localPosition);
- }
- transform.localEulerAngles = Vector3.zero;
- transform.localScale = Vector3.one;
- }
- else
- {
- // SettingTransform(value.ObjectTransform);
- }
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objText.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Image:
- var image = loader.LoadSync<GameObject>("Resources/Template/Prefab/Image");
- GameObject objImage = image.Instantiate();
- objImage.GetComponent<SetImageV>().SetImage((Sprite)loaderObj);
- objImage.transform.SetParent(transform);
- objImage.name = value.name;
- objImage.transform.LocalIdentity();
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Video:
- var video = loader.LoadSync<GameObject>("Resources/Template/Prefab/Video");
- GameObject objVideo = video.Instantiate();
- objVideo.GetComponent<VideoControl>().VideoURL = loaderObj.ToString();
- objVideo.transform.SetParent(transform);
- objVideo.name = value.name;
- objVideo.transform.LocalIdentity();
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Model:
- List<GameObject> listobj = (List<GameObject>)loaderObj;
- GameObject model = listobj[0];
- GameObject objModel = model.Instantiate();
- objModel.transform.SetParent(transform);
- objModel.name = value.name;
- objModel.transform.LocalIdentity();
- break;
- default:
- break;
- }
- MsgHandler.RemoveListener(value.DownloadPath, HandleMsg);
- SettingTransform(value.ObjectTransform);
- }
- //private void SettingTransform(MaterialObjValue value)
- //{
- // if (value.nowScale == Vector3.zero)
- // {
- // transform.localPosition = value.startPos;
- // transform.localEulerAngles = value.startRot;
- // transform.localScale = value.startScale;
- // }
- // else
- // {
- // transform.localPosition = value.nowPos;
- // transform.localEulerAngles = value.nowRot;
- // transform.localScale = value.nowScale;
- // }
- //}
- IEnumerator LoadMaterial(object loaderObj , float times)
- {
- MsgHandler.RemoveListener(value.DownloadPath, HandleMsg);
- yield return new WaitForEndOfFrame();
- Debug.Log(value.name + " 下载素材@@@@@@@@@@@@@" + num++);
- var loader = ResLoader.Allocate();
- switch ((MaterialType)int.Parse(value.type))
- {
- case MaterialType.Text:
- string path = "Resources/Template/Prefab/Text1";
- //if (int.Parse(value.TextBJ) == 1)
- //{
- // path = "Resources/Template/Prefab/Text";
- //}
- switch (value.TextBJ)
- {
- case "1":
- path = "Resources/Template/Prefab/Text1";
- break;
- case "2":
- path = "Resources/Template/Prefab/Text2";
- break;
- case "3":
- path = "Resources/Template/Prefab/Text3";
- break;
- default:
- break;
- }
- var mText = loader.LoadSync<GameObject>(path);
- GameObject objText = (GameObject)mText.Instantiate();
- objText.GetComponent<SetTextV>().SetText(loaderObj.ToString());
- objText.transform.SetParent(transform);
- objText.name = value.name;
- objText.transform.localPosition = Vector3.zero;
- objText.transform.localEulerAngles = Vector3.zero;
- // objText.transform.Identity();
- if (value.ObjectTransform.nowScale == Vector3.zero)
- {
- if (value.textPosition == "bottom")
- {
- transform.localPosition = new Vector3(0, -0.5f, 0);
- Debug.Log("bottom" + transform.localPosition);
- }
- else if (value.textPosition == "top")
- {
- transform.localPosition = new Vector3(0, 0.5f, 0);
- Debug.Log("top" + transform.localPosition);
- }
- transform.localEulerAngles = Vector3.zero;
- transform.localScale = Vector3.one;
- }
- else
- {
- // SettingTransform(value.ObjectTransform);
- }
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objText.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Image:
- var image = loader.LoadSync<GameObject>("Resources/Template/Prefab/Image");
- GameObject objImage = image.Instantiate();
- objImage.GetComponent<SetImageV>().SetImage((Sprite)loaderObj);
- objImage.transform.SetParent(transform);
- objImage.name = value.name;
- objImage.transform.LocalIdentity();
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Video:
- var video = loader.LoadSync<GameObject>("Resources/Template/Prefab/Video");
- GameObject objVideo = video.Instantiate();
- objVideo.GetComponent<VideoControl>().VideoURL = loaderObj.ToString();
- objVideo.transform.SetParent(transform);
- objVideo.name = value.name;
- objVideo.transform.LocalIdentity();
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Model:
- List<GameObject> listobj = (List<GameObject>)loaderObj;
- GameObject model = listobj[0];
- GameObject objModel = null;
- if(model.name == "Saloon")
- {
- objModel = GameManager.Instance.Saloon;
- }
- else
- {
- objModel = model.Instantiate();
- }
-
- objModel.SetActive(true);
- objModel.transform.SetParent(transform);
- objModel.name = value.name;
- objModel.transform.LocalIdentity();
- break;
- default:
- break;
- }
-
- SettingTransform(value.ObjectTransform);
-
- }
- /// <summary>
- /// 加载本地素材
- /// </summary>
- public void LoadLocalMaterial()
- {
- try
- {
- Debug.Log(value.name + " 本地素材@@@@@@@@@@@@@");
- var loader = ResLoader.Allocate();
- switch ((MaterialType)int.Parse(value.type))
- {
- case MaterialType.Text:
- string path = "Resources/Template/Prefab/Text";
- switch (value.TextBJ)
- {
- case "1":
- path = "Resources/Template/Prefab/Text1";
- break;
- case "2":
- path = "Resources/Template/Prefab/Text2";
- break;
- case "3":
- path = "Resources/Template/Prefab/Text3";
- break;
- default:
- break;
- }
- var mText = loader.LoadSync<GameObject>(path);
- GameObject objText = (GameObject)mText.Instantiate();
- objText.GetComponent<SetTextV>().SetText(value.textContent);
- objText.transform.SetParent(transform);
- objText.name = value.name;
- objText.transform.localPosition = Vector3.zero;
- objText.transform.localEulerAngles = Vector3.zero;
- if (value.ObjectTransform.nowScale == Vector3.zero)
- {
- if (value.textPosition == "bottom")
- {
- transform.localPosition = new Vector3(0, -0.5f, 0);
- Debug.Log("bottom" + transform.localPosition);
- }
- else if (value.textPosition == "top")
- {
- transform.localPosition = new Vector3(0, 0.5f, 0);
- Debug.Log("top" + transform.localPosition);
- }
- transform.localEulerAngles = value.ObjectTransform.startRot;
- if (value.ObjectTransform.startScale != Vector3.zero)
- transform.localScale = value.ObjectTransform.startScale;
- else
- transform.localScale = Vector3.one;
- }
- else
- {
- SettingTransform(value.ObjectTransform);
- }
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objText.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Image:
- var image = loader.LoadSync<GameObject>("Resources/Template/Prefab/Image");
- GameObject objImage = image.Instantiate();
- objImage.GetComponent<SetImageV>().SetImage(loader.LoadSync<Sprite>(value.DownloadPath));
- objImage.transform.SetParent(transform);
- objImage.name = value.name;
- objImage.transform.LocalIdentity();
- SettingTransform(value.ObjectTransform);
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Video:
- var video = loader.LoadSync<GameObject>("Resources/Template/Prefab/Video");
- GameObject objVideo = video.Instantiate();
- objVideo.GetComponent<VideoControl>().VideoURL = value.DownloadPath;
- objVideo.transform.SetParent(transform);
- objVideo.name = value.name;
- objVideo.transform.LocalIdentity();
- SettingTransform(value.ObjectTransform);
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Model:
- GameObject model = loader.LoadSync<GameObject>(value.DownloadPath);
- GameObject objModel = model.Instantiate();
- objModel.transform.SetParent(transform);
- objModel.name = value.name;
- objModel.transform.LocalIdentity();
- SettingTransform(value.ObjectTransform);
- break;
- default:
- break;
- }
- }
- catch (System.Exception)
- {
- ErrorLogPanel.Instance.Show(" 加载本地素材出现错误 ");
- }
- }
- IEnumerator LoadLocalMaterial(float times)
- {
- yield return new WaitForEndOfFrame();
- try
- {
- Debug.Log(value.name + " 本地素材@@@@@@@@@@@@@");
- var loader = ResLoader.Allocate();
- switch ((MaterialType)int.Parse(value.type))
- {
- case MaterialType.Text:
- string path = "Resources/Template/Prefab/Text";
- switch (value.TextBJ)
- {
- case "1":
- path = "Resources/Template/Prefab/Text1";
- break;
- case "2":
- path = "Resources/Template/Prefab/Text2";
- break;
- case "3":
- path = "Resources/Template/Prefab/Text3";
- break;
- default:
- break;
- }
- var mText = loader.LoadSync<GameObject>(path);
- GameObject objText = (GameObject)mText.Instantiate();
- objText.GetComponent<SetTextV>().SetText(value.textContent);
- objText.transform.SetParent(transform);
- objText.name = value.name;
- objText.transform.localPosition = Vector3.zero;
- objText.transform.localEulerAngles = Vector3.zero;
- if (value.ObjectTransform.nowScale == Vector3.zero)
- {
- if (value.textPosition == "bottom")
- {
- transform.localPosition = new Vector3(0, -0.5f, 0);
- Debug.Log("bottom" + transform.localPosition);
- }
- else if (value.textPosition == "top")
- {
- transform.localPosition = new Vector3(0, 0.5f, 0);
- Debug.Log("top" + transform.localPosition);
- }
- transform.localEulerAngles = value.ObjectTransform.startRot;
- if (value.ObjectTransform.startScale != Vector3.zero)
- transform.localScale = value.ObjectTransform.startScale;
- else
- transform.localScale = Vector3.one;
- }
- else
- {
- SettingTransform(value.ObjectTransform);
- }
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objText.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Image:
- var image = loader.LoadSync<GameObject>("Resources/Template/Prefab/Image");
- GameObject objImage = image.Instantiate();
- objImage.GetComponent<SetImageV>().SetImage(loader.LoadSync<Sprite>(value.DownloadPath));
- objImage.transform.SetParent(transform);
- objImage.name = value.name;
- objImage.transform.LocalIdentity();
- SettingTransform(value.ObjectTransform);
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Video:
- var video = loader.LoadSync<GameObject>("Resources/Template/Prefab/Video");
- GameObject objVideo = video.Instantiate();
- objVideo.GetComponent<VideoControl>().VideoURL = value.DownloadPath;
- objVideo.transform.SetParent(transform);
- objVideo.name = value.name;
- objVideo.transform.LocalIdentity();
- SettingTransform(value.ObjectTransform);
- UIKit.GetPanel<EditorPanel>().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent<SelectBJImage>());
- break;
- case MaterialType.Model:
- GameObject model = loader.LoadSync<GameObject>(value.DownloadPath);
- GameObject objModel = model.Instantiate();
- objModel.SetActive(true);
- objModel.transform.SetParent(transform);
- objModel.name = value.name;
- objModel.transform.LocalIdentity();
- SettingTransform(value.ObjectTransform);
- break;
- default:
- break;
- }
- }
- catch (System.Exception)
- {
- ErrorLogPanel.Instance.Show(" 加载本地素材出现错误 ");
- }
- }
- /// <summary>
- /// 设置Transform
- /// </summary>
- /// <param name="value"></param>
- private void SettingTransform(ObjectTransform value)
- {
- if (value.nowScale == Vector3.zero)
- {
- if (value.startScale == Vector3.zero)
- value.startScale = Vector3.one;
- transform.localPosition = value.startPos;
- transform.localEulerAngles = value.startRot;
- transform.localScale = value.startScale;
- Debug.Log("Start " + value.startPos.x);
- }
- else
- {
- transform.localPosition = value.nowPos;
- transform.localEulerAngles = value.nowRot;
- transform.localScale = value.nowScale;
- Debug.Log("now " + value.nowPos.x);
- }
- }
- }
|