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) .
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.
not sure you solved the issue yet. I have it up and running. But its not as nice as it should be. And I receive errors as soon as I go to another page and then back.... but heres what I did.. My Component:
test.js:
hope this helps! I'm currently trying to move everything into a service and get rid of jquery....