UnityEngine.dll issues

Hello! I am currently trying to create a Unity game using Haxe’s C# target. I have imported the unity dll file in my build.hxml file (-net-lib /Applications/Unity/Hub/Editor/2020.3.24f1/, but I am having some issues with getting some stuff to work.

I am importing unityengine.* into my haxe files. I am trying to add component of type MeshRenderer to a game object, as described in this documentation: Unity - Scripting API: GameObject.AddComponent. However, the haxe autocomplete (and the build process) tells me that the only way to do it is to use the outdated AddComponent(className: String). If I try to do something like AddComponent<MeshRenderer>() the compiler throws an error about an unexpected semicolon, I assume because I did not provide the required className string. This wouldn’t be an issue, except that when I do that, I can’t set a sharedMaterial. I also want to point out that the mentioned AddComponent method that haxe is using has been outdated for a while, and I have used the same dll (I assume, using the same unity version as in the dll path, and using UnityEngine) with the new version of AddComponent with no problem.

If anyone knows how to help or is willing to troubleshoot with me, please let me know! If this is the wrong place to put this, please let me know where to go! Thanks a lot!

  • gaetgu

I personally gave up on using directly UnityEngine.dll because it gave me compile errors in some situations, in favor of custom C# externs, which give a bit more control to the exposed types, although it requires some additional work and is only exposing what you would write externs for.

Regarding your call to AddComponent<MeshRenderer>(), I don’t know if there is a simple way to do that with regular haxe code, but you can workaround it by using untyped __cs__('raw C# code') (or cs.Syntax.code()).

Basically, anything from C# that you can’t call/iterate with Haxe, you can still fall back to using raw C#, even if that’s not very elegant.

Thanks alot!