Unity WebGL provides an easy solution for embedding Unity WebGL builds in your webApp or Vue.js project, with two-way communication between your webApp and Unity application with advanced API's.
loaderUrl: string The url to the build json file generated by Unity
dataUrl: string The url to the build data file generated by Unity
frameworkUrl: string The url to the framework file generated by Unity
codeUrl: string The url to the unity code file generated by Unity
streamingAssetsUrl?: string The url where the streaming assets can be found
companyName?: string The applications company name
productName?: string The applications product name
productVersion?: string The applications product version
webglContextAttributes?: IWebGLContextAttributes This object allow you to configure WebGLRenderingContext creation options. see MDN@WebGLRenderingContext
devicePixelRatio?: number Uncomment this to override low DPI rendering on high DPI displays. see MDN@devicePixelRatio
matchWebGLToCanvasSize?: boolean Uncomment this to separately control WebGL canvas render size and DOM element size. see unity3d@matchWebGLToCanvasSize
Methods
create(canvasElement: HTMLCanvasElement | string)
send(objectName: string, methodName: string, params?: any) Sends a message to the UnityInstance to invoke a public method.
setFullscreen(enabled: boolean) Enables or disabled the fullscreen mode of the UnityInstance.
destroy() Quits the Unity Instance and clears it from memory.
on(eventName: string, eventListener: Function) Use instance.on() to register a method for Unity-script call.
once(eventName: string, eventListener: Function)
off(eventName: string)
clear()
emit(eventName: string)
Events
progress loading progress.
unityContext.on('progress',(number)=>{})
loaded loading completed.
unityContext.on('loaded',()=>{})
created Unity Instance is created.
unityContext.on('created',()=>{})
destroyed Quits the Unity Instance and clears it from memory.
// # in webAppconstUnity=newUnityWebgl()// Register functionsUnity.on('showDialog',(data)=>{console.log(data)$('#dialog').show()})// you also can call function.Unity.emit('showDialog',data)// orwindow[Unity.global_name].showDialog(data)// 📢 Unity.global_name = __UnityLib__
In the Unity project, add the registered showDialog method to the project, and then call those functions directly from your script code. To do so, place files with JavaScript code using the .jslib extension under a “Plugins” subfolder in your Assets folder. The plugin file needs to have a syntax like this:
// javascript_extend.jslibmergeInto(LibraryManager.library,{// this is you codeshowDialog: function(str){vardata=Pointer_stringify(str);// '__UnityLib__' is a global function collection.__UnityLib__.showDialog(data);},Hello: function(){window.alert("Hello, world!");}});
Then you can call these functions from your C# scripts like this:
usingUnityEngine;
usingSystem.Runtime.InteropServices;
publicclassNewBehaviourScript : MonoBehaviour {
[DllImport("__Internal")]
privatestaticexternvoidHello();
[DllImport("__Internal")]
privatestaticexternvoidshowDialog(stringstr);
voidStart() {
Hello();
showDialog("This is a string.");
}
}
2. Calling Unity scripts functions from JavaScript
使用 JavaScript 调用 Unity 脚本函数
constUnity=newUnityWebgl()/** * Sends a message to the UnityInstance to invoke a public method. * @param {string} objectName Unity scene name. * @param {string} methodName public method name. * @param {any} params an optional method parameter. */Unity.send(objectName,methodName,params)// e.g. Initialize Building#001 dataUnity.send('mainScene','init',{id: 'b001',name: 'building#001',length: 95,width: 27,height: 120})
Everything is working fine except communication from unity to vue. I want to listen on a method listenToUnity() in Unity. My code in Vue looks like this:
Unity.on("listenToUnity", () => console.log("I am listening to unity."));
What do I have to do in Unity? Your docu says __UnityLib__.showDialog(data), but I dont understand what I should do..
We have an app with different games. User can select a game from map-page.
Each game uses unity. We have a wrapper component for unity.
The problem is that when you open the NEXT game unity build loads twice.
So, for example, you open the first game (unity build loads). Then you go to map-page and choose another game. When this another game page is opened the build loads twice for some reason.
Hello, I created a game with unity and WebGL. Everything works fine on every device except for iOS. For some reason when i try to emit some values from unity to a function in javascript IOS return me an error and unity crashes. The error: __UnityLib__unityStarted() is not a function unityStarted is my function tha needs to be called to communicate in javascript. Again it gives me this error only on an Ipad or Iphone.
For the frontend tecnologies i'am using Nuxt (vuejs)
When I want to access unityInstace attribute in UnityWebGL instance right in created event,I found the attribute is null. I need to use setTimeout to wait 1ms to unityInstace, which doesn't make sense. So I suggest this update.
Genshin Lyre Parser is a webapp that can generate a playable note list from almost any Genshin Impact lyre video. You can simply pick a file, drag it in the parser, process it, and enjoy your note list.