123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- 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<AndroidJavaObject>("currentActivity");
- Invoke("init",1f);
- }
- private RenderTexture renderTexture;
- public int TextureID;
- SDKCallbackListener aaa;
- public TClient tcp;
- //WebSocketClient wc;
- public void init()
- {
- tcp.InitSocket();
- renderTexture = new RenderTexture(1280, 720, 1);
- ca.targetTexture = renderTexture;
- aaa = new SDKCallbackListener();
- CurrentActivity.Call("initSDK2", aaa);
- // wc = WebSocketClient.webSockets["castScreen"];
- // wc.OnMessaged += scoketMessage;
- tcp.OnMessaged += scoketMessage;
- CurrentActivity.Call("InitTouPin", "192.168.50.189", "50001", "test2");
- Debug.Log("连接init");
- Invoke("StartCast",1f);
- }
- public void close()
- {
- CurrentActivity.Call("closeTou");
- TClient.Instance.SocketQuit();
- }
- public void scoketMessage(string msg)
- {
- Debug.Log(msg+ "scoketMessage接收到的信息=====<");
- //textMesh.text = msg;
- CurrentActivity.Call("onMessage", msg);
- try
- {
- JsonData data = JsonMapper.ToObject(msg);
- {
- if (data["type"].ToString() == "View")
- {
- sendInfo = data["data"];
- ChangeView(sendInfo);
- }
- }
- }
- catch
- {
- }
- }
- //public TextMesh textMesh;
- private IEnumerator CallPluginAtEndOfFrames()
- {
- while (true)
- {
- if (TextureID != 0)
- {
- // RenderTexture.active = renderTexture;
-
- // float t = Time.time;
- GL.IssuePluginEvent(GetLocalEventFunc(), TextureID);
- GL.Clear(true, true,new Color(0,0,0,0));
- // UnityEngine.Debug.Log(string.Format("totalUnity消耗时间: {0} ms", Time.time - t));
- }
- // skip one frame
- yield return new WaitForSeconds(1f / 60);
- }
- }
- 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("房间开启。。。");
- }
- public void changeSize()
- {
- if (renderTexture != null)
- renderTexture.Release();
- renderTexture = new RenderTexture(1280, 720,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;
- public GameObject backGround;
- // Update is called once per frame
- void Update()
- {
-
- }
- private void ChangeView(JsonData sendInfo)
- {
- if (sendInfo != null)
- {
- JsonData data = sendInfo;
- Debug.Log(sendInfo.ToString()+"%%%%%");
- if (data["method"].ToString() == "firstView")
- {
- Debug.Log("第一视角:");
- backGround.SetActive(true);
- tf.position = GSXRManager.Instance.head.position;
- tf.eulerAngles = GSXRManager.Instance.head.eulerAngles;
- }
- else if (data["method"].ToString() == "thirdView")
- {
- Debug.Log("第三视角:");
- backGround.SetActive(false);
- 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]));
- }
- }
- }
- }
|