SlideShare a Scribd company logo
Unity3D Plugins
Development Guide
ChenKaiJung
Plugins Are?
● Libraries of native code written in C, C++,
Objective-C….etc
● Allow your game code to call functions from
these libraries
Why do you need to develop a Plugin?
●
●
●
●
●
●

Unity3D can’t Do That
Reuse C/C++/Objective-C components
Difficult customize in Unity3D Assets Store
Cross-Device
Cross-Platform
Security Issues
Interface?
● Just like call Unmanaged DLL in .NET
● Sync
○ Call the functions and get return values

● Async
○ Use UnitySendMessage

● Name mangling Issue
○ Use extern "C" { }
○ or _ZN·9funtown·7ios·6init·E
Plugins for Different Device
● Compile time - iOS
#if UNITY_IOS
#else
#endif

● Compile time - Android
#if UNITY_ANDROID
#else
#endif
Plugins for Different Device
● Runtime - iOS
if (Application.platform == RuntimePlatform.OSXPlayer)

● Runtime - Android
if (Application.platform == RuntimePlatform.Android)
iOS Plugins
● C# Dll Import
[DllImport ("__Internal")] private static extern void iosInit(bool cookie, bool
logging, bool status, bool frictionlessRequests);

● C/C++ Native Functions in xxx.mm
extern "C" {
void iosInit(bool _cookie, bool _logging, bool _status, bool
_frictionlessRequests) { [[FbUnityInterface alloc] initWithCookie:_cookie
logging:_logging status:_status frictionlessRequests:_frictionlessRequests]; } }
iOS Plugins
#ifdef __cplusplus
extern "C" {
#endif
void UnitySendMessage(const char* obj, const char* method, const char*
msg);
#ifdef __cplusplus
}
#endif
iOS 3rd Powerful tools
● XCode Editor for
Unity3D

{
"group": "Facebook",
"patches": [],
"libs": [
"Facebook/Editor/iOS/FacebookSDK/libfacebook_ios_sdk.a:<group>"
],
"librarysearchpaths": [
"Facebook/Editor/iOS/FacebookSDK/"
],
"frameworks": [
"Accounts.framework:weak"
],
"headerpaths": [
"Facebook/Editor/iOS"
],
"files": [
"Facebook/Editor/iOS/FbUnityInterface.mm",
"Facebook/Editor/iOS/FbUnityInterface.h"
],
"folders": [],
"excludes": [
"^.*.meta$",
"^.*.mdown^",
"^.*.pdf$"
]

○ Post Process Build
○ Json Configure
(fixup.projmods)

}
iOS Tips
● DO NOT call plugins Pre-Frame
● String passing should be encoded by UTF-8
● Wrappers your Message and let it easily
encodes and decodes cross language. Ex:
JSON,Query String
● Uses XCode Editor for building automation
Android Plugins
● C# Dll Import
[DllImport ("unityinapppurchase")] private static extern void androidFTInit
(bool cookie, bool logging, bool status, bool frictionlessRequests);

● C++ Native Functions in unityinapppurchase.
so
extern "C" {
void androidFTInit(bool _cookie, bool _logging, bool _status, bool
_frictionlessRequests) { LOGD("androidFTInit called"); } }
Android Plugins
● C# Load Java Class
ftJava = new AndroidJavaClass("tw.com.funtown.unity.FT");
ftjava.CallStatic("Init", "");

