从Unity WebGL调用Angular2函数 [英] Call an Angular2 Function from Unity WebGL

查看:145
本文介绍了从Unity WebGL调用Angular2函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用带有Unity可视化工具的Angular2 2.1.2版,该可视化工具由Unity 5.5构建.

Currently I am working with Angular2 version 2.1.2 with a Unity visualizer, built with Unity 5.5.

我需要做的是从Unity到Angular2进行通信.
我正在使用类似于下面的代码

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);
    }

上面的函数将在Angular2中像下面这样被调用

Where the above function would be called in Angular2 like below

public GetBillOfMaterial() 
{
  SendMessage("Manager", "GetBillOfMaterials", "alert");
}

问题是,当我在"callbackFn"内尝试任何angular2函数时,找不到我的函数.

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);
}
}

我基本上需要能够使用Application.ExternalCall()或任何起作用的功能从Unity调用上述服务中的"TestFunction".
让我知道是否可以做进一步的澄清.

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.

谢谢!

推荐答案

我实际上已经为对解决方案感兴趣的任何人解决了这个问题.

I've actually solved the question for anyone who is interested in the solution.

以下是这个问题的示例: Angular2 -如何从应用程序外部调用组件功能

Following the example from this SO question: Angular2 - how to call component function from outside the app

我已经更新了"GetBillOfMaterial"的服务功能,如下所示:

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);
}

以上解决方案将允许您从模块/服务统一调用ANY angular2函数,同时仍使用"Application.ExternalCall()"方法.

The above solution will allow you to call ANY angular2 function from module/service from unity while still using the "Application.ExternalCall()" method.

这篇关于从Unity WebGL调用Angular2函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