赞
踩
apply plugin: 'com.android.library' android { compileSdkVersion 25 buildToolsVersion "25.0.0" defaultConfig { minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile files('libs/classes.jar') }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.wangby.Android" >
-
- <application
- android:allowBackup="true"
- android:label="@string/app_name"
- android:supportsRtl="true" >
- <activity android:name="com.wangby.Android.MainActivity" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
- </activity>
- </application>
-
- </manifest>
package com.wangby.Android; //import android.support.v7.app.AppCompatActivity;//这里不需要引用这个android库文件 import android.os.Bundle; import com.unity3d.player.UnityPlayerActivity; import android.app.AlertDialog; import android.widget.Toast; import android.os.Vibrator; import com.unity3d.player.UnityPlayer; public class MainActivity extends UnityPlayerActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main);//注释掉这句话,这句话的意思是:启动界面是Android中的这个activity_main } // 调用弹出Android一个窗口 public String ShowDialog(final String _title, final String _content){ runOnUiThread(new Runnable() { @Override public void run() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle(_title).setMessage(_content).setPositiveButton("Down", null); builder.show(); } }); return "Java return"; } // 定义一个显示Toast的方法,在Unity中调用此方法 public void ShowToast(final String mStr2Show) { // 同样需要在UI线程下执行 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(),mStr2Show, Toast.LENGTH_LONG).show(); } }); } // 定义一个手机振动的方法,在Unity中调用此方法 public void SetVibrator() { Vibrator mVibrator=(Vibrator)getSystemService(VIBRATOR_SERVICE); mVibrator.vibrate(new long[]{200, 2000, 2000, 200, 200, 200}, -1); //-1:表示不重复 0:循环的震动 } // arg1:unity中的对象名字,记住是对象名字,不是脚本类名 // arg2: 函数名 // arg3: 传给函数的参数,目前只看到一个参 数,并且是string的,自己传进去转吧 public void callUnityFunc(String _objName , String _funcStr, String _content) { UnityPlayer.UnitySendMessage(_objName, _funcStr, _content); } }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
-
- public class AndroidTest : MonoBehaviour {
- [SerializeField] private Text _label = null;
-
- // Use this for initialization
- void Start () {
-
- }
-
- // Update is called once per frame
- void Update () {
-
- }
-
- public void myShowDialog () {
- print ("myShowDialog_1");
- AndroidJavaClass androidJavaClass = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
- AndroidJavaObject androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject> ("currentActivity");
-
- string [] mObject = new string [2];
- mObject [0] = "小菜菜";
- mObject [1] = "大春绿";
- string ret = androidJavaObject.Call<string> ("ShowDialog", mObject);
- setMessage (ref ret);
- print ("myShowDialog_2");
- }
- public void myShowToast () {
- print ("myShowToast_1");
- AndroidJavaClass androidJavaClass = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
- AndroidJavaObject androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject> ("currentActivity");
- androidJavaObject.Call ("ShowToast", "你是不是傻");
- print ("myShowToast_2");
- }
- /// <summary>
- /// arg0: 调用android中的方法
- /// arg1: 对象名
- /// arg2: 函数名
- /// arg3: 参数1
- /// </summary>
- public void myInteraction () {
- print ("myInteraction_1");
- AndroidJavaClass androidJavaClass = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
- AndroidJavaObject androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject> ("currentActivity");
- androidJavaObject.Call ("callUnityFunc", "ReceiveObject", "receiveMessage", "此系统表示不喜欢你,并表示你长得很丑...");
- print ("myInteraction_2");
- }
- public void receiveMessage (string message) {
- print ("receiveMessage_:" + message);
- setMessage (ref message);
- }
-
- public void setMessage (ref string content) {
- print ("setMessage_:" + content);
- _label.text = content;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。