2019-03-09 10:47:09 -05:00
|
|
|
package com.rexraphael.flutterunitywidget;
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
2019-03-26 17:06:17 -04:00
|
|
|
import android.os.Debug;
|
2019-03-09 10:47:09 -05:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import io.flutter.plugin.common.MethodCall;
|
|
|
|
import io.flutter.plugin.common.MethodChannel;
|
|
|
|
import io.flutter.plugin.common.PluginRegistry;
|
|
|
|
import io.flutter.plugin.platform.PlatformView;
|
|
|
|
|
|
|
|
@SuppressLint("NewApi")
|
2019-03-10 18:53:08 -04:00
|
|
|
public class FlutterUnityView implements PlatformView, MethodChannel.MethodCallHandler, UnityEventListener {
|
2019-03-09 10:47:09 -05:00
|
|
|
private final Context context;
|
|
|
|
UnityView unityView;
|
|
|
|
MethodChannel channel;
|
2019-03-26 17:06:17 -04:00
|
|
|
public final PluginRegistry.Registrar registrar;
|
|
|
|
static final String LOG_TAG = "FlutterUnityView";
|
|
|
|
public final Activity activity;
|
|
|
|
|
2019-03-09 10:47:09 -05:00
|
|
|
|
|
|
|
FlutterUnityView(Context context, PluginRegistry.Registrar registrar, int id) {
|
|
|
|
this.context = context;
|
|
|
|
this.registrar = registrar;
|
2019-03-26 17:06:17 -04:00
|
|
|
this.activity = registrar.activity();
|
|
|
|
|
2019-03-09 10:47:09 -05:00
|
|
|
unityView = getUnityView(registrar);
|
|
|
|
|
2019-03-26 17:06:17 -04:00
|
|
|
channel = new MethodChannel(registrar.messenger(), "unity_view_" + id);
|
2019-03-09 10:47:09 -05:00
|
|
|
|
|
|
|
channel.setMethodCallHandler(this);
|
2019-03-26 17:06:17 -04:00
|
|
|
UnityUtils.addUnityEventListener(this);
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-10 18:53:08 -04:00
|
|
|
public void onMethodCall(MethodCall methodCall, final MethodChannel.Result result) {
|
2019-03-09 10:47:09 -05:00
|
|
|
switch (methodCall.method) {
|
|
|
|
case "createUnity":
|
|
|
|
UnityUtils.createPlayer(registrar.activity(), new UnityUtils.CreateCallback() {
|
|
|
|
@Override
|
|
|
|
public void onReady() {
|
2019-03-26 17:06:17 -04:00
|
|
|
result.success(true);
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "isReady":
|
|
|
|
result.success(UnityUtils.isUnityReady());
|
|
|
|
break;
|
|
|
|
case "postMessage":
|
|
|
|
String gameObject, methodName, message;
|
|
|
|
gameObject = methodCall.argument("gameObject");
|
|
|
|
methodName = methodCall.argument("methodName");
|
|
|
|
message = methodCall.argument("message");
|
2019-03-26 17:06:17 -04:00
|
|
|
|
|
|
|
System.out.print(gameObject);
|
|
|
|
System.out.print(methodName);
|
|
|
|
System.out.print(message);
|
|
|
|
|
2019-03-09 10:47:09 -05:00
|
|
|
UnityUtils.postMessage(gameObject, methodName, message);
|
2019-03-26 17:06:17 -04:00
|
|
|
result.success(true);
|
2019-03-09 10:47:09 -05:00
|
|
|
break;
|
|
|
|
case "pause":
|
|
|
|
UnityUtils.pause();
|
2019-03-26 17:06:17 -04:00
|
|
|
result.success(true);
|
2019-03-09 10:47:09 -05:00
|
|
|
break;
|
|
|
|
case "resume":
|
|
|
|
UnityUtils.resume();
|
2019-03-26 17:06:17 -04:00
|
|
|
result.success(true);
|
2019-03-09 10:47:09 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result.notImplemented();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView() {
|
|
|
|
return unityView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void dispose() {
|
|
|
|
if (UnityUtils.isUnityReady()) {
|
|
|
|
UnityUtils.getPlayer().quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private UnityView getUnityView(PluginRegistry.Registrar registrar) {
|
|
|
|
final UnityView view = new UnityView(registrar.context());
|
2019-03-10 18:53:08 -04:00
|
|
|
// view.addOnAttachStateChangeListener(this);
|
2019-03-09 10:47:09 -05:00
|
|
|
|
|
|
|
if (UnityUtils.getPlayer() != null) {
|
|
|
|
view.setUnityPlayer(UnityUtils.getPlayer());
|
|
|
|
} else {
|
2019-03-26 17:06:17 -04:00
|
|
|
UnityUtils.createPlayer(this.activity, new UnityUtils.CreateCallback() {
|
2019-03-09 10:47:09 -05:00
|
|
|
@Override
|
|
|
|
public void onReady() {
|
|
|
|
view.setUnityPlayer(UnityUtils.getPlayer());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void restoreUnityUserState() {
|
|
|
|
// restore the unity player state
|
|
|
|
if (UnityUtils.isUnityPaused()) {
|
|
|
|
Handler handler = new Handler();
|
|
|
|
handler.postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (UnityUtils.getPlayer() != null) {
|
|
|
|
UnityUtils.getPlayer().pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 300); //TODO: 300 is the right one?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMessage(String message) {
|
2019-03-26 17:06:17 -04:00
|
|
|
getChannel().invokeMethod("onUnityMessage", message);
|
|
|
|
}
|
2019-03-09 10:47:09 -05:00
|
|
|
|
2019-03-26 17:06:17 -04:00
|
|
|
private MethodChannel getChannel() {
|
|
|
|
return channel;
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
2019-03-26 17:06:17 -04:00
|
|
|
|
|
|
|
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|