● Java Class
public class FT
{
public static void init(String params) {
Log.d("FTUnitySDK", "init called with params: " + params);
}
}
Android Plugins
● Overwrite onActivityResult
public class FTUnityPlayerActivity extends UnityPlayerActivity
{
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("FTUnityPlayerActivity", "onActivityResult: requestCode: " +
requestCode + ", resultCode:" + resultCode);
}
}
Android Plugins
● Modify your Manifest.xml Automatically
<activity android:name="tw.com.funtown.unity.FTUnityPlayerActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="tw.com.funtown.LoginActivity" >
</activity>
Android Plugins
● Call UnitySendMessage using JNI
bool retval=JniHelper::getStaticMethodInfo(t, "com/unity3d/player/UnityPlayer" ,"UnitySendMessage"
,"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
if(retval==false) return;
jstring objectStr = t.env->NewStringUTF(obj);
jstring methodStr = t.env->NewStringUTF(method);
jstring msgStr = t.env->NewStringUTF(msg);
t.env->CallStaticVoidMethod(t.classID,t.methodID,objectStr,methodStr,msgStr);
jthrowable exception = t.env->ExceptionOccurred();
if (exception) {
t.env->ExceptionDescribe();
t.env->DeleteLocalRef(exception);
t.env->ExceptionClear();
} else {
}
…..Please delete your allocated resource here
Android Tips
● DO NOT call plugins Pre-Frame
● Mono garbage collector by using(){..} or xx.
Dispose() in C#
● Put your *.jar *.so in
YOUR_PROJECT/Assets/Plugins
● Be careful of Android resource conflict
problem
Go through OAuth SDK
Q&A
References
● Unity3D Official Plugins document http://docs.unity3d.
com/Documentation/Manual/Plugins.html
● Facebook Unity3D Plugin https://developers.facebook.
com/docs/unity
● A company sells Unity3D Mobile Plugins http://prime31.
com/

More Related Content

PDF
Plugin For Unity
PPTX
Avoid loss of hair while coding Unity3D plugins for mobile
ODP
Developing apps with Kivy
ODP
Kivy - Python UI Library for Any Platform
PPTX
Kivy report
PDF
Flutter tutorial for Beginner Step by Step
PDF
Couverture de code
Plugin For Unity
Avoid loss of hair while coding Unity3D plugins for mobile
Developing apps with Kivy
Kivy - Python UI Library for Any Platform
Kivy report
Flutter tutorial for Beginner Step by Step
Couverture de code

What's hot (20)

PDF
Standalone Android Apps in Python
PPTX
Android ndk
PPTX
NDK Introduction
PDF
Using the Android Native Development Kit (NDK)
PDF
Using the Android Native Development Kit (NDK)
PDF
Brillo / Weave Internals
PDF
Android Platform Debugging and Development
PDF
Android Platform Debugging and Development
PDF
Introduction to the Android NDK
PDF
#Code2Create:: Introduction to App Development in Flutter with Dart
PDF
Embedded Android Workshop with Marshmallow
PDF
Android ndk: Entering the native world
PDF
Android Platform Debugging and Development
PDF
Brillo/Weave Internals
PDF
Android Platform Debugging and Development
PDF
Embedded Android Workshop
PDF
Developing Android Platform Tools
PDF
Android Native Development Kit
PDF
Extending Android's Platform Toolsuite
PDF
Android Platform Debugging and Development
Standalone Android Apps in Python
Android ndk
NDK Introduction
Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)
Brillo / Weave Internals
Android Platform Debugging and Development
Android Platform Debugging and Development
Introduction to the Android NDK
#Code2Create:: Introduction to App Development in Flutter with Dart
Embedded Android Workshop with Marshmallow
Android ndk: Entering the native world
Android Platform Debugging and Development
Brillo/Weave Internals
Android Platform Debugging and Development
Embedded Android Workshop
Developing Android Platform Tools
Android Native Development Kit
Extending Android's Platform Toolsuite
Android Platform Debugging and Development
Ad

Viewers also liked (17)

PDF
製作 Unity Plugin for iOS
PPTX
New York 2013
PDF
Imperial Distribution CaseStudy-Vunani-2012
PDF
Hayn portfolio 2012
PPS
Solo in italia (1)
PPTX
Entregapremiosvela
DOC
6A THE THREE WISE MEN
PPTX
Gamification research
PDF
Macquarie Investor Day 2012
PPTX
Primary research
DOCX
Las marzas
DOCX
How I feel in a day, by Sofía.
PPTX
Avoid loss of hair while coding Unity3D plugin for mobile
PDF
Dr philtest -know_yourself
PPTX
Barriers and facilitators to community pharmacy management of people with ost...
PDF
Introduction to AR with Unity3D
PPTX
Unity 3d Basics
製作 Unity Plugin for iOS
New York 2013
Imperial Distribution CaseStudy-Vunani-2012
Hayn portfolio 2012
Solo in italia (1)
Entregapremiosvela
6A THE THREE WISE MEN
Gamification research
Macquarie Investor Day 2012
Primary research
Las marzas
How I feel in a day, by Sofía.
Avoid loss of hair while coding Unity3D plugin for mobile
Dr philtest -know_yourself
Barriers and facilitators to community pharmacy management of people with ost...
Introduction to AR with Unity3D
Unity 3d Basics
Ad

