Quickstart: Visionary Render plugin development using C#

The Visionary Render programming guide explains how to set up a native plugin using C++. There is also a subset (beta) of the native API available via a .NET Standard package on NuGet.


This page will show you how to set up a VisRen plugin using C#. You can either create the project manually, or use the template available in the next section.


TABLE OF CONTENTS


Manual Project Creation Method


  1. Create a Class Library (.NET Framework) project

  2. Open the project properties

  3. Select the Build settings

  4. Change the platform target to x64

  5. Repeat for Release configuration

  6. Open the NuGet package manager

  7. Search for and install the "UnmanagedExports" and "VRTree.NET" packages

  8. Rename your Class1.cs file to something appropriate for your plugin, and replace its contents with the following code - remember to substitute the namespace, class, and plugin names according to the name of your plugin:
    using RGiesecke.DllExport;
    using System;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using Virtalis.SDK.VRTreeNative;
    
    namespace My_Plugin
    {
      public class Exports
      {
        //Logging function pointer stored here.
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate void LogFunc(int type, string message);
        public static LogFunc Log;
    
        static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
        {
          Assembly assembly = null;
          try
          {
            string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll");
    
            if (!File.Exists(assemblyPath)) return null;
            assembly = Assembly.LoadFrom(assemblyPath);
          }
          catch (Exception e)
          {
            Console.WriteLine(e.Message);
          }
    
          return assembly;
        }
    
        static Exports()
        {
          AppDomain currentDomain = AppDomain.CurrentDomain;
          currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder);
        }
    
        [DllExport("VRPGetAPIVersionMajor", CallingConvention = CallingConvention.Cdecl)]
        public static int MajorV()
        {
          return 1;
        }
    
        [DllExport("VRPGetAPIVersionMinor", CallingConvention = CallingConvention.Cdecl)]
        public static int MinorV()
        {
          return 1;
        }
    
        [DllExport("VRPInit", CallingConvention = CallingConvention.Cdecl)]
        public static int Init()
        {
          return 0;
        }
    
        [DllExport("VRPCleanup", CallingConvention = CallingConvention.Cdecl)]
        public static int Cleanup()
        {
          return 0;
        }
    
        [DllExport("VRPName", CallingConvention = CallingConvention.Cdecl)]
        public static string Name()
        {
          return "My Plugin";
        }
    
        [DllExport("VRPShortName", CallingConvention = CallingConvention.Cdecl)]
        public static string ShortName()
        {
          return "My Plugin";
        }
    
        [DllExport("VRPVersion", CallingConvention = CallingConvention.Cdecl)]
        public static string Version()
        {
          return "0.1";
        }
    
        [DllExport("VRPRegisterLog", CallingConvention = CallingConvention.Cdecl)]
        public static void RegisterLogFunc([MarshalAs(UnmanagedType.FunctionPtr)] LogFunc func)
        {
          Log = func;
        }
    
        [DllExport("VRPSignature", CallingConvention = CallingConvention.Cdecl)]
        public static string Signature()
        {
          return "";
        }
      }
    }


Project Template Method

You can also download and install our project template


  1. Download the VSIX file attached to this article
  2. Double click it and follow the install instructions
  3. Once installed, start a new project and search for "visionary" - and select Visionary Render Plugin (.NET)

  4. Enter a name for your plugin:

  5. Once the project loads, it's best to upgrade packages.config (press OK on the popup dialog)

    This both updates the project to package references, and installs the dependencies from NuGet.

Final Steps

You are now ready to add functionality to your plugin.


As with a C++ based native plugin, you need to create a file called plugin.txt next to your plugin DLL after building it. This file should contain the filename of the main plugin DLL - e.g. "My Plugin.dll". This allows VisRen to find your plugin and distinguish it from the dependency DLLs in the same folder.



Attachments

Visual Studio 2019 project template

J
Jamie is the author of this solution article.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.