unityapp
If you are creating any project in UnityWebGL, then you may need to get some data from another framework. Similarly, if you want to send data from unity WebGL scripts to another framework, then you need some code in your unity WebGL project.
Transition of data:
1. Get data from another framework in unity webgl scripts.
2. Send data from unity webgl to another framework.
Explanation and implementation 1:- Unity webgl receives only string data from another framework so you have to modified data with stringify. And for getting the data from another framework to unity webgl a predefined syntax will follow you can prefer below example.
Example :-
function NameOfFunction(recievedDataParameter) //recievedDataParameter here this data is a JSON type.
{
console.log(recievedDataParameter); // by this code you can print the data and see the browser console.
if(myGameInstance!=null) // myGameInstance is a object for details see the end of the document.
{
myGameInstance.SendMessage("Parameter1","Parameter2",Parameter3);
}
}
info of above function:-
SendMessage is a predefined syntax for interaction get data from another framework.
Parameter1 this is the name of object in your unity project hierarchy which have the attached script. Note:- this GameObject name will pass as a string parameter in the function.
Parameter2 this is the name of the function where you want to receive data and this function must be written in attached script which is add on above GameObject in hierarchy. Note:- this function name pass also as a string parameter in the function.
Parameter3 this parameter is an optional means if you don't want to receive any data from another framework then no need to write it, if need then syntax will be like: JSON.stringify(data).
so the above function code like this :- myGameInstance.SendMessage("GameObjectName","FunctionName",JSON.stringify(data));
Explanation and implementation 2:- Unity webgl send data by one or multi parameter, modified data with stringify. For implementation this you need a file with .jslib extension to make an interface between unity c# script to build index.html file. to implement this you need so write function in unity c# script, .jslib file, and build index.html file.
Firstly unity C# code:- This code you have to write in your class for use DLLImport you need to using "using System.Runtime.InteropServices;" this.
Example:-
[DllImport("__Internal")] // predefined syntax
private static extern void FunctionName(string parameter); // this function name must be same as we will write in jslib file.
Now unity .jslib code:- This code you have to write in your .jslib file which are available in plugin folder in unity project if not available then you have to create one.
Example:-
FunctionName: function (Parameter) // this function name must be same as unity C# function name
{
window.HtmlFunctionName(Pointer_stringify(Parameter)); // this function name must be same as html file function name.
}
Now unity build index.html code:- This code you have to write in your index.html file which are available WebGLTemplates folder or you can create new one.
Example:-
window.HtmlFunctionName=function(parameter) // this function name must be same as .jslib file calling function name
{
window.parent.AnotherFrameworkFunctionName(parameter); // this function name must be same as another framework function which is call from here.
}
Note:-myGameInstance this is an object of webgl (regarding code) will find in index.html file when you create a build for webgl or you can find WebGLTemplates folder in unity project
Ref:-https://docs.unity3d.com/ScriptReference/
We are a 360-degree ERP app development company that builds custom enterprise solutions from the ground-up for varying business needs. Our seasoned developers have ample experience in building feature-rich business applications that are easy to scale. To learn more about our custom ERP development services, drop us a line at [email protected].