Homebrew Tutorial
Updated
Integrating devkitPro for native plugins in Unity 3DS
IMPORTANT!!!
This method currently only works in emulator, because it crashes on 3ds hardware. I am still looking into this issue so until I get it fixed you should probably hold off on doing this, unless you want to start writing a dkp plugin that may or may not be able to be used on an actual 3ds in the future... lol
As most of us know, Unity for 3DS is somewhat limited with quite a few features of the 3ds. This functionality can be expanded by using native plugins written in C or C++ that work as libraries to be used in your game.
More recently, I've found a way to write code using devkitpro and compiling it to be able to be used inside of Unity! And I would like to share it here as a basic tutorial to get started for those who are interested.
As of now I've only tried reading and writing to the SDMC, but this could potentially open the door to the many features of devkitpro and its homebrew libraries to use any low level 3DS service that isn't exposed in Unity. I think this could be a break through a barrier that has been a huge headache for devs using this engine for 3ds.
We will cover some of the essential dos and don'ts I've come across, workarounds for some stuff, and the general setup needed
What you need
Getting Started
Implementing your plugin functionality to your Unity project
Example Project
This example plugin lets you write a file in your SD card
I will attach a ZIP file below that contains:
If you have any issues also feel free to comment here, I'll try to help as much as I can with my limited knowledge
This method currently only works in emulator, because it crashes on 3ds hardware. I am still looking into this issue so until I get it fixed you should probably hold off on doing this, unless you want to start writing a dkp plugin that may or may not be able to be used on an actual 3ds in the future... lol
As most of us know, Unity for 3DS is somewhat limited with quite a few features of the 3ds. This functionality can be expanded by using native plugins written in C or C++ that work as libraries to be used in your game.
More recently, I've found a way to write code using devkitpro and compiling it to be able to be used inside of Unity! And I would like to share it here as a basic tutorial to get started for those who are interested.
As of now I've only tried reading and writing to the SDMC, but this could potentially open the door to the many features of devkitpro and its homebrew libraries to use any low level 3DS service that isn't exposed in Unity. I think this could be a break through a barrier that has been a huge headache for devs using this engine for 3ds.
We will cover some of the essential dos and don'ts I've come across, workarounds for some stuff, and the general setup needed
What you need
- devkitpro
- devkitARM toolchain
- Basic C/C++ and C# knowledge
- Any text editor/IDE
Getting Started
- The Unity 3DS linker uses 16-bit wide characters, but most Makefiles (that I've seen) use 32-bit characters by default. This causes an error when attempting to build the game
- Before you compile your plugin code (this includes libctru as a whole if you use its objects!), you must add the following flag in your Makefile:
-fshort-wchar - Example:
-
Makefile:
CFLAGS := -g -Wall -O2 -mword-relocations -fshort-wchar \ -ffunction-sections \ $(ARCH) - (As I mentioned, you will have to compile libctru with these flags as well if you will be using it)
- Before you compile your plugin code (this includes libctru as a whole if you use its objects!), you must add the following flag in your Makefile:
- Now, even with a successful compile of your plugin, the Unity linker will likely complain about missing internal symbols that it doesn't normally provide. So we must add them manually.
- Examples:
void* __service_ptr = NULL;(This satisfies the environment handler)- If you're going to make use of strlen, you might need to define a local definition for it:
-
C:
size_t strnlen(const char *s, size_t maxlen) { size_t i; for (i = 0; i < maxlen && s[i] != '\0'; i++); return i; }
- Doing this, you can now compile your file
- After making, head to the generated lib folder and drag the
yourfilename.afile into your Plugins folder (Assets/Plugins/N3DS)- Don't forget! If you're using other compiled libraries like libctru you must compile it with 16-bit characters and also drag it into your project!
Implementing your plugin functionality to your Unity project
- Once you have your plugin loaded into your project, create a new script or edit an existing one
- Import
System.Runtime.InteropServices - Use
[DllImport("__Internal")]before statically linking a method from your plugin. Example:-
C#:
using System.Runtime.InteropServices; using UnityEngine; public class NativePlugin: MonoBehaviour { [DllImport("__Internal")] private static extern int MyNativeFunction(); void Start() { MyNativeFunction(); Debug.Log("Devkitpro plugin test"); } } - [IMPORTANT] It is highly likely that running code using the plugin inside of the Editor will only result in an error on your debug console. To test your plugin, run it from an emulator or on your 3ds console instead
-
- You are now able to build your game with new functionality by using your own native plugins with devkitpro!

Example Project
This example plugin lets you write a file in your SD card
I will attach a ZIP file below that contains:
- libctru compiled with 16-bit wide characters
- An example plugin
- Source file
- Compiled .a
- Makefile
- And a C# script to get you started
If you have any issues also feel free to comment here, I'll try to help as much as I can with my limited knowledge
Attachments
Last edited by CooingMaxito,








