SolidAlloy/GenericUnityObjects

FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe

Closed this issue · 14 comments

Hello I'm having the trouble to test this package. I get this error running the samples.
I use Unity 2020.2.1f1, I have turn on .NET 4.X

FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

Did you install the package through Package Manager (Git URL) or OpenUPM? Can you confirm the Type References package is installed? CompilerServices.Unsafe is a part of the Type References package, so it is weird such an error is output. Type References and other packages GenericUnityObjects depends on are listed in the Git URL installation guide

Could you also show the full stack trace of the exception? Which assembly is complaining about it?

Hello, I install through OpenUPM using openupm install com.solidalloy.generic-unity-objects

I have try it again with new project (I'm using 2D template), install through OpenUPM then open Unity. It will give these errors:

Library/PackageCache/com.solidalloy.generic-unity-objects@2.4.6/Runtime/Util/GeneratedUnityObjectCache.cs(56,37): error CS0246: The type or namespace name 'AssemblyBuilder' could not be found (are you missing a using directive or an assembly reference?)
Library/PackageCache/com.solidalloy.generic-unity-objects@2.4.6/Runtime/Util/GeneratedUnityObjectCache.cs(65,37): error CS0246: The type or namespace name 'ModuleBuilder' could not be found (are you missing a using directive or an assembly reference?)

Then I change Api Compatibility Level from .NET Standard 2.0 to .NET 4.x, then the errors are gone.

Then I try to load the sample from package manager. Then it gives me the error above, here is the full stacktrace:

FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
TypeReferences.Editor.TypeDropdown.SelectionTree.CreateFoldersInPathIfNecessary (System.String path) (at Library/PackageCache/com.solidalloy.type-references@2.8.6/Editor/TypeDropdown/SelectionTreeItemsCreation.cs:51)
TypeReferences.Editor.TypeDropdown.SelectionTree.CreateDropdownItem (TypeReferences.Editor.TypeDropdown.TypeItem& item) (at Library/PackageCache/com.solidalloy.type-references@2.8.6/Editor/TypeDropdown/SelectionTreeItemsCreation.cs:25)
TypeReferences.Editor.TypeDropdown.SelectionTree.FillTreeWithItems (TypeReferences.Editor.TypeDropdown.TypeItem[] items) (at Library/PackageCache/com.solidalloy.type-references@2.8.6/Editor/TypeDropdown/SelectionTreeItemsCreation.cs:18)
TypeReferences.Editor.TypeDropdown.SelectionTree..ctor (TypeReferences.Editor.TypeDropdown.TypeItem[] items, System.Type selectedType, System.Action`1[T] onTypeSelected, System.Int32 searchbarMinItemsCount, System.Boolean hideNoneElement) (at Library/PackageCache/com.solidalloy.type-references@2.8.6/Editor/TypeDropdown/SelectionTree.cs:43)
GenericUnityObjects.Editor.ScriptableObjects.SelectionWindow.CenteredTypeDropdownDrawer.Draw (System.Action`1[T] onTypeSelected) (at Library/PackageCache/com.solidalloy.generic-unity-objects@2.4.6/Editor/ScriptableObjects/SelectionWindow/CenteredTypeDropdownDrawer.cs:27)
GenericUnityObjects.Editor.ScriptableObjects.SelectionWindow.OneTypeSelectionWindow.OnGUI () (at Library/PackageCache/com.solidalloy.generic-unity-objects@2.4.6/Editor/ScriptableObjects/SelectionWindow/OneTypeSelectionWindow.cs:37)
UnityEditor.HostView.InvokeOnGUI (UnityEngine.Rect onGUIPosition, UnityEngine.Rect viewRect) (at /Users/bokken/buildslave/unity/build/Editor/Mono/HostView.cs:403)
UnityEditor.DockArea.DrawView (UnityEngine.Rect viewRect, UnityEngine.Rect dockAreaRect) (at /Users/bokken/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:382)
UnityEditor.DockArea.OldOnGUI () (at /Users/bokken/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:372)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at /Users/bokken/buildslave/unity/build/External/MirroredPackageSources/com.unity.ui/Core/IMGUIContainer.cs:333)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) (at /Users/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:189)

and yes I can confirm that the Type References exists in the project.

I tested it in an empty project with almost all packages uninstalled/disabled, and unfortunately failed to reproduce the bug.

