ialex32x / unity-jsb

It brings Javascript runtime capability to Unity3D by integrating QuickJS.
MIT License
337 stars 41 forks source link

Question: why UnityEngine.ParticleSystem is undefined #59

Closed jo32 closed 2 years ago

jo32 commented 2 years ago

I trid to create ScriptRuntime to run example_monobehaviour.js provided in project.

 UnityEngine.Debug.Log("Start Creating Runtime");
var console = new EasyConsole();
var asyncManager = new DefaultAsyncManager();
var pathResolver = new PathResolver();
pathResolver.AddSearchPath("/Users/.../scripts");
var fileSystem = new DefaultFileSystem(console);
var bufferAllocator = new ByteBufferPooledAllocator();
var binder = DefaultBinder.GetBinder(true);
scriptRuntime = ScriptEngine.CreateRuntime();
scriptRuntime.extraBinding = (runtime, register) =>
{
    UnityEngine.Debug.Log("Start Binding");
    UnityEngine.Debug.Log("End Binding");
};
scriptRuntime.AddModuleResolvers();
scriptRuntime.Initialize(new ScriptRuntimeArgs
{
    fileSystem = fileSystem,
    pathResolver = pathResolver,
    asyncManager = asyncManager,
    logger = console,
    byteBufferAllocator = bufferAllocator,
    binder = binder,
});
scriptRuntime.CreateContext();
UnityEngine.Debug.Log("Done Creating Runtime");

However when I hit the line:

https://github.com/ialex32x/unity-jsb/blob/master/Scripts/out/example_monobehaviour.js#L162

It thows error because "UnityEngine_1.ParticleSystem" is undefined.

I noticed that UnityBinding.cs explicitly add some binding by default: https://github.com/ialex32x/unity-jsb/blob/master/Assets/jsb/Source/Unity/Editor/CustomBindings/UnityBinding.cs#L79

What is the recommended way to add ParticleSystem or other types to js runtime?

jo32 commented 2 years ago

Currently I added "UnityEngine.ParticleSystemModule" in implicitAssemblies in Prefs.cs

ialex32x commented 2 years ago

The recommended way is to add a BindingProcess sub-class for your project. Like what in the example: https://github.com/ialex32x/unity-jsb/blob/master/Assets/Examples/Source/Editor/CustomBinding.cs It gives more control than the configuration. It usually contains a bunch of unnecessary types and becomes a burden on the time consumption of packaging/compilation If exporting all types in specific assembly.