Unity has an incredibly powerful native plugin interface which allows almost any native library to be consumed by, and used within, the engine. The engine is also very dynamic and constantly changing, and the majority of information I found online about just how to do this was out of date and no longer applicable or just plain wrong. There are lots of tutorials that show how to call native code from Unity, but very few indeed show how to effectively call Unity code from a native library.  This can be a big problem if the library you're trying to incorporate has asynchronous calls. The only method documented in the official docs, UnitySendMessage, is very clunky and involves costly string parsing. This tutorial will demonstrate how to create C# delegates which actually compile to native code and can be called directly from C. It does involve a little bit of setup code, but it ends up working very nicely with asynchronous code libraries because asynchronous code often involves registering for notifications by design. Since the setup involved in enabling native to managed communication involves registering managed functions, your plugin can enable developers to register their C# code for native callbacks directly.

About This Blog

Jul 21, 2015

Voidstar Solutions is comprised of a small group of experienced software engineers, game designers, and educators. We enjoy designing and building high quality software, games, and websites, and we enjoy sharing our experiences and lessons with others. This blog contains posts on a variety of topics ranging from tutorials on game development to helpful tips about preparing an app for submission to one of the stores, and anything else we might find worth sharing as we pursue turning our passion into a full time career. The website showcases our work, products, and services, and the blog will detail some of the lessons we learn along the way. 

I have spent the past several years of my career working as a rendering engineer on the Lockheed Martin Prepar3D flight simulator, but I've never really done a deep dive into writing custom shaders for Unity. When a recent project we were working on called for a mobile-ready toon-shader I thought I'd take the opportunity to really dig into Unity's graphics pipeline rather than just grabbing one of the excellent offerings already available on the asset store.