Download

How to write C# plugin

(This tuto was possible with the help of those projects:

https://unreal.gg-labs.com/wiki-arch…s/linking-dlls)

First, open a project and create a Blueprint Function Library C++ class:

Click Next and Create Class and visual studio should open, else double click on the class inside the engine.

Copy this into MyBlueprintFunctionLibrary.h :



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"

/**
*
*/
UCLASS()
class VIRTUALREALITY_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()

public:

UFUNCTION(BlueprintCallable, Category = "MyLibrary")
static void ImportDLL();

UFUNCTION(BlueprintCallable, Category = "MyLibrary")
static int Add(int x, int y);
};


Copy this into MyBlueprintFunctionLibrary.cpp :



// Fill out your copyright notice in the Description page of Project Settings.

#include "MyBlueprintFunctionLibrary.h"

typedef int(*_Add)(int x, int y);

void* v_dllHandle;
_Add m_Add;

void UMyBlueprintFunctionLibrary::ImportDLL()
{
FString filePath = "E:/CsPlugin.dll";
FString procA = "Add";

v_dllHandle = FPlatformProcess::GetDllHandle(*filePath);
m_Add = (_Add)FPlatformProcess::GetDllExport(v_dllHandle, *procA);
}

int UMyBlueprintFunctionLibrary::Add(int x, int y)
{
int out = m_Add(x, y);

return out;
}


And build this plugin (do it again if you have error):

Then download this visual project that include a C# demo that will create a dll to be used by the class we created in Unreal.
https://anonfiles.com/Fds7W7Leo3/CsPlugin_zip

Open the project, build it with the same platform you use to compile your Unreal class (x86 or x64) then grab the DLL built and move it to a path sharing the same path as filePath in MyBlueprintFunctionLibrary.cpp

Now, open a blueprint, right click and now you can call Add() from your Unreal plugin that come from a C# plugin.

csK0GFD - Imgur.png

Enjoy C# dev on Unreal Engine :slight_smile:

Now this is interesting. I had been wondering how this was done for a minute now. Still painfully new to writing code… This helps though since Most of my experience has actually been with C# I also have some old code I want to use without completely re-writing all over since it’s just stuff I wanted to experiment with anyhow. Maybe now I can move some of that stuff over into the engine and play around :slight_smile: