[ad_1]
Right here is the state of affairs,
I’ve a GetImageAerial.jslib which has perform one thing like this
var GetImageAerial = {
getImageFromBrowserAerial: perform(objectNameAerialPtr, funcNameAerialPtr) {
var g = window.becauseUnitysBadWithJavacript_getImageFromBrowserAerial || {
busy: false,
initialized: false,
rootDisplayStyle: null,
root: null,
ctx: null
};
window.becauseUnitysBadWithJavacript_getImageFromBrowserAerial = g;
if (g.busy) {
return;
}
g.busy = true;
var objectNameAerial = UTF8ToString(objectNameAerialPtr);
var funcNameAerial = UTF8ToString(funcNameAerialPtr);
if (!g.initialized) {
g.initialized = true;
g.ctx = doc.createElement("canvas").getContext("second");
g.root = doc.createElement("div");
g.root.innerHTML = [
'<style>',
'.getimage {',
' position: absolute;',
' left: 0;',
' top: 0;',
' width: 100%;',
' height: 100%;',
' display: flex;',
' flex-flow: column;',
' justify-content: center;',
' align-items: center;',
' z-index: 2;',
' color: white;',
' background-color: rgba(0,0,0,0.8);',
' font: sans-serif;',
' font-size: x-large;',
'}',
'.getimage a, .getimage label {',
' background-color: #666;',
' border-radius: 0.5em;',
' border: 1px solid black;',
' padding: 0.5em;',
' margin: 0.25em;',
' outline: none;',
' display: inline-block;',
'}',
'.getimage input {',
' display: none;',
'}',
'</style>',
'<div class="getimage">',
' <div>',
' <label for="photo">click to choose an image</label>',
' <input id="photo" type="file" accept="image/*"/><br/>',
' <a>cancel</a>',
' </div>',
'</div>'
].be part of('n');
doc.physique.appendChild(g.root);
var enter = g.root.querySelector("enter");
enter.onchange = getPic;
enter.onclick = preventOtherClicks;
var label = g.root.querySelector("label");
label.onclick = preventOtherClicks;
var cancel = g.root.querySelector("a");
cancel.onclick = handleCancel;
var getImageElement = g.root.querySelector(".getimage");
getImageElement.onclick = handleCancel;
g.rootDisplayStyle = g.root.model.show;
}
g.root.model.show = g.rootDisplayStyle;
perform preventOtherClicks(evt) {
evt.stopPropagation();
}
perform getPic(evt) {
evt.stopPropagation();
var fileInput = evt.goal.recordsdata;
if (!fileInput || !fileInput.size) {
return sendError("no picture chosen");
}
var picURL = URL.createObjectURL(fileInput[0]);
var img = new Picture();
img.onload = perform() {
handleImageLoad(img, sendResult);
};
img.onerror = perform() {
sendError("Couldn't get picture");
};
img.src = picURL;
}
perform handleCancel(evt) {
evt.stopPropagation();
evt.preventDefault();
sendError("cancelled");
}
perform handleImageLoad(img, sendResultCallback) {
g.ctx.canvas.width = 256;
g.ctx.canvas.peak = 256;
g.ctx.drawImage(img, 0, 0, g.ctx.canvas.width, g.ctx.canvas.peak);
var dataUrl = g.ctx.canvas.toDataURL();
g.ctx.canvas.width = 1;
g.ctx.canvas.peak = 1;
sendResultCallback(dataUrl);
}
perform sendError(msg) {
sendResult("error: " + msg);
}
perform sendResult(end result) {
g.root.model.show = "none";
g.busy = false;
SendMessage(objectNameAerial, funcNameAerial, end result);
}
}
};
mergeInto(LibraryManager.library, GetImageAerial);
I’ve a button name that calls this JS perform from C# script which is connected to a gameobject.
public void CallImageAerial()
{
Debug.Log("Name Picture Aerial:" + gameObject.identify);
GetImageAerial.GetImageFromUserAsyncAerial(gameObject.identify, "ReceiveImageAerial");
TurnOffAllButtons();
}
string s_dataUrlPrefix = "knowledge:picture/png;base64,";
public void ReceiveImageAerial(string dataUrl)
{
if (dataUrl.StartsWith(s_dataUrlPrefix))
{
byte[] pngData = System.Convert.FromBase64String(dataUrl.Substring(s_dataUrlPrefix.Size));
// Create a brand new Texture (or use some previous one?)
Texture2D tex = new Texture2D(1, 1); // does the scale matter?
if (tex.LoadImage(pngData))
{
//Renderer renderer = GetComponent<Renderer>();
aerialRenderer.rework.gameObject.SetActive(true);
aerialRenderer.materials.mainTexture = tex;
Debug.Log("Calling Obtain Aerial");
DoneAerial();
}
else
{
Debug.LogError("couldn't decode picture");
}
}
else
{
Debug.LogError("Error getting picture:" + dataUrl);
}
}
I’ve a category which has reference to the static extern void and imports the DLL
utilizing UnityEngine;
utilizing System.Collections;
utilizing System.Runtime.InteropServices;
public class GetImageAerial
{
#if UNITY_WEBGL
[DllImport("__Internal")]
personal static extern void getImageFromBrowserAerial(string objectName, string callbackFuncName);
#endif
static public void GetImageFromUserAsyncAerial(string objectName, string callbackFuncName)
{
#if UNITY_WEBGL
Debug.Log("Inside Get Picture " + callbackFuncName);
getImageFromBrowserAerial(objectName, callbackFuncName);
#else
Debug.LogError("Not applied on this platform");
#endif
}
}
The issue I’ve right here is that I do name this JS perform a number of instances and what I need to do is replace the objectNameAerial and funcNameAerial in consecutive calls however that does not occur. Javascript at all times preserves the primary worth of the variable that was set. I used to be pondering of setting native variables, however I can not technically do this. Any strategies are welcome! Been caught at this for too lengthy!
Thanks!
[ad_2]