1.eclipse中新建Android工程,添加MainActivity.java代码:
package com.example.test; import com.unity3d.player.UnityPlayer; import com.unity3d.player.UnityPlayerActivity; import android.os.Bundle; public class MainActivity extends UnityPlayerActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public void Send() { UnityPlayer.UnitySendMessage("aaa", "AndroidCallBack", "okokoko"); } }
MainActivity
从eclipse中导出jar包
2.Unity中,新建工程,
编写EclipseCall.cs代码如下:
using UnityEngine; using System.Collections; using UnityEngine.UI; using System; public class EclipseCall : MonoBehaviour { private AndroidJavaClass jc; private AndroidJavaObject jo; private Text text; private Button btn; //单例模式 private static EclipseCall _instance; public static EclipseCall Instance { get { if (_instance == null) { _instance = new EclipseCall(); } return _instance; } } void Start() { text = GameObject.Find("aaa").GetComponent<Text>(); btn = GameObject.Find("Button").GetComponent<Button>(); this.name = "aaa"; _instance = this; jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); } public void ClickBtn() { jo.Call("Send"); } public void AndroidCallBack(String str) { text.text = str; } }
EclipseCall
将eclipse中导出的jar包拖进libs文件中
打包:
运行,点击后的UI:
原文地址:https://www.cnblogs.com/shirln/p/8423561.html
时间: 2024-11-05 22:50:45