using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.WSA;
//using UnityEngine.XR.WSA.Sharing;
///
/// 导出锚点
///
public class ExportAnchorScript : MonoBehaviour
{
public string exportingAnchorName;
private List exportingAnchorBytes = new List();
WorldAnchor gameRootAnchor;
public Text uploadingTip;
public Text createTip;
public Text createTip2;
public Button uploadingBtn;
public Button createBtn;
public Button quitBtn;
public TextMeshProUGUI posText;
public Transform anchorPos;
public Transform anchorParent;
IEnumerator read()
{
WWW www = new WWW(Application.persistentDataPath + "/AnchorSetting.txt");
yield return www;
byte[] test = www.bytes;
exportingAnchorBytes = test.ToList();
}
void Start()
{
//StartCoroutine(read());
uploadingBtn.onClick.AddListener(() =>
{
uploadingBtn.interactable = false;
Invoke("ResetBtn", 5);
UpLoadingAnchor();
});
createBtn.onClick.AddListener(() =>
{
createBtn.interactable = false;
Invoke("ResetBtn", 5);
Send();
});
quitBtn.onClick.AddListener(() =>
{
UnityEngine.SceneManagement.SceneManager.LoadScene("LoginSenceMR");
});
}
private void Update()
{
posText.text = anchorPos.localPosition.ToString("f3")+"\n"+anchorPos.localEulerAngles.ToString("f3");
}
public void SetAnchorParent()
{
anchorPos.SetParent(anchorParent);
}
public void SetAnchor()
{
anchorPos.SetParent(null);
}
public void ResetBtn()
{
if (!createBtn.interactable) createBtn.interactable = true;
if (!uploadingBtn.interactable) uploadingBtn.interactable = true;
}
private void OnExportDataAvailable(byte[] data)
{
exportingAnchorBytes.AddRange(data);
createTip2.gameObject.SetActive(true);
createTip2.text = exportingAnchorBytes.ToArray().Length.ToString();
}
public void Send()
{
if (exportingAnchorBytes != null) exportingAnchorBytes.Clear();
gameRootAnchor = GetComponent();
if (gameRootAnchor!=null)
{
DestroyImmediate(gameRootAnchor);
}
//gameRootAnchor = gameObject.AddComponent();
//WorldAnchorTransferBatch transferBatch = new WorldAnchorTransferBatch();
//transferBatch.AddWorldAnchor(exportingAnchorName, this.GetComponent());
//WorldAnchorTransferBatch.ExportAsync(transferBatch, OnExportDataAvailable, OnExportComplete);
}
//private void OnExportComplete(SerializationCompletionReason completionReason)
//{
// createTip2.text = exportingAnchorBytes.ToArray().Length.ToString()+"\n"+ completionReason;
// if (completionReason == SerializationCompletionReason.Succeeded)
// {
// WriteAnchorSetting();
// }
// else
// {
// createTip.text = "生成失败";
// }
//}
private void WriteAnchorSetting()
{
FileStream sw = new FileStream(Application.persistentDataPath + "/AnchorSetting.txt", FileMode.OpenOrCreate, FileAccess.Write);
sw.Write(exportingAnchorBytes.ToArray(), 0, exportingAnchorBytes.ToArray().Length);
sw.Close();
sw.Dispose();
createTip.text = "生成成功";
createTip2.gameObject.SetActive(false);
}
public void UpLoadingAnchor()
{
StartCoroutine(MyNetMQClient.UpLoadFile("http://" + MyNetMQClient.CallIP + "/Handler/Files.ashx", exportingAnchorBytes.ToArray(), "AnchorSetting.txt", back =>
{
if (back.state)
{
uploadingTip.text = "上传成功";
}
else
{
uploadingTip.text = "上传失败";
}
}));
}
}