Similar to Unity3D Plugins Development Guide (20)

PDF
iOS and Android Development with Unity3D
PDF
Bridging the gap – plugin for unity and i os
PPTX
Native Code is Dead AKA Cross Platform Development with Unity3D
PDF
Making A Game Engine Is Easier Than You Think
PPTX
Ottawa unity user_group_feb13_2015
PDF
AI For Gaming lecture2 Unity and c# basics.pdf
PDF
Deploy All The Games
PDF
STUDY OF AN APPLICATION DEVELOPMENT ENVIRONMENT BASED ON UNITY GAME ENGINE
PDF
Running native code on Android #OSDCfr 2012
PDF
Tools for developing Android Games
PPTX
From Web to Mobile with Stage 3D
PPT
Migrating Unity3D projects to Windows 8
PDF
Game UI Development_1
PPTX
Creating great Unity games for Windows 10 - Part 1
PPTX
How to Create a VR Game in unity 3D
PDF
AKiOSMagic Manual
PDF
Native Code is Dead AKA Cross Platform Development with Unity 3D
PDF
The Basics of Unity - The Game Engine
PDF
Mono for Game Developers - AltDevConf 2012
PPTX
Unity-Beyond Games! - Josh Ruis
iOS and Android Development with Unity3D
Bridging the gap – plugin for unity and i os
Native Code is Dead AKA Cross Platform Development with Unity3D
Making A Game Engine Is Easier Than You Think
Ottawa unity user_group_feb13_2015
AI For Gaming lecture2 Unity and c# basics.pdf
Deploy All The Games
STUDY OF AN APPLICATION DEVELOPMENT ENVIRONMENT BASED ON UNITY GAME ENGINE
Running native code on Android #OSDCfr 2012
Tools for developing Android Games
From Web to Mobile with Stage 3D
Migrating Unity3D projects to Windows 8
Game UI Development_1
Creating great Unity games for Windows 10 - Part 1
How to Create a VR Game in unity 3D
AKiOSMagic Manual
Native Code is Dead AKA Cross Platform Development with Unity 3D
The Basics of Unity - The Game Engine
Mono for Game Developers - AltDevConf 2012
Unity-Beyond Games! - Josh Ruis

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
Teaching material agriculture food technology
PDF
cuic standard and advanced reporting.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Electronic commerce courselecture one. Pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Approach and Philosophy of On baking technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Cloud computing and distributed systems.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
KodekX | Application Modernization Development
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
The AUB Centre for AI in Media Proposal.docx
sap open course for s4hana steps from ECC to s4
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Review of recent advances in non-invasive hemoglobin estimation
Per capita expenditure prediction using model stacking based on satellite ima...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Approach and Philosophy of On baking technology
Empathic Computing: Creating Shared Understanding
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Cloud computing and distributed systems.
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KodekX | Application Modernization Development
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

