ProjectionAPI.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. namespace EZXR.Glass.Projection
  7. {
  8. public class ProjectionAPI
  9. {
  10. static AndroidJavaObject instance;
  11. static AndroidJavaObject Instance
  12. {
  13. get
  14. {
  15. if (instance == null)
  16. {
  17. instance = new AndroidJavaObject("com.ezxr.glass.projectionlib.API");
  18. }
  19. return instance;
  20. }
  21. }
  22. /// <summary>
  23. /// 启动应用时调用,先创建API对象,再调用,非单例模式
  24. /// </summary>
  25. /// <param name="callBack_OnStart"></param>
  26. /// <param name="callBack_OnStop"></param>
  27. public static void Init(Action<int> callBack_OnStart, Action callBack_OnStop)
  28. {
  29. #if UNITY_EDITOR
  30. #else
  31. ProjectionCallback projectionCallback = new ProjectionCallback(callBack_OnStart, callBack_OnStop);
  32. AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  33. AndroidJavaObject unityActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
  34. Instance.Call("init", unityActivity, projectionCallback);
  35. #endif
  36. }
  37. // 退出应用时调用
  38. public static void DeInit()
  39. {
  40. #if UNITY_EDITOR
  41. #else
  42. Instance.Call("deInit");
  43. #endif
  44. }
  45. // 收到投屏/录屏回调时调用,需要在render线程里面调用该接口
  46. // textureId:需要共享的纹理Id
  47. // width:纹理宽度(目前在投录屏server已写死1280×960,该字段暂无使用)
  48. // height:纹理高度(目前在投录屏server已写死1280×960,该字段暂无使用)
  49. public static void Start(int textureId, int width, int height)
  50. {
  51. #if UNITY_EDITOR
  52. #else
  53. Instance.Call("start", textureId, width, height);
  54. #endif
  55. }
  56. // 普通3D应用切换到后台时调用
  57. public static void Stop()
  58. {
  59. #if UNITY_EDITOR
  60. #else
  61. Instance.Call("stop");
  62. #endif
  63. }
  64. /// <summary>
  65. /// 通知投录屏服务器刷新
  66. /// </summary>
  67. /// <param name="timestamp">当前帧时间戳(目前该字段暂无使用,后面可优化)</param>
  68. public static void NotifyNewFrame(long timestamp)
  69. {
  70. #if UNITY_EDITOR
  71. #else
  72. Instance.Call("notifyNewFrame", timestamp);
  73. #endif
  74. }
  75. }
  76. }