目前,我正在使用 Angular2 版本 2.1.2 和 Unity 可视化工具(使用 Unity 5.5 构建)。
我需要做的是从 Unity 到 Angular2 进行通信。
我使用的代码类似于下面
public void GetBillOfMaterials(string callbackFn)
{
var result = LightSystem.BillOfMaterials.Aggregate("", (current, material) => current + (material + Environment.NewLine));
Application.ExternalCall(callbackFn, result);
}
上面的函数将在 Angular2 中调用,如下所示
public GetBillOfMaterial()
{
SendMessage("Manager", "GetBillOfMaterials", "alert");
}
问题是,当我尝试“callbackFn”内的任何 angular2 函数时,它不会找到我的函数。
我尝试调用的函数位于如下所示的服务内部:
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);
}
}
我基本上需要能够使用 Application.ExternalCall() 或任何可以工作的函数从 Unity 调用上述服务内部的“TestFunction”。
如果我可以做任何进一步的说明,请告诉我。
谢谢!
最佳答案
我实际上已经为任何对该解决方案感兴趣的人解决了这个问题。
按照这个问题的示例:Angular2 - how to call component function from outside the app
我已更新“GetBillOfMaterial”的服务函数,如下所示:
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);
}
上述解决方案将允许您从 Unity 的模块/服务调用任何 Angular2 函数,同时仍然使用“Application.ExternalCall()”方法。
关于c# - 从 Unity WebGL 调用 Angular2 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42352355/