Search Unity

Is it possible to reference Newtonsoft.Json on the main game assembly?

Discussion in 'Localization Tools' started by Garrafote, Jun 22, 2020.

  1. Garrafote

    Garrafote

    Joined:
    Jun 13, 2013
    Posts:
    48
    I could not find a way to reference newtonsoft.json on main assembly now that localization tools package depends on it as an external package.

    To show what I mean, I threw together a little project isolating the issue:

    I added newtonsoft to manifest.json dependencies:
    Code (CSharp):
    1.     "com.unity.nuget.newtonsoft-json": "1.1.2"
    then referenced newtonsoft.json on a test script:
    Code (CSharp):
    1.  
    2. using Newtonsoft.Json;
    3.  
    4. public class JsonTest { }
    5.  
    this gives me a reference error.
    upload_2020-6-22_14-2-18.png

    Is this the intended behavior?

    I realize I can add my JsonTest class to an assembly definition and override it's dependencies to explicitly include Newtonsoft.Json but that is not always an easy task on bigger projects.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Yes I think it was disabled by the team that added the package (not Localization). You can modify the asmdef to enable it again. I'll ask the team if they could enable it by default.
     
  3. Garrafote

    Garrafote

    Joined:
    Jun 13, 2013
    Posts:
    48
    Thank you! but I'm not sure if I follow you 100%.. Can you point me on how to modify this asmdef?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Sorry not the asmdef, I forgot it's a dll
    The DLL should have an option called auto reference, https://docs.unity3d.com/Manual/PluginInspector.html
    Now that I think about it, it's probably not a good idea for us to have this enabled as there's more chance of this clashing with people's projects if they use similar DLLs. The safest option is off by default.
    However you can take the package, move it Into your project package folder(embedded package) and then Change the DLL settings to how you like it
     
    Garrafote likes this.
  5. Garrafote

    Garrafote

    Joined:
    Jun 13, 2013
    Posts:
    48
    Enabling auto reference did it!

    Just for the record, apparently adding the following line to csc.rsp will also do the job:
    -r:Library/PackageCache/com.unity.nuget.newtonsoft-json@1.1.2/Runtime/Newtonsoft.Json.dll
    Is there any apparent reason not to go with adding the reference to csc.rsp?
     
    Noxalus likes this.
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Either is fine. I just don't use CSC.rsp much :)
     
    Garrafote likes this.
  7. Noxalus

    Noxalus

    Joined:
    Jan 9, 2018
    Posts:
    80
    csc.rsp file solution proposed by @Garrafote is working fine for me and it doesn't need to include the Newtonsoft package in my project so it's more convenient.
     
    Garrafote and karl_jones like this.
  8. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    456
    @karl_jones Yes, auto-reference I think causes significant issues. Anyone importing assemblies and utilizing external development, and nuget, will absolutely not work with the automatic, hidden and immutable packages Unity3D implicitly imports as a dependency. This is very bad!