KEMBAR78
製作 Unity Plugin for Android | PDF
製作 Unity Plugin for Android
Johnny Sung
2014.09.24 @ Android Taipei
反應還不錯!
(自己講)
https://fb.com/j796160836
Johnny Sung
Mobile devices Developer
https://plus.google.com/+JohnnySung
http://about.me/j796160836
Mobile devices Developer
https://plus.google.com/+JohnnySung
http://about.me/j796160836
Agenda
Unity 概念與簡介
GameObject
Component
Android Plugin實作
Android 接口
C# 接口
檔案打包放置的注意事項
3D 遊戲引擎 (也可以寫2D Game)
全球 60% App 遊戲採用
JavaScript / C# / BOO
夭壽強的跨平台
https://unity3d.com/unity/multiplatform
http://www.moneydj.com/kmdj/news/NewsViewer.aspx?a=f4767522-2281-4d16-8ed0-99c6acb797f8
X
Monodevelop
GameObject
Component
GameObject
GameObject
GameObject
Component
using UnityEngine;

using System.Collections;



public class testComponent : MonoBehaviour {



    // Use this for initialization

    void Start () {

    

    }

    

    // Update is called once per frame

    void Update () {

    

    }

}
http://docs.unity3d.com/ScriptReference/
C#
AndroidJavaClass
AndroidJavaObject
Java
Java

UnitySendMessage()
C#

(對應的接⼝口)
(Unity Engine) (Native)
/Applications/Unity/Unity.app/Contents/
PlaybackEngines/AndroidPlayer/development/bin
classes.jar 檔案位置
Mac
C:Program Files (x86)UnityEditorData
PlaybackEnginesandroidplayerdevelopmentbin
Windows
(optional)
AndroidJavaClass
AndroidJavaObject
h"p://docs.unity3d.com/ScriptReference/AndroidJavaObject.html
AndroidJavaObject
Call()
CallStatic()
Get()
GetStatic()
SetStatic()
h"p://docs.unity3d.com/ScriptReference/AndroidJavaObject.Call.html
package com.example.unitytest;
public class Pet {
private int id;
private String name;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Pet [id=" + id + ", name=" + name + "]";
}
}
AndroidJavaObject obj = new AndroidJavaObject ("com.example.unitytest.Pet");

obj.Call ("setId", 123);

obj.Call ("setName", "Lucky");

string str = obj.Call<string> ("toString");

Debug.Log (str);
Pet obj = new Pet();
obj.setId(123);
obj.setName("Lucky");
String str=obj.toString();
Log.v("Unity", str);
Java
C#
try
{
UnityPlayer.UnitySendMessage(gameObject, method, message);
}
catch(Exception e)
{
Log.e("Unity", "UnitySendMessage failed" + e.getMessage());
}
GameObject Name Method Name Parameter
C#
public void CallFromObjC(string message)

{

   Debug.Log (message);

}
MyViewObject.cs
try {
UnityPlayer.UnitySendMessage("MyGameObject",
"CallFromObjC", "hello");
} catch (Exception e) {
e.printStackTrace();
}
Java
MyPlugin.java
Activity
import android.app.Activity;
import com.unity3d.player.UnityPlayer;
public class MyViewPlugin {
public static Activity getActivity() {
Activity a = UnityPlayer.currentActivity;
return a;
}
}
public static AndroidJavaObject getActivity ()

{

    AndroidJavaClass player;
    player = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");

    AndroidJavaObject a;
    a = player.GetStatic<AndroidJavaObject> (“currentActivity");
return a;

}
C#
Java
package com.unity3d.player;
public class UnityPlayerNativeActivity extends NativeActivity
{
// don't change the name of this variable; referenced from native code
protected UnityPlayer mUnityPlayer;
// Setup activity layout
@Override protected void onCreate (Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().takeSurface(null);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
getWindow().setFormat(PixelFormat.RGB_565);
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
// Quit Unity
@Override protected void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}
// …
UnityPlayerNativeActivity.java
package com.example.unitytest;
import android.os.Bundle;
import com.unity3d.player.UnityPlayerNativeActivity;
public class MainActivity extends UnityPlayerNativeActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.unitytest" android:theme="@android:style/Theme.NoTitleBar"
android:versionName="1.0" android:versionCode="1"
android:installLocation="preferExternal">
<supports-screens android:smallScreens="true" android:normalScreens="true"
android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<application android:icon="@drawable/app_icon" android:label="@string/app_name"
android:debuggable="false">
<activity android:label="@string/app_name" android:screenOrientation="fullSensor"
android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|
keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|
smallestScreenSize|fontScale"
android:name="com.example.unitytest.UnityPlayerNativeActivity">
<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" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik"
android:value="false" />
</activity>
</application>
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="20" />
<uses-feature android:glEsVersion="0x00020000" />
</manifest>
AndroidManifest.xml
打包輸出Assets
Plugins
Android
bin
MyJar.jar
res
drawable-hdpi
drawable-ldpi
…
layout
values
AndroidManifest.xml
打包輸出
Q and A
Thanks!
References
Unity Webview
https://github.com/gree/unity-webview
Building Plugins for Android
http://docs.unity3d.com/Manual/PluginsForAndroid.html
藍斯洛‧雜技的雜記: Unity3D Plug-in for Android -- Activity 擴展⽅方法
http://lancelotdiary.blogspot.tw/2012/05/unity3d-plug-in-for-
android-activity.html
懂點Unity Plugin,替荷包省點錢!(安卓 Android篇)
http://www.unityin.com/2013/05/%E6%87%82%E9%BB%9Eunity-plugin%EF%BC%8C
%E6%9B%BF%E8%8D%B7%E5%8C%85%E7%9C%81%E9%BB%9E%E9%8C%A2%EF%BC
%81%E5%AE%89%E5%8D%93-android%E7%AF%87/
Unity3D研究院之打開Activity與調⽤用JAVA代碼傳遞參數
http://www.xuanyusong.com/archives/667
Unity GPS plugin development tutorial: 

building a Android plugin for Unity with Eclipse and Ant
http://www.mat-d.com/site/unity-gps-plugin-development-tutorial-
building-a-android-plugin-for-unity-with-eclipse-and-ant/
References
Android Back
public class BackKey : MonoBehaviour

{

    void Update() {

        #if UNITY_ANDROID

        if (Input.GetKeyDown(KeyCode.Escape)) {

            Application.Quit(); 
//            Application.LoadLevel("PreviousLevel");

        }

        #endif

    }

}
h"p://answers.unity3d.com/quesBons/25535/android-­‐back-­‐bu"on-­‐event.html
Unity 應用領域
https://www.facebook.com/groups/581769871867384
想做遊戲?

製作 Unity Plugin for Android