从Unity WebGL调用Angular2函数

编程入门 行业动态 更新时间:2024-10-27 17:12:15
本文介绍了从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函数

本文发布于:2023-11-23 04:00:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Unity   WebGL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!