background preloader

Native Plugin

Facebook Twitter

Pass structure (or class) from C++ dll to C# (Unity 3D) Creating an instance of a C++ plugin. Well, a class in c++ consists of the actual data fields (+vtable at the beginning).

Creating an instance of a C++ plugin

C# classes works totally different since every class is inherited from Object. You can not (at least I don't know a way) simply use a c++ class in C#. But you can use stdcalls. Actually i've never tried something like that, but i've found this article that might help: In the end it's always a lot of work to merge managed code and native code. Here's the Unity documentation page about plugins: As the documentation says you can "call into native code libraries" but to use a whole class is not that easy because the underlaying systems are too different. EDIT I just wrote a little example how i would manage the objects in c++. // Example.h #include <map> //Well and here's the cpp: // Example.cpp CManager* CManager::m_Instance = NULL; C# how to get Byte[] from IntPtr. How to pass an array from a c++ library to c# How do I Pass Arrays from C++ to C# in Unity, if at all Possible? Hmm...

How do I Pass Arrays from C++ to C# in Unity, if at all Possible?

How detailed should I get here? It is possible with one caveat. Assuming you don't use an unsupported hack method you'll need to use Unity Pro's plugin feature. Marshal.PtrToStructure Method (IntPtr, Type) (System.Runtime.InteropServices) Note: This API is now obsolete.

Marshal.PtrToStructure Method (IntPtr, Type) (System.Runtime.InteropServices)

Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified type. [ObsoleteAttribute("PtrToStructure(IntPtr, Type) may be unavailable in future releases. Instead, use PtrToStructure<T>(IntPtr). For more info, go to public static Object PtrToStructure( IntPtr ptr, Type structureType ) Parameters ptr Type: System.IntPtr A pointer to an unmanaged block of memory. C++ plugin: pass arrays from C# Unity plugin basics · [prism bucket] This post is an introduction to the basics of Unity plugins and how to create and use them.

Unity plugin basics · [prism bucket]

What are plugins? Artificial stupidity: C++ Plugin Debug Log. Unity3D Plugins Development Guide. Building a Windows Plugin for Unity: Lessons Learned. Here’s some fun trivia about Cosmic DJ: the way that we achieve our rock-solid musical timing and responsive tap input is by using an external audio engine that I wrote.

Building a Windows Plugin for Unity: Lessons Learned

Generally speaking I would not recommend doing this! However, it’s just not really possible to have perfect metronomic time if you are running your metronome in the main game loop. Fluctuations in frame rate mean fluctuations in the smallest timestep available to the metronome, so if a frame takes longer than a 16th note you’ll hear the rhythm get a little wonky! Anyway, that’s a separate discussion, I’m going to talk about all of the things I got caught on while trying to build this non-trivial plugin. Develop in Visual Studio 2008 A major thing I got stuck on for a while was trying to build the plugin with Visual Studio 2010.

Building Plugins for Desktop Platforms. Low-level Native Plugin Interface This page describes Native Code Plugins for desktop platforms (Windows/Mac OS X/Linux).

Building Plugins for Desktop Platforms

Note that plugins are intentionally disabled in webplayers for security reasons. Building a Plugin for Mac OS X On Mac OSX, plugins are deployed as bundles. You can create the bundle project with XCode by selecting and then selecting -> (in XCode 3) or -> -> (in XCode 4) C# - Writing C++ .dlls and access them Unity-3D. Unity and DLLs: C# (managed) and C++ (unmanaged) - Eric Eastwood. With Unity 5, there is no difference in terms of engine features between Personal/Free and Professional editions.

Unity and DLLs: C# (managed) and C++ (unmanaged) - Eric Eastwood

This means that "Native Code Plugin Support" is not a problem anymore and there are no restrictions with DLLs and C++(unmanaged) code.This guide really only applies to Unity 5- (Unity 4) When dealing with unmanaged and managed code, verbiage like P/Invoke, marshaling, interoperability get thrown around. The whole ordeal can be a bit daunting. Tutorial - Create C/C++ plugins for Unity3D. It’s been awhile I haven’t posted anything new.

Tutorial - Create C/C++ plugins for Unity3D

Now exams are over and I’m ready to post here some new stuff! Today I decided to share with you something that I think is not a “common knowledge” and there aren’t many resources on that topic on the internet. In this tutorial I’ll show you how to create plain C/C++ libraries for Unity3. Please note that this method will only work on Mac, on Windows you’ll need to use Visual Studio. Windows version is coming soon! Low-level Native Plugin Interface. Building Plugins for Desktop Platforms Textual Scene File Format In addition to the basic script interface, Native Code Plugins in Unity can receive callbacks when certain events happen.

Low-level Native Plugin Interface

This is mostly used to implement low-level rendering in your plugin and enable it to work with Unity’s multithreaded rendering. Access to the Graphics Device A plugin can receive notification about events on the graphics device by exporting a UnitySetGraphicsDevice function. // If exported by a plugin, this function will be called // when graphics device is created, destroyed, // and before and after it is reset (ie, resolution changed). extern "C" void EXPORT_API UnitySetGraphicsDevice ( void* device, int deviceType, int eventType); Possible values for deviceType: Possible values for eventType: enum GfxDeviceEventType { kGfxDeviceEventInitialize = 0, kGfxDeviceEventShutdown = 1, kGfxDeviceEventBeforeReset = 2, kGfxDeviceEventAfterReset = 3, };