Search Unity

Unable to use custom DLL

Discussion in 'Scripting' started by Studio_Akiba, Mar 28, 2017.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I built the DLL of some code I have been working on and dropped it into a folder in my projects folder (Assets/Scipts/DLL) but when attempting to add the "using" declaration to a c# script, the DLL's namespace didn't appear.

    In my IDE (VS2015Community), the project(s) (.CSharp, .CSharp.Editor, .CSharp.Editor.Plugins and .CSharp.Plugins) didn't have the DLL under their References, and I am unable to add new entries into any of them.

    The DLL is compiled using .NET 3.5 as the target, and Unity displays no errors in the console, or in the inspector when the DLL is selected.

    When clicking the drop-down on the DLL, I can see the class name, so Unity is reading the DLL, I am just unable to use it.

    What have I done wrong to cause this?
     
  2. mumbot

    mumbot

    Joined:
    Oct 19, 2012
    Posts:
    86
    As far as i know any custom dll plugin needs to be placed in Assets/Plugins/Your.dll and the .NET version has to be 2.0 otherwise unity will give you errors.
     
  3. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Actually, when I target .NET 2.0 I am unable to build the DLL, I think some of the libraries in UnityEngine, which my DLL uses, have been updated beyond 2.0.

    I realised the Plugins folder thing and tried that, my DLL now appears in the IntelliSense for Using, the class is visible when trying to use it, but none of the functions are usable, which is strange because they are public.
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    And you're not doing something like trying to access the functions from the class when you should be using an instance?
     
  5. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I don't believe so, I think I have set up the library and using it correctly, and it doesn't do anything terribly complicated.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    3.5 actually.

    It might be clearer if you post some code from the DLL and how you're trying to use it in your project.
     
  7. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I can do 1 better :D

    As the library is so small, and designed for convenience mostly, it is small enough for me to post the project entirely.

    This is something I plan on releasing for free to the Asset Store once I have added support for the other shader variations and a couple of controllable functions, so any help you can render in getting this functional will be added to the credits.
     

    Attached Files:

  8. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    280
    Your project seems to work fine for me. Managed dlls don't have to be in any Plugins folder by the way.
     
    KelsoMRK likes this.
  9. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Just to confirm, you can access (use in another script) the functions INSIDE the ShaderProperties class?
     
  10. Krisztian-Leicht

    Krisztian-Leicht

    Joined:
    Sep 10, 2015
    Posts:
    6
    Works fine for me:
     
  11. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Hmm, just restarted my computer and Unity felt the need to re-import everything, it now works.
    The only thing missing now is the ability to call it inside a function, instead of inheriting it.

    So, I would do something like:
    Code (CSharp):
    1. void Start()
    2. {
    3.     ShaderProperties.Color(UnityEngine.Color.red);
    4.     ShaderProperties.AlbedoMap(SomeTexture);
    5. }
    But this still seems to be missing.
    I've never had problems like this before, but then again I don't work with DLL's very often.
     
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The methods should be static in order to achieve that. Has nothing to do with being in a DLL or not.
     
  13. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Would this not cause object reference errors on the GetComponent declarations?

    EDIT: Yes it does :(
    Changing all the functions to static causes this:
    Error CS0120 An object reference is required for the non-static field, method, or property 'Component.GetComponent<Renderer>()'
    For every instance of it.

    Not entirely sure what else I can do to get this working.
     
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Make a decision about how you want people to use the code you've written :)

    Either they reference them statically or they grab component references and use them as instances.
     
  15. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    All I want to be able to do is use a function as a shortcut.
    The problem I think is that things like SetColor don't seem to return anything, so I am at a complete loss of how to actually implement this.

    Any ideas?
     
  16. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I didn't look at your project so honestly I don't know what you're trying to achieve.

    Judging by the names you've given stuff I'm guessing you're trying to set properties for shaders in every material instance. If you want to use this more as a utility type of thing then I'd make users pass in the GameObject or Renderer instance that they want to modify as well as the property they're setting. So your SetColor method would take a GameObject and a Color and then the method would find all the materials on that GameObject and set the color property on the shader.

    But really - it's up to you how you want people to use the code you've written.
     
  17. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Yes, don't use ShaderProperties.Color. Just call Color(). Let's just strip down your class to the one method and demonstrate:

    Code (csharp):
    1.  
    2. public class ShaderProperties : MonoBehavior
    3. {
    4.         public void Color(Color color)
    5.         {
    6.             GetComponent<Renderer>().material.SetColor("_Color", color);
    7.         }
    8.  
    9.     //other stuff
    10. }
    11.  
    Those are set to public, so if you inherit it you'll be just fine... but you're not going to be able to use it statically because it has to access non-static stuff (i.e. GetComponent<Renderer>) which are going to be instance specific.

    So the best way to do it is:

    Code (csharp):
    1.  
    2. public class CustomShaderProperties : ShaderProperties
    3. {
    4.       void Start()
    5.       {
    6.             //These methods are called on the base class
    7.             Color(UnityEngine.Color.red);
    8.             AlbedoMap(SomeTexture);
    9.       }
    10. }
    11.  
    Maybe the best thing to do would be to make ShaderProperties abstract so it has to be inherited, but those methods cannot be static because they rely on a specific game object instance..