123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- using LitJson;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using UnityEngine;
- using UnityEngine.UI;
- public class PlayCast : MonoBehaviour
- {
- [DllImport("NativeCameraPlugin")]
- public static extern void SetTextureFromUnity(System.IntPtr texture);
- [DllImport("NativeCameraPlugin")]
- private static extern IntPtr GetRenderEventFunc();
- private AndroidJavaClass jc;
- public static PlayCast Instance;
- private AndroidJavaObject CurrentActivity;
- // Start is called before the first frame update
- void Start()
- {
- Instance = this;
- Application.targetFrameRate = 60;
- jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- CurrentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
- }
- // public RenderTexture renderTexture;
- public int TextureID;
- SDKCallbackListener aaa;
- // WebSocketClient wc;
- public TClient tcp;
- public void init(string ip)
- {
- isChoose = false;
- if (tcp!=null)
- tcp.OnMessaged -= scoketMessage;
- CurrentActivity.Call("closeTou");
- Invoke("initStart", 1f);
- TClient.Instance.InitSocket(ip);
- }
- void initStart()
- {
- //if(wc==null)
- // {
- aaa = new SDKCallbackListener();
- CurrentActivity.Call("initSDK2", aaa);
- // wc = WebSocketClient.webSockets["castScreen"];
- tcp.OnMessaged += scoketMessage;
- CurrentActivity.Call("InitTouPin", "172.16.15.42", "3300", "test3");
- // }
- Invoke("StartCast", 1f);
- }
- public void scoketMessage(string msg)
- {
- Debug.Log(msg + "scoketMessage接收到的信息");
- CurrentActivity.Call("onMessage", msg);
- }
- public RawImage rimg;
- private IEnumerator CallPluginAtEndOfFrames()
- {
- while (true)
- {
- if (TextureID != 0)
- {
- // float t = Time.time;
- GL.IssuePluginEvent(GetRenderEventFunc(), TextureID);
- // UnityEngine.Debug.Log(string.Format("totalUnity消耗时间: {0} ms", Time.time - t));
- }
- // skip one frame
- yield return new WaitForSeconds(1f / 60);
- }
- }
- Texture2D tex;
- public void StartCast()
- {
- StopCoroutine("CallPluginAtEndOfFrames");
- StartCoroutine("CallPluginAtEndOfFrames");
- int width = 1280;
- int height = 720;
- tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
- rimg.texture = tex;
- TextureID = tex.GetNativeTexturePtr().ToInt32();
- SetTextureFromUnity(tex.GetNativeTexturePtr());
- CurrentActivity.Call("jieshou", "0001", tex.GetNativeTexturePtr().ToInt32(), isdiyi);
- isChoose = true;
- }
- public void closeSocket()
- {
- CurrentActivity.Call("closeTou");
- TClient.Instance.SocketQuit();
- }
- bool isdiyi;
- public void qiehuan()
- {
- isChoose = true;
- isdiyi = !isdiyi;
- CurrentActivity.Call("changeShijiao", isdiyi);
- }
- bool isChoose = false;
- public Transform tf;
- // Update is called once per frame
- public Camera ca2;
- JsonData data = new JsonData();
- JsonData data2 = new JsonData();
- void Update()
- {
- if(aaa!=null)
- {
- if (aaa.data != null && aaa.data != "")
- {
- JsonData data = JsonMapper.ToObject(aaa.data);
- if (data["action"].ToString() == "changeResolution")
- {
- Debug.Log("更改action");
- int width = int.Parse(data["data"]["w"].ToString());
- int height = int.Parse(data["data"]["h"].ToString());
- tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
- TextureID = tex.GetNativeTexturePtr().ToInt32();
- CurrentActivity.Call("changeTexturID", tex.GetNativeTexturePtr().ToInt32());
- rimg.texture = tex;
- }
- aaa.data = "";
- }
- if (isChoose)
- {
- if(isdiyi)
- {
- data["action"] = "firstView";
- data["data"] = data2;
- data2["info"] = "";
- TClient.Instance.SocketSend(data.ToJson());
- }
- else if(!isdiyi)
- {
- data["action"] = "thirdView";
- data["data"] = data2;
- data2["info"] = tf.position.x + "_" + tf.position.y + "_" + tf.position.z + "_" + tf.eulerAngles.x + "_" + tf.eulerAngles.y + "_" + tf.eulerAngles.z;
- TClient.Instance.SocketSend(data.ToJson());
- }
- }
- }
- }
- }
|