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();
}
break;
case MaterialType.Text:
//string str = value.textContent;
//object loaderObj = str;
//LoadMaterial(loaderObj);
LoadLocalMaterial();
break;
default:
break;
}
}
///
/// 返回素材
///
///
private void HandleMsg(Msg msg)
{
try
{
LoadMaterial(msg.Value);
}
catch (System.Exception e)
{
ErrorLogPanel.Instance.Show(" 返回生成素材出现错误 " + e.Message);
}
}
int num = 0;
///
/// 加载素材
///
/// 返回加载物体
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(path);
GameObject objText = (GameObject)mText.Instantiate();
objText.GetComponent().SetText(loaderObj.ToString());
objText.transform.SetParent(transform);
objText.name = value.name;
objText.transform.LocalIdentity();
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().dicSelectBackImage.Add(spoitName+value.name, objText.GetComponent());
break;
case MaterialType.Image:
var image = loader.LoadSync("Resources/Template/Prefab/Image");
GameObject objImage = image.Instantiate();
objImage.GetComponent().SetImage((Sprite)loaderObj);
objImage.transform.SetParent(transform);
objImage.name = value.name;
objImage.transform.LocalIdentity();
UIKit.GetPanel().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent());
break;
case MaterialType.Video:
var video = loader.LoadSync("Resources/Template/Prefab/Video");
GameObject objVideo = video.Instantiate();
objVideo.GetComponent().VideoURL = loaderObj.ToString();
objVideo.transform.SetParent(transform);
objVideo.name = value.name;
objVideo.transform.LocalIdentity();
UIKit.GetPanel().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent());
break;
case MaterialType.Model:
List listobj = (List)loaderObj;
GameObject model = listobj[0];
Debug.Log(model.name);
GameObject objModel = null;
if (model.name == "Saloon")
{
objModel = GameManager.Instance.Saloon;
objModel.SetActive(true);
}
else
{
objModel = model.Instantiate();
}
if (model.name == "MainDargGames6")
{
GameManager.Instance.SZG.transform.SetParent(transform);
GameManager.Instance.SZG.transform.LocalIdentity();
}
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;
// }
//}
///
/// 加载本地素材
///
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(path);
GameObject objText = (GameObject)mText.Instantiate();
objText.GetComponent().SetText(value.textContent);
objText.transform.SetParent(transform);
objText.name = value.name;
objText.transform.LocalIdentity();
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);
// value.ObjectTransform.SetObjectTransform(transform);
}
UIKit.GetPanel().dicSelectBackImage.Add(spoitName + value.name, objText.GetComponent());
break;
case MaterialType.Image:
var image = loader.LoadSync("Resources/Template/Prefab/Image");
GameObject objImage = image.Instantiate();
objImage.GetComponent().SetImage(loader.LoadSync(value.DownloadPath));
objImage.transform.SetParent(transform);
objImage.name = value.name;
objImage.transform.LocalIdentity();
SettingTransform(value.ObjectTransform);
UIKit.GetPanel().dicSelectBackImage.Add(spoitName + value.name, objImage.GetComponent());
break;
case MaterialType.Video:
var video = loader.LoadSync("Resources/Template/Prefab/Video");
GameObject objVideo = video.Instantiate();
objVideo.GetComponent().VideoURL = value.DownloadPath;
objVideo.transform.SetParent(transform);
objVideo.name = value.name;
objVideo.transform.LocalIdentity();
SettingTransform(value.ObjectTransform);
UIKit.GetPanel().dicSelectBackImage.Add(spoitName + value.name, objVideo.GetComponent());
break;
case MaterialType.Model:
GameObject model = loader.LoadSync(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(" 加载本地素材出现错误 ");
}
//SettingTransform(value.ObjectTransform);
}
///
/// 设置Transform
///
///
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(value.nowScale.x + " Start " + value.startPos.x);
GameManager.Instance.text4.text = value.nowScale.x.ToString()+" "+ this.value.name + " " + value.nowPos.x.ToString();
}
else
{
transform.localPosition = value.nowPos;
transform.localEulerAngles = value.nowRot;
transform.localScale = value.nowScale;
Debug.Log("now " + value.nowPos.x);
GameManager.Instance.text4.text = value.nowScale.x.ToString() + " " + this.value.name + " " + value.nowPos.x.ToString();
}
}
}