A simplified way to communicate between Unity and Angular
- Use a
MonoBehaviour
class. Onlystring
,number
are asparameters
accepted. Only 1 parameter is allowed maximum. It will outputUnityClient.ts
to your/Documents
folder.
/// <summary>
/// My documentation.
/// </summary>
[AngularExposed(gameObjectName: "MyGameObjectName")] // default gameObjectName value is Angular.
public void MyMethod(string myJsonObject)
{
// Logic to parse myJsonObject and manipulate it
}
Note: This cannot be generated at runtime, only at start of application. However, we decided not to support this. Instead a default GameObject name will be used, which can be overridden in the [AngularExposed]
-attribute we want the name of the GameObjects containing a script with the [AngularExposed]
-attribute. Ohterwise it cannot support multiple scenes, since it would look at the active scene to generate GameObjects at runtime, which would empty the UnityClient file each time.
Recompile or click on 'Play' in Unity editor to trigger AngularExposedExport.cs
class.
This will automatically generate:
UnityClient.ts
The bridge between calling C# methods from JavaScript
It will be placed in the special system folderMyDocuments
.
TODO:
- The .ts generation does not set documentation of methods in ts yet.
- Export onBuild these generated files to a npm package and fetch it from that.
- Optionally, let Typescript enter the GameObject names?
- Use a
MonoBehaviour
class. Onlystring
asparameter
is accepted. Since you can only send one string technically, there is an option to add the attribute[StringArrayAttribute]
-> this will make the service split a string into an array by"|"
.
/// <summary>
/// My documentation.
/// </summary>
[DllImport("__Internal")]
private static extern void SendObjectsToWeb(string objectIds);
public void MyMethod()
{
string myString = "value";
#if PLATFORM_WEBGL && !UNITY_EDITOR // otherwise crash
SendObjectsToWeb(myString);
#endif
}
- Recompile or run play to let
JSLibExport.cs
generate a file. - In a Angular component, you can subscribe to this method by importing
UnityJSLibExportedService
.
constructor(private unityJslibExportedService: UnityJSLibExportedService) {
this.unityJslibExportedService.objects$.pipe().subscribe((value) => {
console.log(value);
});
}
Recompile or click on 'Play' in Unity editor to trigger JSLibExport.cs
class.
This will automatically generate:
BrowserInteractions.jslib
The bridge between listening to C# methods from JavaScriptunity-jslib-exported.service.ts
A strongly typed service to function-call from Angular to Unity.
Both will be placed in the folderAssets/Plugins
.
TODO:
- The .ts generation does not set documentation of methods in ts yet.
- Support callbacks, see https://jmschrack.dev/posts/UnityWebGL/
- The .ts should automatically be placed in a ClientApp folder (via npm package).
Create a custom wrapping attribute to simplify usage (Not possible due to sealed attribute)
- It does not seem possible to extend theDLLImport
-attribute soStringArray
could be a parameter instead in a custom wrapping attribute for simplicity reasons.
- If so, also add parameter which says which category type/scene this belongs to (so all listeners can be organized)
public class JSLibExportAttribute : DllImportAttribute
{
public bool IsStringArray { get; set; }
public string Category { get; set; }
public JSLibExportAttribute(bool isStringArray = false, string category = "") {
IsStringArray = isStringArray;
Category = category;
}
}
To then be used as
[JSLibExportAttribute(IsStringArray, Category = "Core")]
private static extern void SendObjectsToWeb(string objectIds);