Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Issue using custom DLL methods?

Discussion in 'Scripting' started by AerionXI, Sep 9, 2022.

  1. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Does anyone know in Unity how to load my own custom-created DLL into the actual source code of my game? I built my DLL using VS2019. Problem is, I cannot figure out after these lines what to do. I just need to be able to call all function within my VS 2019 C# DLL I compiled for Unity inside of its' own namespace and class. When I go to call in Start ( ),

    Code (Csharp):
    1.  
    2. MyFunction.Add ( 1 + 2 )
    3.  
    in a Debug Log, it gives error :

    Code (CSharp):
    1.  
    2. 'Main.MyFunction.Add()' is a method, which is not valid in the given context [Assembly-CSharp]csharp(CS0119)
    3.  
    Code (CSharp):
    1.  
    2. [ DllImport ( "MyMainFunction", CharSet = CharSet.Unicode ) ]
    3. public static extern void MyMainFunction ( );
    4.  
    YES the plugin is in the "Plugins" folder.

    MUCH appreciation to all who help!
     
    Last edited: Sep 9, 2022
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Managed assemblies just work out of the box. It's just as if the source code of your assembly was compiled into your project.

    The error you got would be just the way you use it which is not valid C#. You try to call your method. However method calls can only be done inside other methods.

    DllImports doesn't apply here at all if your DLL is a managed C# dll. DllImport is only used when importing a native C library.

    To sum up: You've been quite vague how your DLL looks like, what namespaces, classes and what methods you actually defined in that managed library. Also it's not clear if the method you try to call is a static method or an instance method that requires an instance of the class.

    Furthermore, as already mentioned, the error you mentioned indicates some issues with your calling code. However you haven't provided any context where or how you exactly call the method.
     
  3. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    This is how it looks inside my DLL
    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. namespace MyLib {
    6.     public static class MyMainFunction {
    7.         public static int Add(int a, int b) {
    8.             int ans=(a+b);
    9.             return ans;
    10.         }
    11.     }
    12. }
    13.  
    This is what I have in my code
    Code (CSharp):
    1.  
    2.     using System;
    3.     using UnityEngine;
    4.     using MainFunction;
    5.  
    6.     public class Main:MonoBehaviour {
    7.  
    8.         [DllImport("MyMainFunction", CharSet = CharSet.Unicode)]
    9.         public static extern void MyMainFunction();
    10.  
    11.         void Start(){
    12.             Debug.Log(MyMainFunction.Add(1,2));
    13.         }
    14.  
    15.     }
    16.  
    My DLL is in my Plugins folder.
     
    Last edited: Sep 9, 2022
  4. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    someone can help?
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Well your code on your calling side makes absolutely no sense. First of all naming a class "MyMainFunction" is more than misleading since that's not a function / method but a class, a type. As I said you don't need / can't use an extern declaration as this only applies to native code that is written in another language and not a managed assembly.

    Your using statement makes no sense either. A using statement can only import namespaces or you can provide an alias name for a certain class. The way you used the using statement you try to import a namespace named "MainFunction". However that name / identifer does not even exist in your dll at all.

    Even if your method would be in a native dll, your extern declaration declares a method. However you use it like an object. This looks like you have no idea what a namespace, class or method is ^^.

    So first of all, a managed dll does not need to be in the plugins folder. Even native plugins can now be placed anywhere inside the Assets folder. You would use your method just like this:


    Code (CSharp):
    1.     using System;
    2.     using UnityEngine;
    3.     using MyLib;
    4.  
    5.     public class Main : MonoBehaviour
    6.     {
    7.         void Start(){
    8.             Debug.Log(MyMainFunction.Add(1, 2));
    9.         }
    10.     }
    or by using the fully qualified class name like this:

    Code (CSharp):
    1.     using System;
    2.     using UnityEngine;
    3.  
    4.     public class Main : MonoBehaviour
    5.     {
    6.         void Start(){
    7.             Debug.Log(MyLib.MyMainFunction.Add(1, 2));
    8.         }
    9.     }
    Though I would highly recommend to rename both, your namespace as well as your class.
     
  6. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    I don't understand... Why is this giving me an error?

    Main.cs
    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Runtime.InteropServices;
    6. using System.Threading.Tasks;
    7. using UnityEngine;
    8. using MyLib;
    9.  
    10. public class Main : MonoBehaviour {
    11.  
    12.     void Start ( ) {
    13.  
    14.         Debug.Log ( MyLib.MyMainFunction.Add ( 1, 2 ) );
    15.  
    16.     }
    17.  
    18. }
    19.  
    DLL code - MyLib.dll
    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. namespace MyLib {
    6.     public static class MyMainFunction {
    7.         public static int Add(int a, int b) {
    8.             int ans=(a+b);
    9.             return ans;
    10.         }
    11.     }
    12. }
    13.  
     
    Last edited: Sep 9, 2022
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    What error? Errors have a lot of information designed to help you reason about them. It matters.
     
    Nad_B likes this.
  8. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Code (CSharp):
    1.  
    2. 'Main.MyMainFunction.Add()' is a method, which is not valid in
    3. the given context [Assembly-CSharp]csharp(CS0119)
    4.  
     
    Last edited: Sep 10, 2022
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    That's your whole code? nothing else? So you have removed the wrong extern declaration you originally had?

    Code (CSharp):
    1. // remove this if you still have this in your code
    2. [DllImport("MyMainFunction", CharSet = CharSet.Unicode)]
    3. public static extern void MyMainFunction();
     
  10. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
  11. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    507
    Double check if you use the correct (new) dll, maybe Unity still uses the old dll for some reason

    First delete your dll inside your Unity project, you should get a different error now, something like "MyLib" doesn't exist, if you don't get this error, then Unity is still using the "wrong" dll.


    Then build & copy the dll again and check if you still get the same error
     
  12. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    @R1PFake nope, its still not letting me activate my namespace

    Code (Csharp):
    1.  
    2. using MyLib;
    3.  
     
  13. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Look, it's really difficult to help you with this as we can only work with the information you provided. I've compiled a lot C# assemblies myself and added them to a Unity project and I never had any issues. I even have my debug tools which live in a seperate assembly which I can actually inject into any running Unity application. Even that works without any issues.

    Have you reduced your problem down to that assembly? So have you tried using a completely new / empty Unity project, adding the assembly into the assets folder and that script above? If you've done that, what are the assembly import settings and if there is a compiler error in Unity, what does it say?

    If it works in an isolated test case, there has to be something else in your other project that causes issues. We also don't know how you actually compile your seperate assembly. It's also possible that you referenced the wrong assemblies in your project so your assembly may be broken and can't be loaded at all. Though you should get an error or warning when you add such an assembly to your project. The debugging work is on your side. We can't really do much as we don't have your project at hand. I could reproduce what you claim you have and I know it will work. So there has to be something missing here.
     
  14. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    Maybe you have other errors in your scripts that does not allow Unity to import your DLL?

    Try removing all calls to your library, make sure everything compiles fine, then import it again?
     
    Bunny83 likes this.
  15. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    @Nad_B no I don't have any errors in my script, otherwise it wouldn't compile in VS 2019..