Search Unity

Add a reference to Assembly-CSharp in assembly definition file

Discussion in 'Scripting' started by Deleted User, Jul 18, 2018.

  1. Deleted User

    Deleted User

    Guest

    Hi. How can I add a reference to predefined assemblies when using assembly definition files?

    I can't use unity packages when I move my scripts to another assembly using asmdf files.
     
    Marks4, will_unity731 and ignarmezh like this.
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    You can't. The predefined assemblies are always the last to compile.

    If you need to reference code from Unity packages, and they're not available, those packages probably ship with their own assemblies, and instructions on how to include them. What packages are you struggling with?
     
  3. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    as far as I understood these assembly definition files you can use them only or not use them at all. So you cannot mix "predefined assemblies" with custom assemblies. Instead you have to put the assembly definition files all over your project including the used third party assets... And do not forget to add asmdf files to all the editor folders with the right compile settings...

    All this is designed without thinking about the real world I think... For me they are totally useless and I use my own tool which splits the assembly into several projects (unfortunately I cannot make use of the good parts of the asmdf approach: improving compile times and excluding projects from certain platforms).

    Edit: here some additional thoughts: https://forum.unity.com/threads/assemblydefinitionfiles-good-idea-but-not-usable-in-praxis.538739/
     
  4. Deleted User

    Deleted User

    Guest

    I was struggling with TextMeshPro. But I finally could fix it. The problem was that when you try to add a reference to assembly definition, the assembly definition files from packages doesn't show up in the pop up window. So I didn't know TextMeshPro had assembly definitions inside.


    It definitely needs improvement.
    At least we need a button to quickly mark assemblies as "Editor Only".
    Another enhancement that I think is really necessary is that Unity should generate Editor Only assembly definitions for all Editor folders with click of a button. Existing projects (and even relatively new ones) might have dozens of Editor folders. It's such a pain to create asmdf files for each one of them by hand ( Which is what I did :| ).
     
    leni8ec likes this.
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    That's just simply not true.

    Code under assembly definition folders work like the plugins folder in the old system. Your default assembly can reference them, but not the other way around. The only difference is that the different assemblies can reference each other in the same fashion, which gives a more granular approach if that's neccessary.

    Assembly definitions isn't special. Any non-unity .NET project in the world can do exactly the same thing by splitting code into different assemblies. Most other languages do too.[/QUOTE]
     
    burningmime and Bunny83 like this.
  6. Simon-O

    Simon-O

    Joined:
    Jan 22, 2014
    Posts:
    51
    That's fine unless you're trying to create a test assembly which references the main codebase but isn't deployed with a build...
     
    alphdevcode, HarvesteR and Jaden_01 like this.
  7. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    531
    Yep, ran into the same wall here. My test assembly (even the editor test assembly) has no way to access the actual game code I want to use for the testing...
     
    vikchokov and alphdevcode like this.
  8. MikeWise

    MikeWise

    Joined:
    Dec 27, 2017
    Posts:
    14
    So if understand it correctly (and I probably do not), then you can only use Unity's test framework on code you have put into an Assembly Definition?

    If it is true (and again I hope it is not), then it seems like a massive hurdle to anyone trying to use the Unity Test Framework (who needs stinkin' tests anyway... :) )
     
    Last edited: Nov 2, 2021
  9. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,074
    Recently decided to play a little with unity tests and stumbled upon the same problem.

    So is this true ?
    When you are going to use test framework you have to put assembly definition in your main scripts folder.

    This will automatically throw a lot of errors during compilation, since you now need to add manually all the references your projects is using to this new assembly definition.
     
  10. g_mek

    g_mek

    Joined:
    Apr 11, 2022
    Posts:
    3
    Compile errors are not the only thing that can happen when adding assembly definitions.

    All fields with an attribute [SerializedReference] lose their reference.
     
  11. MadsAnthony

    MadsAnthony

    Joined:
    Jul 29, 2014
    Posts:
    5
    For testing, if you need to reference something from the main csharp assembly just disregard the created test .asmdef and put all your code inside a editor folder like this:
    upload_2022-8-16_11-2-27.png

    Another valuable thing when using tests is to derive from Editor, that way you can have serialized references in your tests, like this:
    upload_2022-8-16_11-4-22.png
     

    Attached Files:

    alphdevcode, Gunx and NikMikk like this.
  12. NikMikk

    NikMikk

    Joined:
    Nov 4, 2015
    Posts:
    25
    This was a surprisingly simple solution that I somehow didn't see earlier, thanks a bunch!
     
  13. alikun

    alikun

    Joined:
    Oct 17, 2018
    Posts:
    17
    I don't want to revive this post, but I'm struggling with LevelPlay (IronSource) package and haven't found anything on the internet.
    @Baste when I install LevelPlay, it automatically adds IronSource source code folder in Assets folder, however there is no assembly to reference
     
  14. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    They probably have documentation somewhere? If it just dumps code into the assets folder without an assembly definition, and your code is in an assembly definition, then you can probably add an asmdef file to the IronSource code folder.

    Idk, though, not familiar with anything add related at all.
     
  15. unity_D10B9AC7B391AEF00D75

    unity_D10B9AC7B391AEF00D75

    Joined:
    Aug 16, 2023
    Posts:
    1
    This solution is valid for editor tests but I think its not valid for playmode tests.
     
  16. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,006
    Don't do that. Don't use classes for things they aren't created for. The Editor class is actually derived from ScriptableObject and all you actually need for default-reference-assets is a ScriptableObject. So don't just derive things from Editor when it doesn't represent a custom editor.

    Though keep in mind that default references are only applied inside the Unity editor. They do not work in a built game. They are just a tool to fill out common asset references when an instance of that class is created inside the editor.
     
  17. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    748
    It's pretty bad the way it works, I'm trying to use the performance extension, only way to make it reference my main assembly is to pull it out of packages and add the folder manually and remove the asmdef's
     
  18. Lumpazy

    Lumpazy

    Joined:
    Apr 24, 2018
    Posts:
    45
    I'm struggling with this ;(
    [/QUOTE]

    I have a package (the unityGltf) which needs to be reference in the assembly to work in script.
    & only need it temporarily and only in the editor, but still in script - to export a bunch of models (like 500)

    If i create any assembly reference anywhere i get a bunch of errors. I just need the one package and don't feel like reorganizing everything just for the one package i need temporarily !

    how can my default assemly reference this package ?

    I guess this must be easy for anyone who actually has worked with these before ?!
     
  19. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Assembly Definitions has an "auto referenced" checkbox which makes it possible to reference them from code that's not in assembly definitions - see here.

    So you should be able to set the asmdef to only be available in the editor by only checking the editor platform here, set the auto referenced checkbox, and then reference it from your editor code.
     
  20. Lumpazy

    Lumpazy

    Joined:
    Apr 24, 2018
    Posts:
    45
    :) Yes indeed.
    Thanks for the rather quick reply.

    I solved my particular problem. But -
    The things to stumble over :
    1. in an already existing project. add an assembly definition to the scripts folder. Then this should put all the scripts from this folder and subfolders into an assembly. & yes auto-referenced.
    Now this should not change anything ?!
    ALL (my) scripts are in the same assembly. And they can easily find each other.
    This is not the case.
    No idea where i am going wrong with this. (-yet).

    2. In my case the unityGltf package actually did not even contain the assemblies that were supposed to be referenced !!! Quite typical for the so called manual to just 'note' that to use it in script there should be references, but then the thing to reference is not even part of the package !! in the package manager.
    So . the full package from github had the assemblies. And the solution is to make a last child folder with an assembly reference that then contains the package part, and yes - autoreference is on. Then the script there can be used in a parent script from the above folder.