从Unity WebGL调用Angular2函数

时间:2017-02-20 18:59:56

标签: c# angular unity3d web unity-webgl

目前我正在使用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”。
如果我能做进一步澄清,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:1)

我实际上已经为那些对解决方案感兴趣的人解决了这个问题。

按照此SO问题的示例: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);
}

上述解决方案允许您在仍然使用“Application.ExternalCall()”方法的同时从unity / service调用ANY angular2函数。