Unity3d中SendMessage – Android和Unity的交互

    • UnityPlayer的SendMessage

SendMessage


// paramString1 表示挂载脚本的对象
// paramString2 表示脚本中调用的方法名
// paramString3 传参数
UnityPlayer.UnitySendMessage(paramString1, paramString2, paramString3);
    AndroidJavaClass和AndroidJavaObject

using UnityEngine;
using System.Collections;
public static class AndroidUtils {
	public static AndroidJavaClass unityOverrideClass;
	public static AndroidJavaClass UnityOverrideClass {
		get {
			if (unityOverrideClass == null) {
				unityOverrideClass = new AndroidJavaClass ("com.xxx.xxxx.xxxxActivity");
			}
			return unityOverrideClass;
		}
	}

	public static AndroidJavaObject SDK {
		get {
			return UnityOverrideClass.CallStatic ("GetSDK", new object[0]);
		}
	}
}

同时需要我们Android封装Jar包,以及AndroidManifest.xml,XML中定义的Activity所需要的res也需要。将这些内容放置到Plugins中:

Unity调用Android的方法如下


AndroidUtils.SDK.Call("AndroidMethodName", args);

AndroidManifest.xml示例

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.xx.xxxx" platformBuildVersionCode="22" platformBuildVersionName="5.1.1-1819727">
 <supports-screens android:largeScreens="true" android:normalScreens="false" android:requiresSmallestWidthDp="600" android:smallScreens="false" android:xlargeScreens="true"/>
 <uses-feature android:glEsVersion="0x00020000"/>
 <supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture"/>
 <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
 <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false"/>
 <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false"/>
 <uses-permission android:name="com.android.vending.CHECK_LICENSE"/>
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.WAKE_LOCK"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
 <uses-permission android:name="android.permission.BLUETOOTH"/>
 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
 <uses-permission android:name="android.permission.CAMERA"/>
 <android:uses-permission android:name="android.permission.READ_PHONE_STATE"/>
 <application android:allowBackup="true" android:icon="@drawable/app_icon" android:isGame="true" android:label="@string/app_name" android:largeHeap="true" android:name="com.xxx.xxxx.BLApplication" android:supportsRtl="true" android:theme="@style/UnityThemeSelector">
 <activity android:configChanges="locale|fontScale|keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode" android:exported="true" android:label="@string/app_name" android:launchMode="singleTask" android:name="com.xx.xxx.SDKNativeActivity" android:screenOrientation="sensorLandscape">
 <meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
 <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false"/>
 <intent-filter>
 <action android:name="android.intent.action.MAIN"/>
 <category android:name="android.intent.category.LAUNCHER"/>
 </intent-filter>
 </activity>
 </application>
</manifest> 

欢迎留言

avatar
  Subscribe  
Notify of