CompilerServices.Unsafe.dll is referenced by the TypeReferences.Editor assembly definition (where SelectionTreeItemsCreation.cs that reports the error is located). Please go to Packages/Type References/Editor in the Project window, select the TypeReferences.Editor assembly definition, and check if TypeReference_System.Runtime.CompilerServices.Unsafe is referenced in the inspector:
unsafe dll

My guess is that the reference somehow got broken.

sorry for late reply, but it's there and it's not broken
Screenshot 2021-03-17 at 17 31 43

Thanks for checking. That is a very weird issue and is probably Unity-related, because I can see the references are set up and nothing is missing, yet .csproj is probably not generated up correctly by Unity. The error occurs in Type References, my other plugin, and no one has reported such an error there yet.

Please open the TypeReferences.Editor.csproj file in the root of the project (you might need to regenerate project files in Preferences -> External Tools) and check if it contains the following line:

 <Reference Include="TypeReference_System.Runtime.CompilerServices.Unsafe">
 <HintPath>{path-to-project}/Packages/ClassTypeReference-for-Unity/Editor/DLLs/TypeReference_System.Runtime.CompilerServices.Unsafe.dll</HintPath>

Also, to be super-sure it is a problem with a generated DLL, you can go to Library/ScriptAssemblies/ and open TypeReferences.Editor.dll in any .NET decompiler (I use dotPeek). That's what I see there:
image
This way, we can confirm whether CompilerServices.Unsafe is referenced by the DLL when the error occurs.

I'm sorry such massive troubleshooting is required just to make the plugin work. I cannot reproduce the issue on my end, so I can only guess what can be wrong with the setup.

Hello2... I'll post the screenshot and explain what am I doing.

Firstly I try to find TypeReferences.Editor.csproj in the root of the project, but I can not find it.
Screenshot 2021-03-19 at 01 32 45

Then I try to go to Preferences -> External Tools and click Regenerate project files, nothing happens
Screenshot 2021-03-19 at 01 33 21

And then I try to turn on the Registry packages check box, and click Regenerate project files, and the files show up
Screenshot 2021-03-19 at 01 34 11

And I open the files in TextEdit in Mac, and try to search the line that you are referring to, it looks a little bit different, I'm not sure if it causes a problem or not.
Screenshot 2021-03-19 at 01 38 25

Then I try to open the TypeReferences.Editor.dll in Library/ScriptAssemblies/, I don't have dotPeek, and trying to search google, it seems it only works with Windows. So I search alternative, and luckily there's ILSpy and it has plugin for VSCode, so I try to install that, and open the file in VSCode, this is what I get.
Screenshot 2021-03-19 at 01 52 00

From the tree view on the left side, there's no References, but I can see TypeReferences.Editor.Drawers, TypeReferences.Editor.TypeDropdown and TypeReferences.Editor.Util. But on the right side, there's some decompiled script (?). I could see mscorlib, UnityEngine.IMGUIModule, etc. I'm guessing it means the same (?). I'm sorry I'm not familiar with this thing hahahaha. So I scroll down, and I found System.Runtime.CompilerServices.Unsafe.
Screenshot 2021-03-19 at 01 52 17

And that's it, I hope it helps ._.

Thank you so much! My guess now is that System.Memory requires a specific version of CompilerServices.Unsafe (4.5.3) and doesn't want to work with the higher version (5.0.0) (although it works well on my machine). I downgraded CompilerServices.Unsafe to the exact version System.Memory wants to see.

Please update the plugin using OpenUPM (openupm add com.solidalloy.generic-unity-objects) and check if the error is gone.

It seems the error gone :D, but I still have a problem.
When I try the sample, I could create WarriorsRelationship Scriptable Object. It gives this window.
Screenshot 2021-03-19 at 18 23 48

But when I try to create WarriorStats Scriptable Object, the window flashes (it opens and suddenly closes really fast), and it leaves some lines in the UI. I wonder why this happens.
Screenshot 2021-03-19 at 18 24 23

I try to create my custom class, and if it has only one generic, it will fails, if it has two, it does work. I'm not sure why this is happening ._.

Sorry for the delay. I changed how the drop-down is shown for generic scriptable objects with one generic argument. Please update to 2.4.8 and check if you can choose the generic type when creating a WarriorStats asset.

Now it's working as intended XD... thank you so much for fixing this :D

Sure, hope you will find the plugin useful :)