├── Plugins └── Unity3DWebGLInterface.jslib └── Unity3DWebGLInterface.cs /Plugins/Unity3DWebGLInterface.jslib: -------------------------------------------------------------------------------- 1 | mergeInto(LibraryManager.library, { 2 | /*NOTE: Most js objects returned by the jslib methods are not serializable\stringifiable and return empty strings to c#. Hence, it's not useful to return 3 | the entire object to c# unless a custom serialization functions is written first. 4 | Regardless of usefulness\useslessness when called from c#, the methdos must be imported in c# if low-stripping is enabled and then called if 5 | high-stripping is enabled. If not, Unity will strip them and any dependent functions in the jslib will throw "function not found" exceptions 6 | 7 | NOTE: When methods are called from c# and pass a string, a buffer location is provided instead of the string, whereas JS just passes the string. 8 | This is why why check typeof === 'number' and then use Pointer_stringif 9 | */ 10 | consoleLogThis: function(){ 11 | //It's the page object. Just noting what "this" is, in the context of a jslib in UNity JS. 12 | console.log("This:"); 13 | console.log(this); 14 | }, 15 | getGameInstance: function(debug, gameInstanceName, onlyReturnData){//Returned JS OBJECT returns empty string to C# 16 | gameInstanceName = typeof gameInstanceName == 'number' ? Pointer_stringify(gameInstanceName) : gameInstanceName; 17 | 18 | //In the majority of cases there will only be one webgl player on the page and it is named gameInstance - the default Unity template 19 | //So, get that instance if the name is empty. But, return an empty name so the other functions can essentially call "this" when the name is empty 20 | var name = gameInstanceName === '' ? "gameInstance" : gameInstanceName; 21 | var game = {name: gameInstanceName, instance: window[name]}; 22 | if(debug){ 23 | console.log("gameInstanceName:" + name); 24 | console.log(onlyReturnData ? game.instance : game); 25 | } 26 | return onlyReturnData ? game.instance : game; 27 | }, 28 | consoleLogGameInstance: function(gameInstanceName, onlyReturnData){ 29 | _getGameInstance(true, gameInstanceName, onlyReturnData); 30 | }, 31 | getGameInstanceModule: function(debug, gameInstanceName, onlyReturnData){//Returned JS OBJECT returns empty string to C# 32 | var game = _getGameInstance(false, gameInstanceName, false); 33 | game = {name:game.name, instance:game.instance.Module}; 34 | if(debug){ 35 | console.log("gameInstanceName:" + (game.name === '' ? 'THIS' : game.name) + " Module:"); 36 | console.log(onlyReturnData ? game.instance : game); 37 | } 38 | return onlyReturnData ? game.instance : game; 39 | }, 40 | consoleLogGameInstanceModule: function(gameInstanceName, onlyReturnData){ 41 | _getGameInstanceModule(true, gameInstanceName, onlyReturnData); 42 | }, 43 | //TODO Investigate module->backgroundColor, it might allow splash screen customizations 44 | getGameInstanceModuleAsmLibArg: function(debug, gameInstanceName, onlyReturnData){//Returned JS OBJECT returns empty string to C# 45 | var game = _getGameInstance(false, gameInstanceName, false); 46 | game = {name: game.name, instance: game.instance.Module.asmLibraryArg}; 47 | if(debug){ 48 | console.log("gameInstanceName:" + (game.name === '' ? 'THIS' : game.name) + " Module.asmLibraryArg:"); 49 | console.log(onlyReturnData ? game.instance : game); 50 | } 51 | return onlyReturnData ? game.instance : game; 52 | }, 53 | consoleLogGameInstanceModuleAsmLibArg: function(gameInstanceName, onlyReturnData){ 54 | _getGameInstanceModuleAsmLibArg(true, gameInstanceName, onlyReturnData); 55 | }, 56 | getGLctx: function(debug, gameInstanceName, onlyReturnData){//Returned JS OBJECT returns empty string to C# 57 | gameInstanceName = typeof gameInstanceName == 'number' ? Pointer_stringify(gameInstanceName) : gameInstanceName; 58 | var glctx = {name: gameInstanceName, instance: gameInstanceName === '' ? GLctx : window[gameInstanceName].Module["canvas"]["GLctxObject"]["GLctx"]}; 59 | if(debug){ 60 | console.log("GLctx gameInstanceName:" + (gameInstanceName === '' ? 'THIS' : gameInstanceName)); 61 | console.log(onlyReturnData ? glctx.instance : glctx); 62 | } 63 | return onlyReturnData ? glctx.instance : glctx; 64 | }, 65 | consoleLogGLctx: function(gameInstanceName, onlyReturnData){ 66 | _getGLctx(true, gameInstanceName, onlyReturnData); 67 | }, 68 | getOptObj: function(debug, gameInstanceName, optName, isCanvasOpt, onlyReturnData){//Returned JS OBJECT returns empty string to C# 69 | optName = typeof optName == 'number' ? Pointer_stringify(optName) : optName; 70 | var glctx = _getGLctx(false, gameInstanceName, false); 71 | var a = isCanvasOpt ? glctx.instance["canvas"] : glctx.instance; 72 | var b = isCanvasOpt ? glctx.instance["canvas"][optName] : glctx.instance[optName]; 73 | var opt = {name: glctx.name, option: optName, instance: optName === '' ? a : b}; 74 | 75 | if(debug){ 76 | console.log( (isCanvasOpt ? "Canvas " : "GLctx ") + optName + " typeOf: " + (typeof opt.instance) + " gameInstanceName:" + (opt.name === '' ? 'THIS' : opt.name)); 77 | console.log(onlyReturnData ? opt.instance : opt);//call separately just in case it's an object 78 | } 79 | return onlyReturnData ? opt.instance : opt; 80 | }, 81 | getCanvas: function(debug, gameInstanceName, onlyReturnData){//Returned JS OBJECT returns empty string to C# 82 | return _getOptObj(debug, gameInstanceName, "", true, onlyReturnData);//NOTE: the returned object is ready for manipulation with jQuery 83 | }, 84 | consoleLogCanvas: function(gameInstanceName, onlyReturnData){ 85 | _getCanvas(true, gameInstanceName, onlyReturnData); 86 | }, 87 | getWebGLCanvasParentElement: function(debug, gameInstanceName, onlyReturnData){//Returned JS OBJECT returns empty string to C# 88 | return _getOptObj(debug, gameInstanceName, "parentElement", true, onlyReturnData);//NOTE: the returned object is ready for manipulation with jQuery 89 | }, 90 | consoleLogWebGLCanvasParentElement: function(gameInstanceName, onlyReturnData){ 91 | _getWebGLCanvasParentElement(true, gameInstanceName, onlyReturnData); 92 | }, 93 | getWebGLCanvasParentNode: function(debug, gameInstanceName, onlyReturnData){//Returned JS OBJECT returns empty string to C# 94 | return _getOptObj(debug, gameInstanceName, "parentNode", true, onlyReturnData);//NOTE: the returned object is ready for manipulation with jQuery 95 | }, 96 | consoleLogWebGLCanvasParentNode: function(gameInstanceName, onlyReturnData){ 97 | _getWebGLCanvasParentNode(true, gameInstanceName, onlyReturnData); 98 | }, 99 | stringToBuffer: function(str, returnBuffer){ 100 | str = typeof str == 'number' ? Pointer_stringify(str) : str; 101 | str = typeof str !== 'undefined' ? str : "UNDEFINED";//Sometimes the attr doesn't exist and passes the value of undefined. An error is thrown without this 102 | if(returnBuffer){//Sometimes JS calls want the buffer and not the string, so we can't just return buffer if the call is from c# 103 | var bufferSize = lengthBytesUTF8(str) + 1; 104 | var buffer = _malloc(bufferSize); 105 | stringToUTF8(str, buffer, bufferSize); 106 | return buffer 107 | } 108 | return str; 109 | }, 110 | serializeOpt: function(debug, returnBuffer, gameInstanceName, optName, doSerialize, isCanvasOpt, onlyReturnData){ 111 | //If gameInstanceName is '' (ie empty) then this will return this game instance's info. Else, it will return that of the named gameInstance 112 | //http://thisinterestsme.com/json-stringify-empty-string/ 113 | //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify 114 | var opt = _getOptObj(false, gameInstanceName, optName, isCanvasOpt, false); 115 | var str = doSerialize ? jQuery( opt.instance ).serialize() : JSON.stringify( opt.instance );//Apply preferred serialization method 116 | str = opt.name === '' ? _stringToBuffer(str, returnBuffer) : window[opt.name].Module.asmLibraryArg._stringToBuffer(str, returnBuffer); 117 | opt = {name: opt.name, option: opt.option, instance: str}; 118 | if(debug){ 119 | console.log("SerializeOpt " + (isCanvasOpt ? "Canvas " : "GLctx ") + opt.option + " gameInstanceName:" + (opt.name === '' ? 'THIS' : opt.name)); 120 | console.log(onlyReturnData ? opt.instance : opt);//call separately just in case it's an object 121 | } 122 | return onlyReturnData ? opt.instance : opt; 123 | }, 124 | serializeCanvasOpt: function(debug, returnBuffer, gameInstanceName, optName, doSerialize, onlyReturnData){ 125 | return _serializeOpt(debug, returnBuffer, gameInstanceName, optName, doSerialize, true, onlyReturnData); 126 | }, 127 | serializeGameInstanceOpt: function(debug, returnBuffer, gameInstanceName, optName, doSerialize, onlyReturnData){ 128 | optName = typeof optName == 'number' ? Pointer_stringify(optName) : optName; 129 | 130 | var game =_getGameInstance(false, gameInstanceName, false); 131 | var opt = optName === '' ? game.instance : game.instance[optName];//It is really not advisable to serialize the entire game instance object as it adds a huge delay 132 | 133 | var str = doSerialize ? jQuery( opt ).serialize() : JSON.stringify( opt );//Apply preferred serialization method 134 | str = game.name === '' ? _stringToBuffer(str, returnBuffer) : window[game.name].Module.asmLibraryArg._stringToBuffer(str, returnBuffer); 135 | opt = {name: game.name, option: optName, instance: str}; 136 | if(debug){ 137 | console.log("SerializeGameOpt " + opt.option + " gameInstanceName:" + (opt.name === '' ? 'THIS' : opt.name)); 138 | console.log(onlyReturnData ? opt.instance : opt);//call separately just in case it's an object 139 | } 140 | return onlyReturnData ? opt.instance : opt; 141 | }, 142 | serializeGLctxOpt: function(debug, returnBuffer, gameInstanceName, optName, doSerialize, onlyReturnData){ 143 | return _serializeOpt(debug, returnBuffer, gameInstanceName, optName, doSerialize, false, onlyReturnData); 144 | }, 145 | webGLGLctxBindVertexArray: function(){/* Not sure if it's useful to access this, but here for completeness*/}, 146 | webGLGLctxCreateVertexArray: function(){/* Not sure if it's useful to access this, but here for completeness*/}, 147 | webGLGLctxDeleteVertexArray: function(){/* Not sure if it's useful to access this, but here for completeness*/}, 148 | webGLGLctxDisjointTimerQueryExt: function(){/* Not sure if it's useful to access this, but here for completeness*/}, 149 | webGLGLctxDrawArraysInstanced: function(){/* Not sure if it's useful to access this, but here for completeness*/}, 150 | webGLGLctxDrawElementsInstanced: function(){/* Not sure if it's useful to access this, but here for completeness*/}, 151 | getWebGLGLctxDrawingBufferHeight: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 152 | return _serializeGLctxOpt(debug, returnBuffer, gameInstanceName, "drawingBufferHeight", doSerialize, onlyReturnData); 153 | }, 154 | getWebGLGLctxDrawingBufferWidth: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 155 | return _serializeGLctxOpt(debug, returnBuffer, gameInstanceName, "drawingBufferWidth", doSerialize, onlyReturnData); 156 | }, 157 | webGLGLctxIsVertexArray: function(){/* Not sure if it's useful to access this, but here for completeness*/}, 158 | webGLGLctxVertexAttribDivisor: function(){/* Not sure if it's useful to access this, but here for completeness*/}, 159 | getGameInstanceUrl: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 160 | return _serializeGameInstanceOpt(debug, returnBuffer, gameInstanceName, "url", doSerialize, onlyReturnData); 161 | }, 162 | //TODO look into the logo under the gameInstance object...we maybe able to customize splash screen - doubt it, as i think it's in the build 163 | getWebGLCanvasAccessKey: function (debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData) { 164 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "accessKey", doSerialize, onlyReturnData); 165 | }, 166 | getWebGLCanvasAssignedSlot: function (debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData) { 167 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "assignedSlot", doSerialize, onlyReturnData); 168 | }, 169 | getWebGLCanvasAttributeStyleMap: function (debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData) { 170 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "attributeStyleMap", doSerialize, onlyReturnData); 171 | }, 172 | getWebGLCanvasAttributes: function (debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData) { 173 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "attributes", doSerialize, onlyReturnData); 174 | }, 175 | getWebGLCanvasAutoCapitalize: function (debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData) { 176 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "autocapitalize", doSerialize, onlyReturnData); 177 | }, 178 | getWebGLCanvasBaseURI: function (debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData) { 179 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "baseURI", doSerialize, onlyReturnData); 180 | }, 181 | getWebGLCanvasChildElementCount: function (debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData) { 182 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "childElementCount", doSerialize, onlyReturnData); 183 | }, 184 | getWebGLCanvasClassName: function (debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData) { 185 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "className", doSerialize, onlyReturnData); 186 | }, 187 | getWebGLCanvasClientHeight: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 188 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "clientHeight", doSerialize, onlyReturnData); 189 | }, 190 | getWebGLCanvasClientLeft: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 191 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "clientLeft", doSerialize, onlyReturnData); 192 | }, 193 | getWebGLCanvasClientTop: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 194 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "clientTop", doSerialize, onlyReturnData); 195 | }, 196 | getWebGLCanvasClientWidth: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 197 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "clientWidth", doSerialize, onlyReturnData); 198 | }, 199 | getWebGLCanvasDir: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 200 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "dir", doSerialize, onlyReturnData); 201 | }, 202 | getWebGLCanvasHeight: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 203 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "height", doSerialize, onlyReturnData); 204 | }, 205 | getWebGLCanvasHeightNative: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 206 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "heightNative", doSerialize, onlyReturnData); 207 | }, 208 | getWebGLCanvasId: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 209 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "id", doSerialize, onlyReturnData); 210 | }, 211 | getWebGLCanvasIsConnected: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 212 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "isConnected", doSerialize, onlyReturnData); 213 | }, 214 | getWebGLCanvasOffsetHeight: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 215 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "offsetHeight", doSerialize, onlyReturnData); 216 | }, 217 | getWebGLCanvasOffsetLeft: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 218 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "offsetLeft", doSerialize, onlyReturnData); 219 | }, 220 | getWebGLCanvasOffsetTop: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 221 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "offsetTop", doSerialize, onlyReturnData); 222 | }, 223 | getWebGLCanvasOffsetWidth: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 224 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "offsetWidth", doSerialize, onlyReturnData); 225 | }, 226 | getWebGLCanvasScrollHeight: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 227 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "scrollHeight", doSerialize, onlyReturnData); 228 | }, 229 | getWebGLCanvasScrollLeft: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 230 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "scrollLeft", doSerialize, onlyReturnData); 231 | }, 232 | getWebGLCanvasScrollTop: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 233 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "scrollTop", doSerialize, onlyReturnData); 234 | }, 235 | getWebGLCanvasScrollWidth: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 236 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "scrollWidth", doSerialize, onlyReturnData); 237 | }, 238 | getWebGLCanvasWidth: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 239 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "width", doSerialize, onlyReturnData); 240 | }, 241 | getWebGLCanvasWidthNative: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 242 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "widthNative", doSerialize, onlyReturnData); 243 | }, 244 | getWebGLCanvasTitle: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 245 | return _serializeCanvasOpt(debug, returnBuffer, gameInstanceName, "title", doSerialize, onlyReturnData); 246 | }, 247 | getWebGLCanvasParentAttr: function(debug, returnBuffer, gameInstanceName, attrName, doSerialize, onlyReturnData){ 248 | attrName = typeof attrName == 'number' ? Pointer_stringify(attrName) : attrName; 249 | 250 | var parent = _getWebGLCanvasParentElement(false, gameInstanceName, false); 251 | var attr = attrName === '' ? parent.instance : jQuery(parent.instance).attr(attrName); 252 | var str = doSerialize ? jQuery( attr ).serialize() : JSON.stringify( attr );//Apply preferred serialization method 253 | str = parent.name === '' ? _stringToBuffer(str, returnBuffer) : window[parent.name].Module.asmLibraryArg._stringToBuffer(str, returnBuffer); 254 | attr = {name:parent.name, option:attrName, instance:str}; 255 | if(debug){ 256 | console.log("CanvasParentAttr " + attr.option + " gameInstanceName:" + (attr.name === '' ? 'THIS' : attr.name)); 257 | console.log(onlyReturnData ? attr.instance : attr);//call separately just in case it's an object 258 | } 259 | return onlyReturnData ? attr.instance : attr; 260 | }, 261 | getWebGLCanvasParentId: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 262 | return _getWebGLCanvasParentAttr(debug, returnBuffer, gameInstanceName, "id", doSerialize, onlyReturnData); 263 | }, 264 | getWebGLCanvasParentName: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 265 | return _getWebGLCanvasParentAttr(debug, returnBuffer, gameInstanceName, "name", doSerialize, onlyReturnData); 266 | }, 267 | getWebGLCanvasParentClass: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 268 | return _getWebGLCanvasParentAttr(debug, returnBuffer, gameInstanceName, "class", doSerialize, onlyReturnData); 269 | }, 270 | getWebGLCanvasParentStyle: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 271 | return _getWebGLCanvasParentAttr(debug, returnBuffer, gameInstanceName, "style", doSerialize, onlyReturnData); 272 | }, 273 | getWebGLCanvasParentTitle: function(debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData){ 274 | return _getWebGLCanvasParentAttr(debug, returnBuffer, gameInstanceName, "title", doSerialize, onlyReturnData); 275 | }, 276 | getPageUrl: function (debug, returnBuffer, gameInstanceName, doSerialize, onlyReturnData) { 277 | gameInstanceName = typeof gameInstanceName == 'number' ? Pointer_stringify(gameInstanceName) : gameInstanceName; 278 | var href = window.top.location.href; 279 | var str = doSerialize ? jQuery( href ).serialize() : JSON.stringify( href );//Apply preferred serialization method 280 | str = gameInstanceName === '' ? _stringToBuffer(str, returnBuffer) : window[gameInstanceName].Module.asmLibraryArg._stringToBuffer(str, returnBuffer); 281 | str = {name: gameInstanceName, option: "PageUrl", instance: str}; 282 | if(debug){ 283 | console.log("PageUrl gameInstanceName:" + (str.name === '' ? 'THIS' : str.name)); 284 | console.log(onlyReturnData ? str.instance : str);//call separately just in case it's an object 285 | } 286 | return onlyReturnData ? str.instance : str; 287 | }, 288 | isMobile: function (debug) { 289 | if(debug){ 290 | console.log("Is Mobile:" + UnityLoader.SystemInfo.mobile); 291 | } 292 | return UnityLoader.SystemInfo.mobile; 293 | }, 294 | //TODO check if is mobile and if that mobile is supported - some are and some are not 295 | //TODO get settings - this assumes that settings are stored in a specific way on the parent element 296 | }); -------------------------------------------------------------------------------- /Unity3DWebGLInterface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Runtime.InteropServices; 5 | using UnityEngine; 6 | 7 | public class WebGLCanvasInterface : MonoBehaviour{ 8 | 9 | #if UNITY_WEBGL 10 | 11 | /*---------------------------------------- General Use -------------------------------------------------------*/ 12 | [DllImport("__Internal")] 13 | private static extern void consoleLogThis(); 14 | 15 | [DllImport("__Internal")] 16 | private static extern void getOptObj(bool debug, string gameInstanceName, string optName, bool isCanvasOpt, bool onlyReturnData); 17 | 18 | [DllImport("__Internal")] 19 | private static extern string serializeOpt(bool debug, bool returnBuffer, string gameInstanceName, string optName, bool doSerialize, bool isCanvasOpt, bool onlyReturnData); 20 | 21 | [DllImport("__Internal")] 22 | private static extern string serializeCanvasOpt(bool debug, bool returnBuffer, string gameInstanceName, string optName, bool doSerialize, bool onlyReturnData); 23 | 24 | [DllImport("__Internal")] 25 | private static extern string stringToBuffer(string str, bool returnBuffer); 26 | 27 | [DllImport("__Internal")] 28 | private static extern string getPageUrl(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 29 | 30 | [DllImport("__Internal")] 31 | private static extern bool isMobile(bool debug); 32 | 33 | /*---------------------------------------- gameInstance -------------------------------------------------------*/ 34 | [DllImport("__Internal")] 35 | private static extern void getGameInstance(bool debug, string gameInstanceName, bool onlyReturnData); 36 | 37 | [DllImport("__Internal")] 38 | private static extern void consoleLogGameInstance(string gameInstanceName, bool onlyReturnData); 39 | 40 | [DllImport("__Internal")] 41 | private static extern void getGameInstanceModule(bool debug, string gameInstanceName, bool onlyReturnData); 42 | 43 | [DllImport("__Internal")] 44 | private static extern void consoleLogGameInstanceModule(string gameInstanceName, bool onlyReturnData); 45 | 46 | [DllImport("__Internal")] 47 | private static extern void getGameInstanceModuleAsmLibArg(bool debug, string gameInstanceName, bool onlyReturnData); 48 | 49 | [DllImport("__Internal")] 50 | private static extern void consoleLogGameInstanceModuleAsmLibArg(string gameInstanceName, bool onlyReturnData); 51 | 52 | [DllImport("__Internal")] 53 | private static extern void serializeGameInstanceOpt(bool debug, bool returnBuffer, string gameInstanceName, string optName, bool doSerialize, bool onlyReturnData); 54 | 55 | [DllImport("__Internal")] 56 | private static extern string getGameInstanceUrl(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 57 | 58 | /*---------------------------------------- GLctx (Enscripten WebGL Context) -------------------------------------------------------*/ 59 | [DllImport("__Internal")] 60 | private static extern void getGLctx(bool debug, string gameInstanceName, bool onlyReturnData); 61 | 62 | [DllImport("__Internal")] 63 | private static extern void consoleLogGLctx(string gameInstanceName, bool onlyReturnData); 64 | 65 | [DllImport("__Internal")] 66 | private static extern string serializeGLctxOpt(bool debug, bool returnBuffer, string gameInstanceName, string optName, bool doSerialize, bool onlyReturnData); 67 | 68 | [DllImport("__Internal")] 69 | private static extern string getWebGLGLctxDrawingBufferHeight(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 70 | 71 | [DllImport("__Internal")] 72 | private static extern string getWebGLGLctxDrawingBufferWidth(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 73 | 74 | /*---------------------------------------- Canvas -------------------------------------------------------*/ 75 | [DllImport("__Internal")] 76 | private static extern void getCanvas(bool debug, string gameInstanceName, bool onlyReturnData); 77 | 78 | [DllImport("__Internal")] 79 | private static extern void consoleLogCanvas(string gameInstanceName, bool onlyReturnData); 80 | 81 | [DllImport("__Internal")] 82 | private static extern void getWebGLCanvasParentElement(bool debug, string gameInstanceName, bool onlyReturnData); 83 | 84 | [DllImport("__Internal")] 85 | private static extern void consoleLogWebGLCanvasParentElement(string gameInstanceName, bool onlyReturnData); 86 | 87 | [DllImport("__Internal")] 88 | private static extern void getWebGLCanvasParentNode(bool debug, string gameInstanceName, bool onlyReturnData); 89 | 90 | [DllImport("__Internal")] 91 | private static extern void consoleLogWebGLCanvasParentNode(string gameInstanceName, bool onlyReturnData); 92 | 93 | [DllImport("__Internal")] 94 | private static extern string getWebGLCanvasAccessKey(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 95 | 96 | [DllImport("__Internal")] 97 | private static extern string getWebGLCanvasAssignedSlot(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 98 | 99 | [DllImport("__Internal")] 100 | private static extern string getWebGLCanvasAttributeStyleMap(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 101 | 102 | [DllImport("__Internal")] 103 | private static extern string getWebGLCanvasAttributes(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 104 | 105 | [DllImport("__Internal")] 106 | private static extern string getWebGLCanvasAutoCapitalize(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 107 | 108 | [DllImport("__Internal")] 109 | private static extern string getWebGLCanvasBaseURI(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 110 | 111 | [DllImport("__Internal")] 112 | private static extern string getWebGLCanvasChildElementCount(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 113 | 114 | [DllImport("__Internal")] 115 | private static extern string getWebGLCanvasClassName(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 116 | 117 | [DllImport("__Internal")] 118 | private static extern string getWebGLCanvasClientHeight(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 119 | 120 | [DllImport("__Internal")] 121 | private static extern string getWebGLCanvasClientLeft(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 122 | 123 | [DllImport("__Internal")] 124 | private static extern string getWebGLCanvasClientTop(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 125 | 126 | [DllImport("__Internal")] 127 | private static extern string getWebGLCanvasClientWidth(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 128 | 129 | [DllImport("__Internal")] 130 | private static extern string getWebGLCanvasDir(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 131 | 132 | [DllImport("__Internal")] 133 | private static extern string getWebGLCanvasHeight(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 134 | 135 | [DllImport("__Internal")] 136 | private static extern string getWebGLCanvasHeightNative(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 137 | 138 | [DllImport("__Internal")] 139 | private static extern string getWebGLCanvasId(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 140 | 141 | [DllImport("__Internal")] 142 | private static extern string getWebGLCanvasIsConnected(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 143 | 144 | [DllImport("__Internal")] 145 | private static extern string getWebGLCanvasOffsetHeight(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 146 | 147 | [DllImport("__Internal")] 148 | private static extern string getWebGLCanvasOffsetLeft(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 149 | 150 | [DllImport("__Internal")] 151 | private static extern string getWebGLCanvasOffsetTop(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 152 | 153 | [DllImport("__Internal")] 154 | private static extern string getWebGLCanvasOffsetWidth(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 155 | 156 | [DllImport("__Internal")] 157 | private static extern string getWebGLCanvasScrollHeight(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 158 | 159 | [DllImport("__Internal")] 160 | private static extern string getWebGLCanvasScrollLeft(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 161 | 162 | [DllImport("__Internal")] 163 | private static extern string getWebGLCanvasScrollTop(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 164 | 165 | [DllImport("__Internal")] 166 | private static extern string getWebGLCanvasScrollWidth(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 167 | 168 | [DllImport("__Internal")] 169 | private static extern string getWebGLCanvasWidth(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 170 | 171 | [DllImport("__Internal")] 172 | private static extern string getWebGLCanvasWidthNative(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 173 | 174 | [DllImport("__Internal")] 175 | private static extern string getWebGLCanvasTitle(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 176 | 177 | [DllImport("__Internal")] 178 | private static extern string getWebGLCanvasParentAttr(bool debug, bool returnBuffer, string gameInstanceName, string attrName, bool doSerialize, bool onlyReturnData); 179 | 180 | [DllImport("__Internal")] 181 | private static extern string getWebGLCanvasParentId(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 182 | 183 | [DllImport("__Internal")] 184 | private static extern string getWebGLCanvasParentName(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 185 | 186 | [DllImport("__Internal")] 187 | private static extern string getWebGLCanvasParentClass(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 188 | 189 | [DllImport("__Internal")] 190 | private static extern string getWebGLCanvasParentStyle(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 191 | 192 | [DllImport("__Internal")] 193 | private static extern string getWebGLCanvasParentTitle(bool debug, bool returnBuffer, string gameInstanceName, bool doSerialize, bool onlyReturnData); 194 | 195 | private bool printMethodsOnUpdate = false; 196 | 197 | void Start(){ 198 | initializeJSLIB(); 199 | allMethods(); 200 | } 201 | 202 | // Update is called once per frame 203 | void Update(){ 204 | if(printMethodsOnUpdate){ 205 | allMethods(); 206 | } 207 | } 208 | 209 | public void togglePrintMethodsOnUpdate(){ 210 | printMethodsOnUpdate = !printMethodsOnUpdate; 211 | } 212 | private void initializeJSLIB(){ 213 | /* When jslib methods are called from c# or other jslib methods, the methdos must be imported in c# if low-stripping is enabled and then executed if 214 | high-stripping is enabled. If not, Unity will strip them and any dependent functions in the jslib will throw "function not found" exceptions. 215 | As long as this methods is called in Start, you won't have to worry about required functions gettings stripped as they are listed and handled below. 216 | 217 | Also, while you can call these functions on any gameInstance object, you should initialize the following functions for all gameInstance objects so that 218 | they are always accessible regardless of what is calling them 219 | */ 220 | getGameInstance(false, "", true);//Returned JS OBJECT returns empty string to C# 221 | getGameInstanceModule(false, "", true);//Returned JS OBJECT returns empty string to C# 222 | getGameInstanceModuleAsmLibArg(false, "", true);//Returned JS OBJECT returns empty string to C# 223 | getGLctx(false, "", true);//Returned JS OBJECT returns empty string to C# 224 | getOptObj(false, "", "", false, true);//Returned JS OBJECT returns empty string to C# 225 | getCanvas(false, "", true);//Returned JS OBJECT returns empty string to C# 226 | getWebGLCanvasParentElement(false, "", true);//Returned JS OBJECT returns empty string to C# 227 | getWebGLCanvasParentNode(false, "", true);//Returned JS OBJECT returns empty string to C# 228 | stringToBuffer("", true); 229 | serializeOpt(false, true, "", "drawingBufferHeight", false, false, true);//Use drawingBufferHeight to initialize the function to avoid a big delay due to attempting to serialize a large object - the canvas object 230 | serializeCanvasOpt(false, true, "", "baseURI", false, true);//Use baseURI to initialize the function to avoid a big delay due to attempting to serialize a large object - the canvas object 231 | } 232 | 233 | private void allMethods(){ 234 | /* The name of the JS variable used when instantiating the gameInstance. All of the functions work with any number of WebGL instances on the page, so 235 | long as you use unique variable names for each gameInstance (e.g. gameInstance1 and gameInstance2). If only one instance is used on the page, use the 236 | "" empty string for gameInstanceName and the functions will assume gameInstanceName = "gameInstance" like in all Unity examples. 237 | 238 | EX: Pass "gameInstance" for the standard unity webgl template 239 | var gameInstance = UnityLoader.instantiate(...) 240 | */ 241 | string gameInstanceName = "gameInstance"; 242 | 243 | consoleLogImportantJSObjects(gameInstanceName); 244 | gameInstanceMethods(gameInstanceName); 245 | glctxMethods(gameInstanceName); 246 | canvasMethods(gameInstanceName); 247 | canvasContainerMethods(gameInstanceName); 248 | otherUsefulMethods(gameInstanceName); 249 | } 250 | 251 | private void consoleLogImportantJSObjects(string gameInstanceName){ 252 | /* The following show useful objects in the browser console for debugging: 253 | -gameInstance : The entire gameInstance object for the object with the corresponding JS var name 254 | -gameInstance.Module : The gameInstance Module, which contains useful info like the asmLibraryArg, SendMessage function, TOTAL_MEMORY, and a lot more 255 | -gameInstance.Module.asmLibraryArg : Contains the functions from all .JSLIBs and a lot more 256 | -GLctx : The WebGL context object. It contains everything needed to interact with the WebGL, its canvas, and the canvas's container 257 | These are not required, just useful during development. 258 | */ 259 | consoleLogThis();//This ends up being the entire window object 260 | consoleLogGameInstance(gameInstanceName, true); 261 | consoleLogGameInstanceModule(gameInstanceName, true); 262 | consoleLogGameInstanceModuleAsmLibArg(gameInstanceName, true); 263 | consoleLogGLctx(gameInstanceName, true); 264 | } 265 | 266 | private void gameInstanceMethods(string gameInstanceName){ 267 | /* Methods for accessing top-level gameInstace objects and functions. The gameInstance object has more references but honestly, the most relevant option 268 | in this object is gameInstanceUrl. Most likely you'll only ever use Canvas options - in that case, look into 269 | the canvas functions. This is here for completeness just in case other useful info is added in the future. 270 | */ 271 | serializeGameInstanceOpt(false, true, gameInstanceName, "url", false, true);//You only need to call this if call one of the two functions below or create a functions that depends on it. The url option is here to prevent attempting to serialize the whole massive gameInstance object 272 | Debug.Log("C# GameInstanceUrl:" + getGameInstanceUrl(false, true, gameInstanceName, false, true)); 273 | } 274 | 275 | private void glctxMethods(string gameInstanceName){ 276 | /* Methods for accessing top-level GLctx objects and functions. The GLctx object has more references but honestly, the most relevant options 277 | in this object are drawingBufferHeight, drawingBufferWidth, and canvas. Most likely you'll only ever use Canvas options - in that case, look into 278 | the canvas functions. This is here for completeness just in case other useful info is added in the future. 279 | */ 280 | serializeGLctxOpt(false, true, gameInstanceName, "drawingBufferHeight", false, true);//You only need to call this if call one of the two functions below or create a functions that depends on it 281 | Debug.Log("C# GLctx DrawingBufferHeight:" + getWebGLGLctxDrawingBufferHeight(false, true, gameInstanceName, false, true)); 282 | Debug.Log("C# GLctx DrawingBufferWidth:" + getWebGLGLctxDrawingBufferWidth(false, true, gameInstanceName, false, true)); 283 | } 284 | 285 | private void canvasMethods(string gameInstanceName){ 286 | /* Useful methods for accessing canvas information */ 287 | consoleLogCanvas(gameInstanceName, true); 288 | Debug.Log("C# Canvas AccessKey:" + getWebGLCanvasAccessKey(false, true, gameInstanceName, false, true)); 289 | Debug.Log("C# Canvas AssignedSlot:" + getWebGLCanvasAssignedSlot(false, true, gameInstanceName, false, true)); 290 | Debug.Log("C# Canvas AttributesStyleMap:" + getWebGLCanvasAttributeStyleMap(false, true, gameInstanceName, true, true)); 291 | Debug.Log("C# Canvas Attributes:" + getWebGLCanvasAttributes(false, true, gameInstanceName, true, true)); 292 | Debug.Log("C# Canvas AutoCapitalize:" + getWebGLCanvasAutoCapitalize(false, true, gameInstanceName, false, true)); 293 | Debug.Log("C# Canvas BaseUri:" + getWebGLCanvasBaseURI(false, true, gameInstanceName, false, true)); 294 | Debug.Log("C# Canvas ChildElementCount:" + getWebGLCanvasChildElementCount(false, true, gameInstanceName, false, true)); 295 | Debug.Log("C# Canvas ClassName:" + getWebGLCanvasClassName(false, true, gameInstanceName, false, true)); 296 | Debug.Log("C# Canvas ClientHeight:" + getWebGLCanvasClientHeight(false, true, gameInstanceName, false, true)); 297 | Debug.Log("C# Canvas ClientLeft:" + getWebGLCanvasClientLeft(false, true, gameInstanceName, false, true)); 298 | Debug.Log("C# Canvas ClientTop:" + getWebGLCanvasClientTop(false, true, gameInstanceName, false, true)); 299 | Debug.Log("C# Canvas ClientWidth:" + getWebGLCanvasClientWidth(false, true, gameInstanceName, false, true)); 300 | Debug.Log("C# Canvas Dir:" + getWebGLCanvasDir(false, true, gameInstanceName, false, true)); 301 | Debug.Log("C# Canvas Height:" + getWebGLCanvasHeight(false, true, gameInstanceName, false, true)); 302 | Debug.Log("C# Canvas HeightNative:" + getWebGLCanvasHeightNative(false, true, gameInstanceName, false, true)); 303 | Debug.Log("C# Canvas ID:" + getWebGLCanvasId(false, true, gameInstanceName, false, true)); 304 | Debug.Log("C# Canvas IsConnected:" + getWebGLCanvasIsConnected(false, true, gameInstanceName, false, true)); 305 | Debug.Log("C# Canvas OffsetHeight:" + getWebGLCanvasOffsetHeight(false, true, gameInstanceName, false, true)); 306 | Debug.Log("C# Canvas OffsetLeft:" + getWebGLCanvasOffsetLeft(false, true, gameInstanceName, false, true)); 307 | Debug.Log("C# Canvas OffsetTop:" + getWebGLCanvasOffsetTop(false, true, gameInstanceName, false, true)); 308 | Debug.Log("C# Canvas OffsetWidth:" + getWebGLCanvasOffsetWidth(false, true, gameInstanceName, false, true)); 309 | Debug.Log("C# Canvas ScrollHeight:" + getWebGLCanvasScrollHeight(false, true, gameInstanceName, false, true)); 310 | Debug.Log("C# Canvas ScrollLeft:" + getWebGLCanvasScrollLeft(false, true, gameInstanceName, false, true)); 311 | Debug.Log("C# Canvas ScrollTop:" + getWebGLCanvasScrollTop(false, true, gameInstanceName, false, true)); 312 | Debug.Log("C# Canvas ScrollWidth:" + getWebGLCanvasScrollWidth(false, true, gameInstanceName, false, true)); 313 | Debug.Log("C# Canvas Width:" + getWebGLCanvasWidth(false, true, gameInstanceName, false, true)); 314 | Debug.Log("C# Canvas WidthNative:" + getWebGLCanvasWidthNative(false, true, gameInstanceName, false, true)); 315 | Debug.Log("C# Canvas Title:" + getWebGLCanvasTitle(false, true, gameInstanceName, false, true)); 316 | } 317 | 318 | private void canvasContainerMethods(string gameInstanceName){ 319 | /* Useful methods for accessing canvas container information */ 320 | consoleLogWebGLCanvasParentElement(gameInstanceName, true); 321 | consoleLogWebGLCanvasParentNode(gameInstanceName, true); 322 | 323 | /* Canvas Parent Attributes */ 324 | Debug.Log("C# Canvas ParentAttr-id:" + getWebGLCanvasParentAttr(false, true, gameInstanceName, "id", false, true));//Use id to initialize the function to avoid a big delay due to attempting to serialize a large object - the canvas parent object 325 | Debug.Log("C# Canvas ParentID:" + getWebGLCanvasParentId(false, true, gameInstanceName, false, true)); 326 | Debug.Log("C# Canvas ParentName:" + getWebGLCanvasParentName(false, true, gameInstanceName, false, true)); 327 | Debug.Log("C# Canvas ParentClass:" + getWebGLCanvasParentClass(false, true, gameInstanceName, false, true)); 328 | Debug.Log("C# Canvas ParentStyle:" + getWebGLCanvasParentStyle(false, true, gameInstanceName, false, true)); 329 | Debug.Log("C# Canvas ParentTitle:" + getWebGLCanvasParentTitle(false, true, gameInstanceName, false, true)); 330 | } 331 | 332 | private void otherUsefulMethods(string gameInstanceName){ 333 | Debug.Log("C# PageUrl:" + getPageUrl(true, true, gameInstanceName, false, true)); 334 | Debug.Log("C# IsMobile:" + isMobile(true)); 335 | } 336 | #endif 337 | } --------------------------------------------------------------------------------