Unity Android iOS 交互(不用UnitySend
2016-12-12 本文已影响1017人
2b75747cf703
以实现弹窗为例。
C#代码
using UnityEngine;
using System.Runtime.InteropServices;
using System;
public class AlertView
{
public delegate void AlertViewDelegate(int buttonIndex);
#if UNITY_IOS
private static AlertViewDelegate alertViewDelegate;
[AOT.MonoPInvokeCallback(typeof(AlertViewDelegate))]
static void AlertViewCallback(int buttonIndex)
{
Debug.Log("AlertViewCallback " + buttonIndex);
if (alertViewDelegate != null)
alertViewDelegate(buttonIndex);
}
[DllImport("__Internal")]
private static extern void _showAlert(string title, string message, string okButton, string cancelButton, AlertViewDelegate callback);
#elif UNITY_ANDROID
class OnClickListener : AndroidJavaProxy
{
AlertViewDelegate alertViewDelegate = null;
public OnClickListener(AlertViewDelegate alertViewDelegate) : base("com.sinyee.babybus.plugins.PluginAlertDialog$OnClickListener")
{
this.alertViewDelegate = alertViewDelegate;
}
public void onClick(int buttonIndex)
{
alertViewDelegate(buttonIndex);
}
}
//static AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
//static void Run(Action action)
//{
// activity.Call("runOnUiThread", new AndroidJavaRunnable(action));
//}
#endif
public static void ShowAlert(string title, string message, string okButton, string cancelButton, AlertViewDelegate callback = null)
{
#if UNITY_IOS
alertViewDelegate = callback;
_showAlert(title, message, okButton, cancelButton, AlertViewCallback);
#elif UNITY_ANDROID
new AndroidJavaClass("com.sinyee.babybus.plugins.PluginAlertDialog").CallStatic("showAlertDialog", title, message, okButton, cancelButton, new OnClickListener(callback));
#endif
}
}
java代码
package com.sinyee.babybus.plugins;
import android.app.AlertDialog;
import android.content.DialogInterface;
import com.unity3d.player.UnityPlayer;
/**
* Created by Lin on 2016/11/22.
*/
public class PluginAlertDialog
{
public interface OnClickListener {
void onClick(int buttonIndex);
}
public static void showAlertDialog(final String title, final String message, final CharSequence yesString, final CharSequence noString, final OnClickListener onClickListener) {
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(UnityPlayer.currentActivity)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(title)
.setMessage(message)
.setPositiveButton(yesString, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (onClickListener != null)
onClickListener.onClick(which);
}
});
if (noString != null && !noString.equals("")) {
alertDialogBuilder.setNegativeButton(noString, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (onClickListener != null)
onClickListener.onClick(which);
}
});
}
alertDialogBuilder.show();
}
});
}
}
oc代码
//
// BBAlertView.h
// Unity-iPhone
//
// Created by Lin on 16/11/22.
//
//
#import <Foundation/Foundation.h>
@interface BBAlertView : NSObject<UIAlertViewDelegate>
- (void)showAlert:(const char *)title message:(const char*)message okButtonTitle:(const char*)okButton cancelButtonTitle:(const char*)cancelButton;
@end
//
// BBAlertView.m
// Unity-iPhone
//
// Created by Lin on 16/11/22.
//
//
#import "BBAlertView.h"
typedef void (*AlertViewDelegate)(int buttonIndex);
AlertViewDelegate g_alertViewDelegate;
void _showAlert(const char* title, const char* message, const char* okButton, const char* cancelButton, AlertViewDelegate alertViewDelegate)
{
g_alertViewDelegate = alertViewDelegate;
[[[BBAlertView alloc] init] showAlert:title message:message okButtonTitle:okButton cancelButtonTitle:cancelButton];
}
@implementation BBAlertView
- (void)showAlert:(const char *)title message:(const char*)message okButtonTitle:(const char*)okButton cancelButtonTitle:(const char*)cancelButton{
NSString* cancelButtonTitle = nil;
if(strcmp(cancelButton, "") != 0)
cancelButtonTitle = [NSString stringWithUTF8String:cancelButton];
NSString* okButtonTitle = nil;
if(strcmp(okButton, "") != 0)
okButtonTitle = [NSString stringWithUTF8String:okButton];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithUTF8String:title]
message:[NSString stringWithUTF8String:message]
delegate:self
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:okButtonTitle, nil];
[alert show];
[alert release];//ARC模式下不会回调UIAlertViewDelegate中的方法
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(g_alertViewDelegate != NULL)
g_alertViewDelegate((int)buttonIndex);
}
@end