using LitJson;
using Newtonsoft.Json.Linq;
using SC.XR.Unity;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;
using Blue;
using UnityEngine.Networking;
using XRTool.Util;
public class SceneChoose : BaseUI
{
public CustomHorizintalScrollView view;
private Button m_LeftBtn;
private Button m_RightBtn;
private Button m_OkBtn;
private GameObject m_SceneItem;
private Transform m_ScenePanel;
///
/// 场景选择面板中确认的场景
///
private SceneValue m_CurrentData;
private List m_Items;
private CarouselImage m_CarImage;
private List m_SceneData;
public static int Sid = -1;
protected override void OnAwake()
{
base.OnAwake();
m_SceneData = new List();
m_Items = new List();
m_CurrentData = null;
m_LeftBtn = CacheTransform.Find("Left").GetComponent();
m_RightBtn = CacheTransform.Find("Right").GetComponent();
m_OkBtn = CacheTransform.Find("OK").GetComponent();
m_SceneItem = CacheTransform.Find("SceneChooseItem").gameObject;
m_ScenePanel = CacheTransform.Find("ScenePanel");
m_ScenePanel.GetComponent().sizeDelta = new Vector2(400, 150);
Debug.Log("CustomHorizintalScrollView " + view.name);
//m_LeftBtn.onClick.AddListener(ONClickLeftBtn);
//m_RightBtn.onClick.AddListener(ONClickRightBtn);
//m_OkBtn.onClick.AddListener(OnClickOKBtn);
}
#region UI监听事件
private void ONClickLeftBtn()
{
if (m_CarImage != null)
{
m_CarImage.MoveToIndex(m_CarImage.CurrentIndex + 1);
}
}
private void ONClickRightBtn()
{
if (m_CarImage != null)
{
m_CarImage.MoveToIndex(m_CarImage.CurrentIndex - 1);
}
}
public void OnClickOKBtn(SceneValue m_CurrentData)
{
// GetComponent().position = new Vector3(1000000,42.1f,0);
this.m_CurrentData = m_CurrentData;
MQTTClient.Instance.SubscribeScene(this.m_CurrentData.id.ToString());
Sid = this.m_CurrentData.id;
//根据场景数据,下载资源
Debug.Log("选择的场景为:" + m_CurrentData.name);
SendSceneDetail detail = new SendSceneDetail();
detail.id = m_CurrentData.id;
string str = JsonConvert.SerializeObject(detail);
this.GetModel().sceneID = m_CurrentData.id;
this.SendCommand(new PointFileGetUrlCommand(detail.id, PointFileType.json));
this.SendCommand(new PointFileGetUrlCommand(detail.id, PointFileType.bytes));
this.SendCommand(new PointPosRotDownloadCommand(detail.id));
this.SendCommand(new MinMapGetSetCommand());
HttpTool.Instance.PostTest("/project/detail", str, (mes) =>
{
if (mes == "UnityWebRequest Error") return;
JObject jObject = JObject.Parse(mes);
if (jObject["code"].ToString() == "200")
{
mes = jObject["data"].ToString();
SceneValue scene = JsonConvert.DeserializeObject(mes);
Debug.Log("scene.backgroundImages===>" + mes);
Debug.Log("scene.backgroundImages===>"+ scene.backgroundImages.Count);
GameManager.Instance.MapPicUrl = scene.backgroundImages[0].base64;
GameManager.Instance.GetMinMap(scene, (bool back) =>
{
Debug.Log("scene.backgroundImages===>back"+ back);
if (back)
{
GetSNInit();
this.SendCommand(new GetReferencePosCommand(detail.id));
}
else
{
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), scene+" 通讯出错 请尝试重启应用");
m_OkBtn.interactable = true;
}
} );
}
else
{
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "/project/detail 通讯出错 请尝试重启应用");
m_OkBtn.interactable = true;
}
});
/*
if (m_CurrentData != null)
{
}
else
{
///Module_Notice.getInstance.SetNoticeInfo("未选择导览场景", "请先勾选要进入的场景", NoticeType.Normal, 1.7f);
//Module_Notice.getInstance._Follower.WindowAnchor = TextAnchor.UpperCenter;
//Module_Notice.getInstance.StartNotice(3f);
// this.SendCommand(new InstantiateCommand(InstantiateSystem.Instance.BlueObject.WarningPopUp));
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "请选择场景");
TimerMgr.Instance.CreateTimer(()=> {
UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
m_OkBtn.interactable = true;
},1f);
}
*/
}
private IEnumerator LoadCRT()
{
#if UNITY_ANDROID
string streamingAssetsPath = "jar:file://" + Application.dataPath + "!/assets/";
string fileName = "";
string filePath = Path.Combine(streamingAssetsPath, fileName);
Debug.Log("DGJ ===> filePath " + filePath);
using (WWW www = new WWW(filePath))
{
yield return www;
byte[] bytes = www.bytes;
// MQTTClient.Instance.bytes = bytes;
Debug.Log("DGJ ===> bytes " + bytes.Length);
}
#endif
}
private void GetSNInit()
{
// 选择场景
SendInit send = new SendInit();
send.sn = SendSN.GetSN();
//send.sn = API_GSXR_Slam.SlamManager.plugin.SN;
send.projectId = m_CurrentData.id;
string jsonString = JsonConvert.SerializeObject(send);
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.sceneChoose);
//通过SN获取场景数据
HttpTool.Instance.PostTest("/sn/init", jsonString, (message) =>
{
if (message == "UnityWebRequest Error") return;
Debug.Log("SN:" + send.sn);
Debug.Log("通过SN获取场景数据:" + message);
if (!string.IsNullOrWhiteSpace(message))
{
Debug.Log("Hjj 当前选择场景数据 1: " + message);
JsonData jobject = JsonMapper.ToObject(message);
if (!jobject.Keys.Contains("code") ||jobject["code"].ToJson() == "400")
{
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "/sn/init 通讯出错 请尝试重启应用");
}
else
{
message = jobject["data"]["info"].ToJson();
SceneValue scene = JsonConvert.DeserializeObject(message);
DataManager.Instance.CurrentScene = scene;
DataManager.Instance.ProjectID = m_CurrentData.id;
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.createScene);
StartCoroutine(GetSpoitValueDetail(scene));
}
}
else
{
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "/sn/init 通讯出错 请尝试重启应用");
}
});
}
private IEnumerator GetSpoitValueDetail(SceneValue scene)
{
var spoits = scene.listSpoit;
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "正在获取服务器景点信息");
for (int i = 0; i < spoits.Count; i++)
{
//通过ID获取场景详情
SendSceneDetail detail = new SendSceneDetail();
detail.id = spoits[i].id;
string jsonString = JsonConvert.SerializeObject(detail);
yield return StartCoroutine(HttpTool.Instance.SendHttp("/viewpoint/detail", jsonString, (message) =>
{
//Debug.Log("当前选择场景数据: " + message);
JObject jObject = JObject.Parse(message);
if (jObject["code"].ToString() == "200")
{
message = jObject["data"].ToString();
Debug.Log("Hjj jObjectjObjectjObject " + message);
SpoitValueDetail spoit = JsonConvert.DeserializeObject(message);
if(!GameManager.Instance.SpoitsValueDetail.ContainsKey(spoit.id))
GameManager.Instance.SpoitsValueDetail.Add(spoit.id, spoit);
else
{
GameManager.Instance.SpoitsValueDetail[spoit.id] = spoit;
}
}else
{
UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "/viewpoint/detail 通讯出错 请尝试重启应用");
}
}));
}
Debug.Log("Hjj scene.vuforiaXML" + scene.vuforiaXML);
if(UserInfo.Instance.is20)
{
string xmlName = Path.GetFileName(scene.vuforiaXML);
if (xmlName != null)
{
xmlName = xmlName.Substring(0, xmlName.Length - 4);
AppConfigConst.IMAGE_TARGET_FILE_NAME = xmlName;
}
if (scene.vuforiaXML != null)
{
if(Directory.Exists(Application.persistentDataPath + "/StreamingAssets"))
Directory.Delete(Application.persistentDataPath + "/StreamingAssets");
//DownloadData xml = new DownloadData();
//string filename = Path.GetFileName(scene.vuforiaXML);
//xml.name = filename;
//xml.type = 8;
//xml.downloadPath = scene.vuforiaXML;
//xml.updateTime = (int)scene.updateTime;
//xml.localSavePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + filename;
//DownloadManager.Instance.AddDownloadData(xml);
//DownloadData dat = new DownloadData();
//string filename2 = Path.GetFileName(scene.vuforiaDat);
//dat.name = filename2;
//dat.type = 8;
//dat.downloadPath = scene.vuforiaDat;
//dat.updateTime = (int)scene.updateTime;
//dat.localSavePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + filename2;
//DownloadManager.Instance.AddDownloadData(dat);
DownLoadMaterial xml = new DownLoadMaterial();
string filename = Path.GetFileName(scene.vuforiaXML);
// xml.name = filename;
xml.type = "-2";
xml.downLoadPath = scene.vuforiaXML;
xml.updataTime = scene.updateTime;
xml.localLoadPath = Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia." + filename.Split('.')[1];
// DownloadManager.Instance.AddDownloadData(xml);
DownloadResManager.Instance.DownLoad(xml);
DownLoadMaterial dat = new DownLoadMaterial();
string filename2 = Path.GetFileName(scene.vuforiaDat);
//dat.name = filename2;
dat.type = "-2";
dat.downLoadPath = scene.vuforiaDat;
dat.updataTime = (int)scene.updateTime;
dat.localLoadPath = Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia." + filename2.Split('.')[1];
DownloadResManager.Instance.DownLoad(dat);
}
}
yield return new WaitForSeconds(1f);
while(GameManager.Instance.isShowDownTip)
{
yield return null;
}
Debug.Log("创建场景123");
//创建场景
StartCoroutine(GameManager.Instance.CreateScene(scene));
Hide();
}
public RectTransform scrollview;
private void UpdateSceneItem()
{
Debug.Log("UpdateSceneItem1");
if (m_SceneData == null)
{
return;
}
ClearItem();
Debug.Log("UpdateSceneItem2");
for (int i = 0; i < m_SceneData.Count; i++)
{
Debug.Log("UpdateSceneItem21");
var obj = Instantiate(m_SceneItem);
Debug.Log("UpdateSceneItem22");
obj.transform.SetParent(m_ScenePanel);
var item = obj.GetComponent();
obj.gameObject.SetActive(true);
obj.transform.localScale = new Vector3(1, 1, 1);
obj.transform.localEulerAngles = Vector3.zero;
item.OnSelectScene += OnClickSceneItem;
item.SceneData = m_SceneData[i];
m_Items.Add(item);
}
switch(m_SceneData.Count)
{
case 1:
scrollview.sizeDelta = new Vector2(200,266);
break;
case 2:
scrollview.sizeDelta = new Vector2(500, 266);
break;
}
Debug.Log("UpdateSceneItem3");
if (m_CarImage == null)
{
m_CarImage = m_ScenePanel.gameObject.AddComponent();
}
Debug.Log("UpdateSceneItem4");
Debug.Log("CustomHorizintalScrollView " + view.name);
view.Init(m_SceneData,this);
Debug.Log("UpdateSceneItem6");
m_CarImage.mCellSize = new Vector2(400, 150);
m_CarImage.mSpacing = new Vector2(0, 0);
m_CarImage.mAutoLoop = false;
m_CarImage.mDrag = true;
m_CarImage.mLoopSpaceTime = 5f;
m_CarImage.resizeChildren();
Debug.Log("UpdateSceneItem5");
}
private void OnClickSceneItem(SceneValue item, bool b)
{
m_CurrentData = b ? item : null;
}
#endregion
protected override void OnInit()
{
base.OnInit();
}
protected override void OnShow(object param)
{
base.OnShow(param);
m_SceneData = (List)param;
UpdateSceneItem();
}
protected override void OnHide()
{
base.OnHide();
}
///
/// 清除所有场景信息
///
private void ClearItem()
{
while (m_Items.Count > 0)
{
var item = m_Items[0];
m_Items.RemoveAt(0);
Destroy(item.gameObject);
}
}
}