--------------------------------------------------C# call java---------------------------------------------------
using RininRes;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace XXX
{
public class XXMgr:RSSingleton<XXMgr>
{
public static AndroidJavaClass jc;
public AndroidJavaObject jo;
public bool callAds = false;
private static string JAVA_CLASS_Name = "com.xxx.cr.MainActivity";
public void Init()
{
CallAdsFunc();
}
private void CallAdsFunc()
{
try
{
jo = new AndroidJavaClass(JAVA_CLASS_Name);
jo.CallStatic("init");
//调用Java方法
callAds = true;
}
catch (System.Exception ex)
{
callAds = false;
}
}
public void SendLogin()
{
SendEvent("cb_open");
SendEvent("cb_login");
if (GameMgr.Instance.Level <= 0)
{
SendEvent("cb_register");
}
}
public void SendEvent(string eventId)
{
try
{
jo.CallStatic("SendEvent", eventId);
}
catch (System.Exception e)
{
Debug.LogError(string.Format("Call SendEvent****", e));
}
}
public void SendPay()
{
try
{
jo.CallStatic("pay");
}
catch (System.Exception e)
{
Debug.LogError(string.Format("Call SendEvent****", e));
}
}
}
}
----------------------------------------java-----------------------------------------------------------
public class MainActivity {
private static Activity unityActivity;
private static Context context;
public static void init(){
getActivity();
}
private static Activity getActivity(){
if(null == unityActivity) {
try {
Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
unityActivity = activity;
context = activity;
initSdk();
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (IllegalAccessException e) {
System.out.println(e.getMessage());
} catch (NoSuchFieldException e) {
System.out.println(e.getMessage());
}
}
return unityActivity;
}
public static void JavaCallUnityFunc(String mge){
//参数1:参数1表示发送游戏对象的名称 参数2:方法名 参数3:要传递的参数
UnityPlayer.UnitySendMessage("Main Camera", "InitCallback", mge);
}
-------------------------java call C#----------------------------
public void InitCallback(string code)
{
Debug.Log("Call UNITY***"+ code);
}