I would like to to store a value within a .jslib file:
Assets/Plugins/WebGL/MyPlugin.jslib
mergeInto(LibraryManager.library, {
var a = 123;
Setvar: function (val) {
a = val;
},
Getvar: function () {
return a;
},
});
and set/get it with separate functions. Is that possible?
Or by creating a DOM element with assigned value?
I should also mention that with this technique, the variable is visible to all other libraries (jslib’s). If you need library specific variables, you may want to do this instead:
var LibraryMyPlugin = {
$MyData: {
myVar: 123,
},
Setvar: function (val) {
MyData.myVar = val;
},
Getvar: function () {
return MyData.myVar;
},
};
autoAddDeps(LibraryMyPlugin, '$MyData');
mergeInto(LibraryManager.library, LibraryMyPlugin);
Please help, somethinhg must be wrong with the below code.
I wanted this.gameObject to store the global value here. But SendMessage returns undefined instead of this.gameObject
Does anyone know how to fix?
I found a workaround by storing the value as the dom Element attribute, but there must be some more straightforwand way.
Hi, recently i am developing a webapp, and i want my variables to have like getters and setters, then i saw this thread, used this technique “$MyData: {}” for storing variables, at first it is great! so i adapt this technique to my plugin, and then i tried to make another plugin with same technique, with same name, but different properties, and when i tested it, there is a undefined error popping out on my browser, i read the code many times and yeah, i know there is no mispell in my plugins, and then i suspect that the “$MyData” of the other plugin is being accessed by the new plugin, not the “$MyData” of itself, so i have done a test and confirmed that “$MyData” of the other plugin is being accessed.
I think with this technique, it is still not possible to hide your variables to other jslibs.
And also i think it might be the way to access variables of other jslibs.
I had this exact same experience. I had two .jslib files that both define $Shared as a storage place for “static” variables within the jslib. But one appears to be overwriting the other so I keep getting “undefined” exception from one of the jslibs.
I believe the trick is to rename the $Shared to be unique for each .jslib file to prevent them from accidentally overwriting each other. For example, if use $Shared0 for one .jslib and $Shared1 for the other .jslib, then the problem goes away. (I recommend using the name of the .jslib file in the $Shared variable name to guarantee uniqueness.)
Of course, this doesn’t help with access control since one .jslib seems to be able to access the $Shared of another .jslib, but like with all access control, we’ll have to live with “don’t do it” until Unity fixes it.
When the function is compiled, it is converted by adding an ‘_’ (underscore) to the beginning of the function name.
So I think you can call it like this: