在 Unity 建立一個 Callback 讓 Android plugin 做完某事之後呼叫.
一、Eclipse
1. 建立一個 interface
例:
public interface CAdLocusListenerCallBack {
void onReceiveAd();
void onFailedToReceiveAd(int errorcode);
}
2. 把它加入至某 class 裡
例:
public class CAdLocus
{
//callback.
CAdLocusListenerCallBack listenerCB = null;
public void SetListenerCB(CAdLocusListenerCallBack cb)
{
listenerCB = cb;
}
}
3.做完事後呼叫 Callback
例:
public void onReceiveAd(Ad adView )
{
//AdLocusLayout llayout = (AdLocusLayout)adView;
if (listenerCB != null)
{
listenerCB.onReceiveAd();
}
}
public void onFailedToReceiveAd(Ad adView , com.adlocus.AdLocusLayout$ErrorCode errorCode)
{
if (listenerCB != null)
{
listenerCB.onFailedToReceiveAd(1);
}
}
二、Unity
1.建立 Callback 腳本
例:
public class AdLocusListenerCallBack : AndroidJavaProxy
{
public AdLocusListenerCallBack():base("com.ad.adlocus.CAdLocusListenerCallBack")
{
}
void onReceiveAd()
{
}
void onFailedToReceiveAd(int errorcode)
{
}
}
2.對 Plugin 註冊 Callback
例:
AndroidJavaClass player = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" );
AndroidJavaObject activity = player.GetStatic<AndroidJavaObject>("currentActivity");
AdLocusJo = new AndroidJavaObject("com.ad.adlocus.CAdLocus", activity);
AdLocusListenerCallBack AdListenerCB = new AdLocusListenerCallBack();
AdLocusJo.Call("SetListenerCB", AdListenerCB);
四、最後
也有人是直 sendmessage 的方式接調用 Unity 物件的函式,
例:
//unity
void gameobjectFunction(string parm)
{
}
//Java
UnityPlayer.UnitySendMessage("gameobjectName", "gameobjectFunction", "parms");
沒有留言:
張貼留言