using LitJson; using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; public class CastScreen : MonoBehaviour { [DllImport("NativeCameraPlugin")] public static extern void SetTextureLocal(System.IntPtr texture); [DllImport("NativeCameraPlugin")] private static extern IntPtr GetLocalEventFunc(); private AndroidJavaClass jc; public Camera ca; private AndroidJavaObject CurrentActivity; // Start is called before the first frame update void Start() { jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); CurrentActivity = jc.GetStatic("currentActivity"); Invoke("init",1f); } private RenderTexture renderTexture; public int TextureID; SDKCallbackListener aaa; public TClient tcp; //public TextMesh fpsobj; //WebSocketClient wc; public void init() { #if UNITY_ANDROID && !UNITY_EDITOR tcp.InitSocket(); renderTexture = new RenderTexture(CustomInfo.mWidth, CustomInfo.mHight, 1); ca.targetTexture = renderTexture; aaa = new SDKCallbackListener(); CurrentActivity.Call("initSDK2", aaa); // wc = WebSocketClient.webSockets["castScreen"]; // wc.OnMessaged += scoketMessage; tcp.OnMessaged += scoketMessage; //CurrentActivity.Call("InitTouPin", "test2"); // Debug.Log("连接init"); // Invoke("StartCast",1f); #endif } public void close() { CurrentActivity.Call("closeTou"); TClient.Instance.SocketQuit(); } public bool isInit; public void scoketMessage(string msg) { Debug.Log(msg+ "scoketMessage接收到的信息=====<[lgs]"); //textMesh.text = msg; CurrentActivity.Call("onMessage", msg); try { JsonData data = JsonMapper.ToObject(msg); { if (data["type"].ToString() == "View") { sendInfo = data["data"]; ChangeView(sendInfo); } else if (data["type"].ToString() == "RePos") { Debug.Log("[lgs] " + data.ToString()); ReLocation(); } else if (data["type"].ToString() == "init") { CurrentActivity.Call("InitTouPin", "test2"); Debug.Log("连接init"); Invoke("StartCast", 1f); } else { } } } catch { } } //public TextMesh textMesh; private IEnumerator CallPluginAtEndOfFrames() { while (true) { if (TextureID != 0) { // RenderTexture.active = renderTexture; // float t = Time.time; GL.IssuePluginEvent(GetLocalEventFunc(), TextureID); // UnityEngine.Debug.Log(string.Format("totalUnity消耗时间: {0} ms", Time.time - t)); } // skip one frame yield return new WaitForSeconds(0.013f); } } public MeshRenderer mr; public void StartCast() { StopCoroutine("CallPluginAtEndOfFrames"); StartCoroutine("CallPluginAtEndOfFrames"); int width = renderTexture.width; int height = renderTexture.height; TextureID = renderTexture.GetNativeTexturePtr().ToInt32(); mr.material = (Material)Resources.Load("renderMat"); mr.material.mainTexture = renderTexture; SetTextureLocal(renderTexture.GetNativeTexturePtr()); CurrentActivity.Call("touping", "0001", renderTexture.GetNativeTexturePtr().ToInt32(), width, height); // CurrentActivity.Call("changeVideo", width, height, 60); Debug.Log("房间开启。。。"); JsonData initTcp = new JsonData(); initTcp["action"] = "init"; initTcp["data"] = "init"; TClient.Instance.SocketSend(initTcp.ToJson()); } public void changeSize() { if (renderTexture != null) renderTexture.Release(); renderTexture = new RenderTexture(CustomInfo.mWidth, CustomInfo.mHight, 1); ca.targetTexture = renderTexture; mr.material.mainTexture = renderTexture; int width = renderTexture.width; int height = renderTexture.height; TextureID = renderTexture.GetNativeTexturePtr().ToInt32(); CurrentActivity.Call("changeSize", width, height, TextureID); } public Transform tf; JsonData sendInfo; JsonData sendInfo2; JsonData data = new JsonData(); JsonData data2 = new JsonData(); public GameObject backGround; public List poslist = new List(); public List eullist = new List(); private int delay = 7; // Update is called once per frame void Update() { poslist.Add(SvrManager.Instance.head.position); eullist.Add(SvrManager.Instance.head.eulerAngles); if(poslist.Count > delay) { poslist.RemoveAt(0); eullist.RemoveAt(0); } } private void ChangeView(JsonData sendInfo) { if (sendInfo != null) { JsonData data = sendInfo; if (data["method"].ToString() == "firstView") { backGround.SetActive(true); tf.position = poslist[0]; ca.transform.localRotation =Quaternion.Euler(-10,0,0); tf.eulerAngles = eullist[0]; } else if (data["method"].ToString() == "thirdView") { backGround.SetActive(false); ca.transform.localRotation = Quaternion.Euler(0, 0, 0); string[] strs = data["pose"].ToString().Split('_'); tf.position = new Vector3(float.Parse(strs[0]), float.Parse(strs[1]), float.Parse(strs[2])); tf.eulerAngles = new Vector3(float.Parse(strs[3]), float.Parse(strs[4]), float.Parse(strs[5])); //fpsobj.text = data["fps"].ToString(); } } } private void ReLocation() { data["action"] = "Relocation"; data["data"] = data2; data2["info"] = SvrManager.Instance.head.position.x + "_" + SvrManager.Instance.head.position.y + "_" + SvrManager.Instance.head.position.z + "_" + SvrManager.Instance.head.eulerAngles.x + "_" + SvrManager.Instance.head.eulerAngles.y + "_" + SvrManager.Instance.head.eulerAngles.z; TClient.Instance.SocketSend(data.ToJson()); Debug.Log("[lgs] sendPos"+data.ToString()); } private void OnDestroy() { } }