| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace SmartBowLib.lebo
- {
- /*
- * 投屏工具
- */
- public class ScreenProjection : MonoBehaviour
- {
- //单例
- public static ScreenProjection ins;
- private static float _lastReleaseTime = -100; //上次销毁是什么时候
- //初始化-单例
- public static void Init() {
- if (ins) return;
- if (Time.realtimeSinceStartup - _lastReleaseTime < 0.5) return;
- ins = new GameObject("ScreenProjection").AddComponent<ScreenProjection>();
- DontDestroyOnLoad(ins);
- }
- private bool _released = false;
- //释放(销毁实例)
- public static void Release() {
- if (ins) {
- _lastReleaseTime = Time.realtimeSinceStartup;
- ins._released = true;
- Destroy(ins.gameObject);
- ins = null;
- }
- }
- void OnDestroy()
- {
- _lastReleaseTime = Time.realtimeSinceStartup;
- MsgReceiver.ins.onMessage -= onMessage;
- UnbindSDK();
- if (_objectScreenProjection != null) {
- // _objectScreenProjection.Call
- _objectScreenProjection.Dispose();
- }
- if (_classUnityPlayer != null) {
- _objectScreenProjection.Dispose();
- }
- if (_currentActivity != null) {
- _currentActivity.Dispose();
- }
- }
- void Start() {
- MsgReceiver.ins.onMessage += onMessage;
- InitSDK();
- }
- AndroidJavaObject _objectScreenProjection;
- AndroidJavaClass _classUnityPlayer;
- AndroidJavaObject _currentActivity;
- private bool _hasCallInitedSDK;
- void InitSDK() {
- MsgReceiver.ins.Log("在unity中初始化");
- _objectScreenProjection = new AndroidJavaObject("com.example.smartbowlib.lebo.ScreenProjection");
- _classUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- _currentActivity = _classUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
- _hasCallInitedSDK = true;
- _objectScreenProjection.Call("initSDK", _currentActivity);
- }
- void UnbindSDK() {
- if (_hasCallInitedSDK) {
- _objectScreenProjection.Call("unBindSdk");
- }
- }
- bool _browsing = false;
- public void StartBrowse() {
- if (!_authed) return;
- if (_browsing) return;
- _browsing = true;
- _objectScreenProjection.Call("startBrowse");
- }
- public void StopBrowse() {
- if (_browsing) {
- _browsing = false;
- _objectScreenProjection.Call("stopBrowse");
- }
- }
-
- string[] _connectingDevice;
- public void Connect(string uid, string name) {
- _connectingDevice = new string[]{uid, name};
- _objectScreenProjection.Call("connect", uid);
- }
- public void setPassword(string text) {
- _objectScreenProjection.Call("setPassword", text);
- }
- public void StartMirror() {
- _objectScreenProjection.Call("startMirror", mirrorResolutionID);
- }
- public void GetConnectList() {
- _objectScreenProjection.Call("getConnectList");
- }
- void onMessage(string tag, string msg) {
- if (_released) return;
- if (tag == "lebo.onAuthSuccess") onAuthSuccess();
- else if (tag == "lebo.onConnect") onConnect();
- else if (tag == "lebo.onDisconnect") onDisconnect();
- else if (tag == "lebo.onStart") onStart();
- else if (tag == "lebo.onStop") onStop();
- else if (tag == "lebo.onError") onError(msg);
- else if (tag == "lebo.onBrowseStop") onBrowseStop();
- }
- bool _authed = false;
- void onAuthSuccess() {
- _authed = true;
- StartBrowse();
- }
- void onBrowseStop() {
- _browsing= false;
- if (!HasDeviceInConnect()) {
- StartBrowse();
- }
- }
- string[] _connectedDevice;
- public static int mirrorResolutionID = 2;
- void onConnect() {
- _connectedDevice = _connectingDevice;
- _connectingDevice = null;
- StartMirror();
- }
- void onDisconnect() {
- _connectedDevice = null;
- _connectingDevice = null;
- }
- void onStart() {
- }
- void onStop() {
- }
- void onError(string text) {
- PopupMgr.ins.ShowTipTop(text);
- if (text == "请输入密码") {
- ScreenProjectionView.ins?.ShowInputPWD();
- }
- }
- //判断
- public bool HasDeviceInConnect() {
- return _connectingDevice != null || _connectedDevice != null;
- }
- }
- }
|