123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- using LitJson;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using UnityEngine;
- using UnityEngine.UI;
- //using Vuforia;
- public class PlayCast : MonoBehaviour
- {
- public GameObject rawImage;
- [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;
- public GameObject bgChar;
- private bool isRePos = false;
- // Start is called before the first frame update
- void Start()
- {
- // rimg.texture = rt;
- // return;
- Instance = this;
- Application.targetFrameRate = 30;
- jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- CurrentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
- // tempVuf.position = Vector3.zero;
- // tempVuf.eulerAngles = Vector3.zero;
- rimg.rectTransform.sizeDelta = new Vector2(Screen.width, Screen.width * 0.5625f);
- mr3.rectTransform.sizeDelta = new Vector2(Screen.width, Screen.width * 0.75f);
- for (int i = 0; i < delayInt; i++)
- {
- text2d.Add(new Texture2D(1920, 1080));
- }
- }
- public Text text;
- public static int fps;
- public static int fps2;
- // public RenderTexture renderTexture;
- public int TextureID;
- SDKCallbackListener aaa;
- // WebSocketClient wc;
- public TClient tcp;
- public void init(string ip)
- {
- // rimg.texture = rt;
- // return;
- isChoose = false;
- if (tcp != null)
- tcp.OnMessaged -= scoketMessage;
- CurrentActivity.Call("closeTou");
- Invoke("initStart", 1f);
- TClient.Instance.InitSocket(ip);
- }
- void initStart()
- {
- // rimg.texture = rt;
- // return;
- //if(wc==null)
- // {
- aaa = new SDKCallbackListener();
- CurrentActivity.Call("initSDK2", aaa);
- // wc = WebSocketClient.webSockets["castScreen"];
- tcp.OnMessaged += scoketMessage;
- JsonData initTcp = new JsonData();
- initTcp["action"] = "init";
- initTcp["data"] = "init";
- TClient.Instance.SocketSend(initTcp.ToJson());
- // }
- // Invoke("StartCast", 1f);
- }
- JsonData sendInfo = new JsonData();
- public void scoketMessage(string msg)
- {
- Debug.Log(msg + "scoketMessage接收到的信息");
- CurrentActivity.Call("onMessage", msg);
- try
- {
- JsonData datamsg = JsonMapper.ToObject(msg);
- {
- if (datamsg["type"].ToString() == "RePos")
- {
- sendInfo = datamsg["data"];
- isRePos = true;
- // RePos(sendInfo);
- }
- else if (datamsg["type"].ToString() == "init")
- {
- CurrentActivity.Call("InitTouPin", "test3");
- Invoke("StartCast", 0.2f);
- }
- }
- }
- catch
- {
- }
- }
- 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(0.013f);
- }
- }
- Texture2D tex;
- public void StartCast()
- {
- StopCoroutine("CallPluginAtEndOfFrames");
- StartCoroutine("CallPluginAtEndOfFrames");
- // StopCoroutine(CalculateFramesPerSecond());
- // StartCoroutine(CalculateFramesPerSecond());
- 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);
- // bool focusModeSet = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
- isChoose = true;
- }
- public void closeSocket()
- {
- if (!bgChar.activeSelf)//如果连接成功,返回列表才断开连接
- {
- isChoose = false;
- CurrentActivity.Call("closeTou");
- TClient.Instance.SocketQuit();
- }
- }
- public RenderTexture rt;
- bool isdiyi;
- public void qiehuan()
- {
- isChoose = true;
- isdiyi = !isdiyi;
- CurrentActivity.Call("changeShijiao", isdiyi);
- rawImage.SetActive(isdiyi);
- }
- bool isChoose = false;
- public Transform tf;
- // Update is called once per frame
- public Camera ca2;
- JsonData data = new JsonData();
- JsonData data2 = new JsonData();
- JsonData datapos = new JsonData();
- JsonData datapos2 = new JsonData();
- // private float _framesPerSecond;
- //int FrameCount = 0;
- //private IEnumerator CalculateFramesPerSecond()//FPS
- //{
- // int lastFrameCount = 0;
- // while (true)
- // {
- // yield return new WaitForSecondsRealtime(1f);
- // var elapsedFrames = FrameCount - lastFrameCount;
- // _framesPerSecond = elapsedFrames / 1f;
- // // Debug.Log("FPS====>"+ _framesPerSecond.ToString());
- // lastFrameCount = FrameCount;
- // }
- //}
- public Texture2D textBG;
- private int nowFps;
- private int nowFps2;
- private float timefloat;
- // PIXEL_FORMAT mPixelFormat = PIXEL_FORMAT.RGB888;
- public static Dictionary<string, Texture2D> imageList = new Dictionary<string, Texture2D>();
- private int nowIndex = -1;
- List<Texture2D> text2d = new List<Texture2D>();
- void Update()
- {
- if (aaa != null)
- {
- if (aaa.data != null && aaa.data != "")
- {
- JsonData datalis = JsonMapper.ToObject(aaa.data);
- if (datalis["action"].ToString() == "changeResolution")
- {
- int width = int.Parse(datalis["data"]["w"].ToString());
- int height = int.Parse(datalis["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)
- {
- if (isRePos)
- {
- isRePos = false;
- RePos(sendInfo);
- }
- //temptf.position = new Vector3(tf.position.x-tempVuf.position.x, tf.position.y - tempVuf.position.y, tf.position.z - tempVuf.position.z);
- //temptf.position = tf.position - tempVuf.position;
- //temptf.eulerAngles = new Vector3(tf.eulerAngles.x-tempVuf.eulerAngles.x, tf.eulerAngles.y - tempVuf.eulerAngles.y, tf.eulerAngles.z - tempVuf.eulerAngles.z);
- //temptf.eulerAngles = tf.eulerAngles - tempVuf.eulerAngles;
- 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;
- if (nowIndex >= 0&& text2d[nowIndex]!=null)
- {
- mr3.texture = text2d[nowIndex];
- nowIndex++;
- if (nowIndex >= text2d.Count)
- nowIndex = 0;
- }
-
- // qT2.Add(t2);
- //FrameCount++;
- // data2["Fps"] = string.Format("{0:F2}", _framesPerSecond);
- TClient.Instance.SocketSend(data.ToJson());
- }
- }
- else
- {
- mr3.texture = textBG;
- rimg.texture = textBG;
- }
- }
- }
- private int delayInt = 15;
- public static int isUpdate = 0;
- List<Texture2D> qT2 = new List<Texture2D>();
- public Transform ArParent;
- private Transform tempVuf;
- private Transform temptf;
- public RawImage mr3;
- public void Relocation()
- {
- datapos["action"] = "Relocation";
- datapos["data"] = datapos2;
- datapos2["info"] = "";
- TClient.Instance.SocketSend(datapos.ToJson());
- }
- private void RePos(JsonData sendInfo)
- {
- if (sendInfo["method"].ToString() == "Relocation")
- {
- //tempVuf.position = tf.position;
- // tempVuf.eulerAngles = tf.eulerAngles;
- string[] strs = sendInfo["pose"].ToString().Split('_');
- ArParent.position = new Vector3(float.Parse(strs[0]), float.Parse(strs[1]), float.Parse(strs[2]));
- ArParent.eulerAngles = new Vector3(float.Parse(strs[3]), float.Parse(strs[4]), float.Parse(strs[5]));
- }
- }
- }
|