AmlogicJarSend.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * File Name: AmlogicJarSend
  3. * Author: Guan B
  4. * Version: 1.0.0
  5. * Data: 2022.04.08
  6. * Des: 发送数据给Android端
  7. * Other: Zeewain
  8. */
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. using UnityEngine.UI;
  13. /*
  14. using static System.Net.Mime.MediaTypeNames;*/
  15. public class AmlogicJarSend : MonoBehaviour
  16. {
  17. public static AmlogicJarSend single;
  18. private AndroidJavaClass jc;
  19. private AndroidJavaObject jo;
  20. // public Button btn;
  21. /* public Text text;*/
  22. int num = 0;
  23. // byte[] photo;
  24. string str = "{\"body\":null,\"cmd\":100,\"seq\":1,\"state\":0}";
  25. string str2 = "{\"body\":null,\"cmd\":104,\"seq\":1,\"state\":0}";
  26. void Awake()
  27. {
  28. single = this;
  29. //print("已进入Awake");
  30. if (Application.platform == RuntimePlatform.WindowsEditor
  31. || Application.platform == RuntimePlatform.WindowsPlayer)
  32. {
  33. }
  34. if (Application.platform == RuntimePlatform.Android)// 使用Unity切换Platform无法模拟
  35. {
  36. print("开始调用AndroidJavaClass");
  37. //这两行是固定写法
  38. jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  39. jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
  40. // btn.onClick.AddListener(OnBtnClickHandler);
  41. print("成功调用AndroidJavaClass");
  42. // send("发送测试");
  43. send(str);
  44. send(str2);
  45. print("发送104协议");
  46. }
  47. }
  48. // public Webcam_Test web_tex;
  49. public void OnBtnClickHandler()
  50. {
  51. if (Application.platform == RuntimePlatform.Android)// 使用Unity切换Platform无法模拟
  52. {
  53. //调用Android中的方法UnityCallAndroid
  54. jo.Call("UnityCallAndroid");
  55. // num = jo.CallStatic<int>("GetNum");
  56. num = jo.Call<int>("Setting");
  57. }
  58. }
  59. public void send(string str)
  60. {
  61. print("开始发送消息给AndroidJavaClass" + str);
  62. jo.Call("OnUnityMsg", 1, str);
  63. print("成功发送消息给AndroidJavaClass");
  64. }
  65. }