Unity3D Plugins Development Guide

  • 2. Plugins Are? ● Libraries of native code written in C, C++, Objective-C….etc ● Allow your game code to call functions from these libraries
  • 3. Why do you need to develop a Plugin? ● ● ● ● ● ● Unity3D can’t Do That Reuse C/C++/Objective-C components Difficult customize in Unity3D Assets Store Cross-Device Cross-Platform Security Issues
  • 4. Interface? ● Just like call Unmanaged DLL in .NET ● Sync ○ Call the functions and get return values ● Async ○ Use UnitySendMessage ● Name mangling Issue ○ Use extern "C" { } ○ or _ZN·9funtown·7ios·6init·E
  • 5. Plugins for Different Device ● Compile time - iOS #if UNITY_IOS #else #endif ● Compile time - Android #if UNITY_ANDROID #else #endif
  • 6. Plugins for Different Device ● Runtime - iOS if (Application.platform == RuntimePlatform.OSXPlayer) ● Runtime - Android if (Application.platform == RuntimePlatform.Android)
  • 7. iOS Plugins ● C# Dll Import [DllImport ("__Internal")] private static extern void iosInit(bool cookie, bool logging, bool status, bool frictionlessRequests); ● C/C++ Native Functions in xxx.mm extern "C" { void iosInit(bool _cookie, bool _logging, bool _status, bool _frictionlessRequests) { [[FbUnityInterface alloc] initWithCookie:_cookie logging:_logging status:_status frictionlessRequests:_frictionlessRequests]; } }
  • 8. iOS Plugins #ifdef __cplusplus extern "C" { #endif void UnitySendMessage(const char* obj, const char* method, const char* msg); #ifdef __cplusplus } #endif
  • 9. iOS 3rd Powerful tools ● XCode Editor for Unity3D { "group": "Facebook", "patches": [], "libs": [ "Facebook/Editor/iOS/FacebookSDK/libfacebook_ios_sdk.a:<group>" ], "librarysearchpaths": [ "Facebook/Editor/iOS/FacebookSDK/" ], "frameworks": [ "Accounts.framework:weak" ], "headerpaths": [ "Facebook/Editor/iOS" ], "files": [ "Facebook/Editor/iOS/FbUnityInterface.mm", "Facebook/Editor/iOS/FbUnityInterface.h" ], "folders": [], "excludes": [ "^.*.meta$", "^.*.mdown^", "^.*.pdf$" ] ○ Post Process Build ○ Json Configure (fixup.projmods) }
  • 10. iOS Tips ● DO NOT call plugins Pre-Frame ● String passing should be encoded by UTF-8 ● Wrappers your Message and let it easily encodes and decodes cross language. Ex: JSON,Query String ● Uses XCode Editor for building automation
  • 11. Android Plugins ● C# Dll Import [DllImport ("unityinapppurchase")] private static extern void androidFTInit (bool cookie, bool logging, bool status, bool frictionlessRequests); ● C++ Native Functions in unityinapppurchase. so extern "C" { void androidFTInit(bool _cookie, bool _logging, bool _status, bool _frictionlessRequests) { LOGD("androidFTInit called"); } }
  • 12. Android Plugins ● C# Load Java Class ftJava = new AndroidJavaClass("tw.com.funtown.unity.FT"); ftjava.CallStatic("Init", ""); ● Java Class public class FT { public static void init(String params) { Log.d("FTUnitySDK", "init called with params: " + params); } }
  • 13. Android Plugins ● Overwrite onActivityResult public class FTUnityPlayerActivity extends UnityPlayerActivity { public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("FTUnityPlayerActivity", "onActivityResult: requestCode: " + requestCode + ", resultCode:" + resultCode); } }
  • 14. Android Plugins ● Modify your Manifest.xml Automatically <activity android:name="tw.com.funtown.unity.FTUnityPlayerActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="tw.com.funtown.LoginActivity" > </activity>
  • 15. Android Plugins ● Call UnitySendMessage using JNI bool retval=JniHelper::getStaticMethodInfo(t, "com/unity3d/player/UnityPlayer" ,"UnitySendMessage" ,"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); if(retval==false) return; jstring objectStr = t.env->NewStringUTF(obj); jstring methodStr = t.env->NewStringUTF(method); jstring msgStr = t.env->NewStringUTF(msg); t.env->CallStaticVoidMethod(t.classID,t.methodID,objectStr,methodStr,msgStr); jthrowable exception = t.env->ExceptionOccurred(); if (exception) { t.env->ExceptionDescribe(); t.env->DeleteLocalRef(exception); t.env->ExceptionClear(); } else { } …..Please delete your allocated resource here
  • 16. Android Tips ● DO NOT call plugins Pre-Frame ● Mono garbage collector by using(){..} or xx. Dispose() in C# ● Put your *.jar *.so in YOUR_PROJECT/Assets/Plugins ● Be careful of Android resource conflict problem
  • 18. Q&A
  • 19. References ● Unity3D Official Plugins document http://docs.unity3d. com/Documentation/Manual/Plugins.html ● Facebook Unity3D Plugin https://developers.facebook. com/docs/unity ● A company sells Unity3D Mobile Plugins http://prime31. com/