Here is what I found for you Discord
If you have a need to access and instantiate C# classes defined in Unity itself (not from TypeScript), you can’t directly instantiate C# classes in TypeScript. Instead, you can create a factory method in Unity to instantiate and expose C# instances to the TypeScript side via a messaging or event system, which can then be used within the TypeScript context.
Here’s an example of how to achieve this:
- In Unity, create a factory method in a MonoBehaviour to instantiate and expose the C# class instance.
- Expose a method to receive requests from the TypeScript side to instantiate the C# class instance.
- Handle the incoming requests, instantiate the C# class, and provide access to the instance.
Example in C# (Unity):
public class CSharpInstanceFactory : MonoBehaviour
{
public void InstantiateCSharpClass(string className)
{
// Instantiate the C# class based on className
// ...
// Send the instance or reference to TypeScript
// Example using Needle Engine's messaging system
MessagingSystem.SendCSharpInstanceToTypeScript(instantiatedCSharpInstance);
}
}
Example in TypeScript:
// Subscribe to receive the C# instance
MessagingSystem.onCSharpInstanceReceived((instance: any) => {
// Handle the received C# instance
console.log(instance);
// Now you can access and use the C# instance within TypeScript
});
In this approach, CSharpInstanceFactory
acts as a bridge to create and send the C# class instances to TypeScript through a messaging system, allowing you to use the instances within the TypeScript context. The exact implementation details will depend on your project’s architecture and requirements.
3 messages left GPT 3.5
thread_szALd9pk7MpWnlGfoZd1AKy3
@Ken2 what do you mean by that?
@marcel
I tried finding out if it’s possible to write C# cs in Unity and access it on typescript,
Because Generating custom class such as from my example, class BoardArea(contains one string and one Transform) was not able to converted into C# and build,
because it was a customtype(commented out from build).
So I was testing if it’s possible to build containers List on Unity Asset/BoardArea.cs and access it from typescript,
but it seems that I am not able to use class BoardArea on bothside because they cannot be defined in both side (C#, Typescript).
by user 151660356224286721
actually on the ts side it should be look like this
by user 151660356224286721
It’s possible to export your custom class.
You just need it in Ts and C# and mark it with @serializable(…)
@marcel thank you! I’ll try that
by user 151660356224286721
it worked! thanks!
by user 151660356224286721
Great!