2016-12-14 9 views
2

TypeScriptを使用してUnityLoader.jsをAngularでインポートする必要があります。しかし、libはts宣言を持たず、動作するためには設定変数(配列)が必要です(下記参照)。UnityLoader.jsへのd.tsファイルの作成(Unity3D)

Link to UnityLoader.js

デフォルトの実装:

<!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> 

私はすべてのdocsを読んでいると私は宣言ファイルを作成するために、thisを追いました。

ここに私の宣言ファイル(index.d.ts):node_modulesで

// 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 {}; 

マイフォルダ構造:

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

しかし、私はまだ一つのことを理解していません。私の宣言へのリンクは、npmパッケージではなくただ.jsのlibにすることができますか?実際には、docsはnpmパッケージにしか見えません。

現在import unityLoader = require("unity-loader");は動作していますが、UnityLoader.jsをインポートしていないので、使用できません。

したがって、Typescriptでシンプルなグローバルライブラリを使用するにはどうすればよいですか? その問題は私にとって非常に重要ですSee here私はあなたが助けることを願っています!

Ps:Webpackを使用してアプリケーションを構築しています。

+0

誰かが助けることができますか? – Hagen

答えて

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

} 

test.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を取り除こうとしています....

関連する問題