Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of interaction between WebGL and web pages

Shulou Source: shulou.com Published: 2022-05-31 17:20:22 10月16日 Update

Today, the editor will share with you how to solve the problem of interaction between WebGL and web pages. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

(1) first of all, talk about the method of calling the page method by Unity.

First of all, you need to create a Plugins folder in the Asset directory of the project, and then create a .txt file in the folder. The name doesn't matter. Change the extension to .jslib after creation. The file should contain something similar to the following:

MergeInto (LibraryManager.library, {Hello: function () {window.alert ("Hello, world!");}, HelloString: function (str) {window.alert (Pointer_stringify (str));}, PrintFloatArray: function (array, size) {for (var I = 0; I)

< size; i++) console.log(HEAPF32[(array >

> 2) + I]);}, AddNumbers: function (x, y) {return x + y;}, StringReturnValueFunction: function () {var returnStr = "bla"; var bufferSize = lengthBytesUTF8 (returnStr) + 1; var buffer = _ malloc (bufferSize); stringToUTF8 (returnStr, buffer, bufferSize); return buffer;}, BindWebGLTexture: function (texture) {GLctx.bindTexture (GLctx.TEXTURE_2D, GL.textures [texture]);},})

Of these, only the second parameter of mergeInto can be modified, and the second parameter is an object that contains references to multiple methods (such as Hello (), BingdeWebGLTexture (), etc.) that can be introduced in Unity programming. The methods called within these methods (for example: wiindow.alert (), GLctx.bindTexture (), and so on) can be called in the future page.

Specific ways to introduce methods into Unity programming take C # as an example:

First, you need to introduce a namespace:

Using System.Runtime.InteropServices

Secondly, you need to write specific introduction code:

[DllImport ("_ _ Internal")] private static extern void Hello ()

Refer to the following code introduction and usage example

Using UnityEngine;using System.Runtime.InteropServices; public class NewBehaviourScript: MonoBehaviour {[DllImport ("_ Internal")] private static extern void Hello (); [DllImport ("_ Internal")] private static extern void HelloString (string str); [DllImport ("_ Internal")] private static extern void PrintFloatArray (float [] array, int size); [DllImport ("_ Internal")] private static extern int AddNumbers (int x, int y) [DllImport ("_ Internal")] private static extern string StringReturnValueFunction (); [DllImport ("_ Internal")] private static extern void BindWebGLTexture (int texture); void Start () {Hello (); HelloString ("This is a string."); float [] myArray = new float [10]; PrintFloatArray (myArray, myArray.Length) Int result = AddNumbers (5,7); Debug.Log (result); Debug.Log (StringReturnValueFunction ()); var texture = new Texture2D (0,0, TextureFormat.ARGB32, false); BindWebGLTexture (texture.GetNativeTextureID ());} (II) secondly, talk about how page methods call methods in Unity.

To put it simply, use unityInstance to send messages. The specific method is defined as follows:

UnityInstance.SendMessage (objectName, methodName, value)

The parameter objectName is the name of the object in the Unity scene list. Make sure that there is only one object with that name in the scene. Don't have the same name, or it will be messed up. MethodName is the name of the method that sends the message, and value is the parameter of the method. This parameter can be absent or, if any, an integer or a string.

The specific usage is as follows:

UnityInstance.SendMessage ('MyGameObject',' MyFunction'); unityInstance.SendMessage ('MyGameObject',' MyFunction', 5); unityInstance.SendMessage ('MyGameObject',' MyFunction', 'MyString')

But this unityInstance is an internal object (I don't know how to say this accurately, so let's say so for the time being. If you want to reference this object externally, please refer to the page code as follows:

Var myGameInstance = null; createUnityInstance (canvas, config). Then ((unityInstance) = > {myGameInstance = unityInstance;}); var SendCmd = function (funName) {myGameInstance.SendMessage ("ZongCai", funName);}

In this way, you use myGameInstance to get a reference to unityInstance, and you can use myGameInstance to send messages.

These are all the contents of this article entitled "how to solve the problem of interaction between WebGL and web pages". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Methods parameters objects files knowledge articles pages references code content names that is messages web pages problems methods only scenarios folders methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei MariaDB Linux MySQL