1. 유니티 스크립트 작성
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class test : MonoBehaviour {
public Text screentext;
private static AndroidJavaObject _plugins;
// Use this for initialization
void Start () {
AndroidJNI.AttachCurrentThread();
AndroidJavaClass Ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
_plugins = Ajc.GetStatic<AndroidJavaObject>("currentActivity");
screentext.text += _plugins.ToString();
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
}
public void keyreceive(string keyevent)
{
screentext.text += "\n" + keyevent;
}
public void sendToast()
{
_plugins.Call("ToastMessage");
}
}
스마트폰 버튼 조작 (볼륨, 메뉴 등) : 안드로이드에서 유니티로 메세지 전송하여 keyreceive 함수 호출 -> 화면에 버튼 텍스트 값 출력
유니티에 띄어진 메뉴 조작 -> sendToast 함수 호출 -> 안드로이드로 ToastMessage call -> Toast 출력
2. PlayerSetting 에서 패키지명을 수정
3. 안드로이드 스튜디오 프로젝트 작성 (위에서 설정한 것과 같게.)
4. 유니티\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Release\Classes\classes.jar 추가
open Module Setting -> Modules -> Dependencies
5. Build Gradle 을 다음과 같이 수정
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile files('libs/classes.jar')
}
// task to delete the old jar
task deleteOldJar (type: Delete){
delete 'release/AndroidPlugin.jar'
}
// task to export contents as jar
task exportJar (type: Copy){
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
// Rename the jar
rename('classes.jar', 'AndroidPlugin.jar')
}
exportJar.dependsOn(deleteOldJar, build)
6. 안드로이드 코드 작성
package com.example.yhc50.withunitytest;
import android.os.Bundle;
import android.os.Message;
import android.os.Handler;
import android.view.KeyEvent;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity {
@Override
public void onCreate(Bundle savedInstancesState){
super.onCreate(savedInstancesState);
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
public boolean dispatchKeyEvent(KeyEvent event){
String keyCodeInt = String.valueOf(event.getKeyCode());
UnityPlayer.UnitySendMessage("Main Camera", "keyreceive", keyCodeInt);
return super.dispatchKeyEvent(event);
}
public void ToastMessage() {
handler.sendEmptyMessage(0);
}
}
7. Gradle -> ExportJar 로 출력
8. app\src\main\AndroidManifest.xml 내용 수정
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yhc50.withunitytest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
9. xml 과 Jar 을 유니티 프로젝트\Plugins\Android 로 복사
10. 유니티 빌드 후 실행. 끝
'#DevStudy > Unity' 카테고리의 다른 글
Unity <-> Node.js 통신 (Socket.io) (0) | 2016.09.30 |
---|---|
Unity <-> php 샘플 코드 (1) | 2016.08.28 |
www 클래스 세션 유지 관련 (0) | 2016.07.26 |
댓글