2

I have to import UnityLoader.js in Angular using TypeScript. However, that lib don't have ts declaration and need configuration variable (array) to work (See below) .

Link to UnityLoader.js

Default implementation:

<!doctype html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Unity WebGL Player | Test Unity</title>
    <style>
    /* a style sheet needs to be present for cursor hiding and custom cursors to work. */
    </style>
  </head>
  <body>
    <canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" height="600px" width="960px"></canvas>
    <script type='text/javascript'>
/* Configuration variable here !! */
  var Module = {
    TOTAL_MEMORY: 268435456,
    errorhandler: null,         // arguments: err, url, line. This function must return 'true' if the error is handled, otherwise 'false'
    compatibilitycheck: null,
    dataUrl: "Development/ExportMiniDev.data",
    codeUrl: "Development/ExportMiniDev.js",
    memUrl: "Development/ExportMiniDev.mem",

  };
</script>
/* Library call here !! */
<script src="Development/UnityLoader.js"></script>
  </body>
</html>

I have read all docs and I followed this to create a declaration file.

Here my declaration file (index.d.ts):

// Type definitions for UnityLoader 5.4.3.f1
// Project: UnityLoader
// Definitions by: Alexandre Hagen <https://github.com/AlexandreHagen>

declare namespace UnityLoader {

    interface Module {
        TOTAL_MEMORY: number,
        errorhandler?: boolean,
        compatibilitycheck?: boolean,
        dataUrl: string,
        codeUrl: string,
        memUrl: string
    }

}
export {};

My Folder Structure in node_modules :

    .
    ├── ...
    ├── unity-loader                  
    │   ├── index.d.ts    # Ts declaration file
    │   ├── index.js    # It is a remame of UnityLoader.js
    │   └── package.js    # Package to npm
    └── ...

But I still don't understand one thing. How can have links to my declaration a lib that is not in npm package but just a .js? Indeed docs seem about npm packages only.

Currently import unityLoader = require("unity-loader"); is working but no import of UnityLoader.js... So I can't use it.

So what can we do to use a simple global library in Typescript? That issue is very important to me See here I hope you can help!

Ps: I am using Webpack to build my app.

4

1 回答 1

1

不确定你是否解决了这个问题。我已经启动并运行了。但它没有它应该的那么好。当我转到另一个页面然后返回时,我会立即收到错误......但这是我所做的......我的组件:

import { Component, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import { UnityService } from './unity.service';
declare var jQuery: any;
declare function feet(): any;
declare function water(): any;

@Component({
  moduleId: module.id,
  selector: 'sd-about',
  templateUrl: 'about.component.html',
  styleUrls: [ 'about.component.css' ]
})
export class AboutComponent implements OnInit {
  public selectedValue: string;
  public data: any;

  constructor( private http: Http, private _unity: UnityService) {
  }

  ngOnInit(): void {
    jQuery.getScript('app/about/test.js');
    jQuery.getScript('assets/Unity/UnityLoader.js');
  }

  public toggle_feet() {
    feet();
  }

  public toggle_water() {
    water();
  }

  private onError( onError: any ) {
    console.log(onError);
  }

}

测试.js:

var Module = {
  TOTAL_MEMORY: 568435456,
  errorhandler: null,
  compatibilitycheck: null,
  dataUrl: "assets/WebGL/WebGL.data",
  codeUrl: "assets/WebGL/WebGL.js",
  asmUrl: "assets/WebGL/WebGL.asm.js",
  memUrl: "assets/WebGL/WebGL.mem"
};

function unity() {
  console.log("Hello from Unity")
}

function water(){
  SendMessage("Build","Toggle_Water");
}

function feet(data){
  SendMessage("Build","Toggle_Feet", data);
}

希望这可以帮助!我目前正在尝试将所有内容都转移到服务中并摆脱 jquery ....

于 2017-05-12T07:42:33.740 回答