a project that I’m porting from Unity 4.3 to Unity 5.2.1f1 (latest) is giving me a strange problem.
I have a bunch of javascript scripts that access plugins written in C#. The javascripts are in Assembly-UnityScript (so phase 3, according to Unity - Manual: Special folders and script compilation order) and the C# scripts are in Assembly-CSharp-firstpass (phase 1, according to the same source). So based on that, my understanding is that the javascripts should be able to see those C# scripts, and that was indeed the case for the various platforms this project has shipped so far (although it was built with Unity 4.3).
Now on Unity 5.2.1f1 when building for Windows Store Universal 10, I’m getting a bunch of these:
Assets/spriteAssets/mansion/elevator_door/elevator_door_sound.js(3,22): BCE0018: The name ‘tk2dSpriteAnimator’ does not denote a valid type (‘not found’).
the reason why your javascript scripts cannot see C# scripts is that by default all C# scripts on Windows Store (Universal 10) are compiled with Microsoft C# compiler (Roslyn), which targets .NET Core 5, and UnityScript compiler cannot reference such assemblies.
To fix it, go to player settings, find compilation overrides setting and set it to “Use .NET Core partially”. It will make Assembly-CSharp-firstpass.dll to be compiled with Mono C# compiler, while Assembly-CSharp.dll will still be compiled using Roslyn.
Thanks, that fixes that issue!
However with “Use .NET Core partially” now it doesn’t seem like I can use TypeInfo… Tried it in a separate project as well, works with Use .NET Core but not with partially. Any idea how I can work around this?
Scripts under Plugins and Standard Assets folders go to Assembly-CSharp-firstpass, others should go to Assembly-CSharp.
With ‘Use .NET Core Partially’ the later is compiled using Microsoft compiler, so arranging your scripts accordingly might help.
I don’t get it; I’m talking about System.Type.GetTypeInfo() and the TypeInfo type that we have to use for windows store as part of System.Reflection.
Shouldn’t this just be available regardless of the compilation overrides?
A bunch of my plugins need this, and I suspect if I start arranging those scripts I’m gonna run in other issues
System.Type.GetTypeInfo() availability depends on the compiler which you’re using (available when used with MS Compiler which uses .NET Core, not available when Mono compiler is used), Compilation Overrides controls which compiler is used when compiling scrip.
So I think this sets us back to square one, because if I move my plugins to Assembly-CSharp, in order to get access to System.Type.GetTypeInfo(), they are then compiled with the MS compiler, and UnityScript compiler cannot reference these assemblies, as Tautvydas Zilys pointed out; and so now I’m back to my original problem: my javascript scripts cannot see the C# scripts that I had to move in order to get System.Type.GetTypeInfo()…
Don’t use System.Type.GetTypeInfo, use regular reflection API
Have System.Type.GetInfo functions in Assembly-CSharp.dll, then set callbacks in to Assembly-CSharp-firstppas.dll, so whenever somebody calls function from Assembly-CSharp-firstpass.dll, the callback would be called. For.ex.
// Assembly-CSharp-firstpass.dll - compiler with Mono
public void delegate _DoSomethingWithTypeInfo();
public class Utility
{
public static _DoSomethingWithTypeInfo doSomething;
// Accessible both from JS & C#
public void DoSomething()
{
_doSomething();
}
}
//Assembly-CSharp.dll - compiled with MS compiler
public class Implementation
{
public static void DoSomething()
{
TypeInfo t = typeof(something).GetTypeInfo();
}
// Call this somewhere in the begining of your game
public static void InitializedCallback()
{
Utility.doSomething = DoSomething;
}
}