用到的是 AndroidJavaProxy 这个类
public interface ExActivityListener
{
public void onRestart();
public void onStart();
public void onResume();
public void onPause();
public void onStop();
public void onActivityResult(int requestCode, int resultCode, Intent data);
public void onTest();
}
public class MainActivity extends UnityPlayerActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
private ExActivityListener listener;
public void setListener(ExActivityListener listener)
{
Log.v("Unity", "setListener(1)!------------");
this.listener = listener;
}
@Override
public void onRestart()
{
Log.v("Unity", "onRestart!------------");
super.onRestart();
if(listener != null) listener.onRestart();
}
@Override
public void onStart()
{
super.onStart();
if(listener != null) listener.onStart();
}
@Override
public void onResume()
{
super.onResume();
if(listener != null) listener.onResume();
}
@Override
public void onPause()
{
super.onPause();
if(listener != null) listener.onPause();
}
@Override
public void onStop()
{
if(listener != null) listener.onStop();
super.onStop();
}
public void onTest()
{
if(listener != null) listener.onTest();
super.onStop();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(listener != null) listener.onActivityResult(requestCode, resultCode, data);
}
public String test()
{
Log.v("测试","成功");
onTest();
return "测试成功";
}
}
Unity代码
public class ActivityListener : AndroidJavaProxy
{
public ActivityListener() : base("com.test.mytest.ExActivityListener")
{
}
public void onRestart()
{
UnityEngine.Debug.LogError("Back to Unity onRestart");
}
public void onStart()
{
UnityEngine.Debug.LogError("Back to Unity onStart");
}
public void onResume()
{
UnityEngine.Debug.LogError("Back to Unity onResume");
}
public void onPause()
{
UnityEngine.Debug.LogError("Back to Unity onPause");
}
public void onStop()
{
UnityEngine.Debug.LogError("Back to Unity onStop");
}
public void onActivityResult(int requestCode, int resultCode, AndroidJavaObject data)
{
UnityEngine.Debug.LogError("onActivityResult");
}
public void onTest()
{
UnityEngine.Debug.LogError("Back to Unity OnTest");
}
}
void Awake()
{
AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
activity.Call("setListener", new ActivityListener());
UnityEngine.Debug.LogError("Awake");
}
void Start()
{
AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
string value2 = activity.Call<string>("test");
Debug.Log (value2);
}
}
网友评论