Reputation: 301
Currently I am working with Angular2 version 2.1.2 with a Unity visualizer, built with Unity 5.5.
What I need to be able to do is communicate from Unity to Angular2.
I was using code similar to below
public void GetBillOfMaterials(string callbackFn)
{
var result = LightSystem.BillOfMaterials.Aggregate("", (current, material) => current + (material + Environment.NewLine));
Application.ExternalCall(callbackFn, result);
}
Where the above function would be called in Angular2 like below
public GetBillOfMaterial()
{
SendMessage("Manager", "GetBillOfMaterials", "alert");
}
The problem is that when I try any angular2 function inside of the "callbackFn" it won't find my function.
The function I am trying to call is inside of a service that looks like this:
import { Injectable } from '@angular/core';
import { FinishService } from "./sysWideComps/finish/finish.service";
import { ColorService } from "./sysWideComps/colorTemp/color.service";
import { CircuitService } from "./filters/tabs/circuit/circuit-service";
import { StandoffService } from "./filters/tabs/standoff/standoff-tab.service";
@Injectable()
export class UnityService {
private isFirstCall: boolean = true;
constructor(private colorService: ColorService, private finishService:FinishService, private circuitService: CircuitService, private standoffService: StandoffService) { }
public ChangeFinish(finishType: string) {
console.log(`This is the finish type ${finishType}`);
this.sendMsgParam("ChangeFinish", finishType);
}
public ChangeColorTemp(colorTemp: number) {
this.sendMsgParam("ChangeColorTemperature", colorTemp);
}
public ChangeCircuit(circuitType: string) {
this.sendMsgParam("ChangeCircuit", circuitType);
}
public CreateModel(params: string) {
if (this.isFirstCall) {
this.ChangeCircuit(this.circuitService.getCircuit());
this.ChangeFinish(this.finishService.getFinish().FinishName.replace(" ", ""));
this.ChangeStandoffLength(this.standoffService.getStandoffLength());
//Dropdown goes here
let tempCTemp = this.colorService.getColorTemp().Temperature.replace("k", "");
let tempNum: number =+ tempCTemp;
this.ChangeColorTemp(tempNum);
this.isFirstCall = false;
}
this.sendMsgParam("CreateModel", params);
}
public Delete() {
this.sendMsg("Delete");
}
public ResetView() {
this.sendMsg("ResetCamera");
}
public RotateObject() {
this.sendMsg("RotateCurrentObject");
}
public Unselect() {
this.sendMsg("Unselect");
}
ChangeStandoffLength(input: number) {
this.sendMsgParam("ChangeSystemDropdownLength", input.toString());
}
public AddStandoff(params: string) {
let data: string = params;
this.sendMsgParam("AddStandoffs", data);
}
public GetBillOfMaterial() {
SendMessage("Manager", "GetBillOfMaterials", "alert");
}
public testFunction(input: string) {
alert(input);
}
sendMsgParam(functionName: string, params: any) {
SendMessage("Manager", functionName, params);
}
sendMsg(functionName: string) {
SendMessage("Manager", functionName);
}
}
I basically need to be able to call "TestFunction" inside of the above service from Unity using Application.ExternalCall() or whatever function would work.
Let me know if there is any further clarification I can do.
Thanks!
Upvotes: 2
Views: 1388
Reputation: 301
I've actually solved the question for anyone who is interested in the solution.
Following the example from this SO question: Angular2 - how to call component function from outside the app
I've updated the service function of "GetBillOfMaterial" to look as below:
constructor(
private colorService: ColorService
,private finishService:FinishService
,private circuitService: CircuitService
,private standoffService: StandoffService
,private ngZone:NgZone) { }
public GetBillOfMaterial{
if(!this.isConfigured) {
this.isConfigured = true;
window.my = window.my || {};
window.my.namespace = window.my.namespace || {};
window.my.namespace.publicFunc = this.PubFunc.bind(this);
}
SendMessage("Manager", "GetBillOfMaterials", "window.my.namespace.publicFunc");
}
public PubFunc(input: string) {
this.ngZone.run(() => this.testFunction(input));
}
public testFunction(input: string) {
alert(input);
}
The above solution will allow you to call ANY angular2 function from module/service from unity while still using the "Application.ExternalCall()" method.
Upvotes: 2