Announcement

Collapse
No announcement yet.

How to write C# plugin

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [PLUGIN] How to write C# plugin

    (This tuto was possible with the help of those projects:
    https://github.com/3F/DllExport
    https://unreal.gg-labs.com/wiki-arch...s/linking-dlls)

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

    Click image for larger version  Name:	eznOGMJ - Imgur.png Views:	0 Size:	154.4 KB ID:	1799606

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

    Copy this into MyBlueprintFunctionLibrary.h :
    Code:
    // 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 :

    Code:
    // 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):

    Click image for larger version  Name:	XsQsB05 - Imgur.png Views:	0 Size:	31.9 KB ID:	1799604

    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.

    Click image for larger version  Name:	csK0GFD - Imgur.png Views:	0 Size:	14.5 KB ID:	1799605

    Enjoy C# dev on Unreal Engine

    #2
    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

    Comment

    Working...
    X