- Home /
Confusing Transitive Assembly References
TL;DR
How do you write managed plugins in Unity 2019.2+ that reference types defined in Unity Packages, particularly those that used to be defined in UnityEngine.UI.dll
, like ScrollRect
?
MORE INFO
I have been trying to create two managed plugins (assemblies) to pull out common logic for my Unity projects: UnityUtil.dll
, which defines some common types, and UnityUtil.Editor.dll
which references and defines custom inspectors for some of the types in UnityUtil
. Both assemblies referenced the UnityEngine.dll
and UnityEngine.UI.dll
assemblies stored in Program Files/
, and UnityUtil.Editor
also referenced UnityEditor.dll
. After upgrading to Unity 2019.2, UnityEngine.UI.dll
is no longer present in Program Files/
; instead, it is part of the com.unity.ugui Unity Package, and thus ends up in Library/ScriptAssemblies
inside the Unity project, so I changed my references to use that path.
Unfortunately, Visual Studio's compiler still wasn't satisfied. One of the classes in UnityUtil
is called ScrollRectVelocityClamper
, and zeroes out ScrollRect.velocity
for very low scroll velocities, as a performance optimization. Unfortunately, I got compiler errors on all lines accessing ScrollRect.velocity
, with the following error:
Error CS0012 The type 'Vector2' is defined in an assembly that is not referenced. You must add a reference to assembly 'UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
So apparently the new UnityEngine.UI.dll
in 2019.2 (which declares ScrollRect.velocity
) directly references UnityEngine.CoreModule.dll
(which declares Vector2
), rather than UnityEngine.dll
. So I replaced the UnityEngine.dll
reference in UnityUtil
with references to the various module assemblies that I'm using (including core module).
But now, my UnityUtil.Editor
assembly is unsatisfied. As I did above, I replaced the UnityEngine.dll
reference in UnityUtil.Editor
with references to the specific Unity modules that I'm using, including the core module. UnityUtil.Editor
defines an Editor
for a class in UnityUtil
derived from ScriptableObject
and is showing a similar error to that above:
Error CS0012 The type 'ScriptableObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
This error doesn't make sense to me. Why does the compiler think that ScriptableObject
is defined in UnityEngine.dll
, if both UnityUtil
and UnityUtil.Editor
are referencing UnityEngine.CoreModule.dll
, not UnityEngine.dll
? I can't just make UnityUtil.Editor
reference UnityEngine.dll
also, because then there are duplicate declaration (CS0433) errors. Nor can UnityUtil.Editor
reference UnityEngine.dll
instead of the module assemblies, because then I get similar CS0012 errors to what I first saw in UnityUtil
, about types like Sprite
being "defined in an assembly that is not referenced", namely UnityEngine.CoreModule.dll
.
Has anyone else run into these complex reference issues when making managed plugins for Unity 2019.2+? I could really use some guidance. Thanks in advance!