노력과 삽질 퇴적물

유니티: 안드로이드 API 플러그인 본문

프로그래밍note/엔진 관련

유니티: 안드로이드 API 플러그인

MTG 2016. 4. 8. 22:52

* Unity 5.3.1f1 기준으로 작성됐습니다.

* 이클립스 기준입니다.






0. 필요한 파일


파일명

예시 경로

 classes.jar

(아래의 경로들에서 찾을수 있습니다.)

 (...Unity설치경로...)\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes

 (...Unity설치경로...)\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Release\Classes

 (...Unity설치경로...)\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Development\Classes

 (...Unity설치경로...)\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Development\Classes

 이클립스

 (생략)






1. 이클립스에서


1) 프로젝트 설정

> 프로젝트 생성시, 유니티에서 사용중인 패키지명과 동일하게 합니다.

> (프로젝트 우클릭)-Properties-Java Build Path


앞서 명시해둔 경로를 사용해서 classes.jar을 연결.



2) JAVA코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.xxxxxx.xxxxxx;//Unity 프로젝트와 동일한 패키지
 
import com.unity3d.player.UnityPlayerActivity;
 
import android.os.Bundle;
import android.widget.Toast;
 
 
public class JavaNative extends UnityPlayerActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate( savedInstanceState );
        //setContentView( R.layout.activity_main );
    }
    
    public void SetToast(final String msg)
    {
        this.runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText( JavaNative.this, msg, Toast.LENGTH_LONG ).show();
                }
            });
    }
}
cs



3) AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxxxxx.xxxxxx"
    android:installLocation="internalOnly"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <application
        android:theme="@android:style/Theme.NoTitleBar" 
        android:icon="@drawable/icon"
        android:label="MY_APP_NAME"
    >
        <activity
            android:name="com.unity3d.player.UnityPlayerActivity"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:label="MY_APP_NAME"
            android:screenOrientation="landscape" >
 
        </activity>
        <activity android:name="com.xxxxxx.xxxxxx.JavaNative" 
            android:label="MY_APP_NAME"
            android:icon="@drawable/icon"
            android:screenOrientation="landscape"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
            <!--android:launchMode="singleTask"-->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <activity
            android:name="com.unity3d.player.UnityPlayerNativeActivity"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:label="MY_APP_NAME"
            android:screenOrientation="landscape" >
        </activity>
                <activity
            android:name="com.unity3d.player.UnityPlayerProxyActivity"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:label="MY_APP_NAME"
            android:screenOrientation="landscape" >
        </activity>
    </application>
 
</manifest>
cs


(프로젝트 우클릭)-Export






2. 유니티에서


1) 파일배치

> 전 단계의 프로젝트내 res폴더, AndroidManifest.xml, jar파일을

   (...유니티 프로젝트...)\Assets\Plugins\Android에 복사합니다.



2) C#

1
2
3
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("SetToast""THIS IS ANDROID API");
cs






기타. 참조자료


유니티 - 매뉴얼: Android 플러그인 빌드

How To Use Android Plugins in Unity Apps | Vuforia Library Prod






기타. 변경이력


일자

변경이력

2016-04-08

 초안