Search Unity

How to support Mraid on Project Tiny

Discussion in 'Project Tiny' started by Solal_, Apr 22, 2021.

  1. Solal_

    Solal_

    Joined:
    Jan 13, 2019
    Posts:
    13
    B_Maxime likes this.
  2. tonialatalo

    tonialatalo

    Joined:
    Apr 23, 2015
    Posts:
    59
    The old, first Tiny from 2018 was a Typescript / Javascript lib, completely different tech from current Tiny. That's why the code there is Javascript:
    Code (JavaScript):
    1. export class PlayableAdsTriggerSystem extends ut.ComponentSystem {
    2.         OnUpdate(): void {
    The latter link gives the solution: you can use a browser JS lib, and call that from your Tiny C#, which is documented in the link given in the post there, https://docs.google.com/document/d/...HfnAX-CpYLK3aOdwA/edit#heading=h.6u7xyqroin5d

    That Getting Started doc is the Tiny doc in general too.

    So you need a JS lib to do the mraid biz, and use the Tiny interop system to integrate it with your C# code:
    Code (JavaScript):
    1.  
    2. mergeInto(LibraryManager.library, {
    3.   MyJavaScriptFunction1: function (...) {
    4.     ...
    5.   },
    6.   MyJavaScriptFunction2: function (...) {
    7.     ...
    8.   },
    9.   ...
    10.   MyJavaScriptFunctionN: function (...) {
    11.     ...
    12.   },
    13. });
    For parameters you need to convert the types, the doc has nice examples:
    Code (JavaScript):
    1. mergeInto(LibraryManager.library, {
    2.  
    3.   // this function prints a “Hello” message to the console
    4.   PrintHello: function () {
    5.     console.log("[JavaScript] PrintHello: " + "Hello");
    6.   },
    7.  
    8.   // this function prints a string, provided as an argument, to the console
    9.   PrintString: function (pStr) {
    10.     var str = UTF8ToString(pStr); // create a JavaScript string from a null-terminated UTF-8 string allocated on the heap, pointed by pStr
    11.     console.log("[JavaScript] PrintString: " + str);
    12.   },
    13.  
    14.   // this function adds two numbers and returns the result
    15.   AddNumbers: function (x, y) {
    16.     console.log("[JavaScript] AddNumbers: " + x + " + " + y);
    17.     return x + y;
    18.   },
    Then you declare your JS functions in C# like this and they are usable in Unity:
    Code (CSharp):
    1. using System;
    2. using Unity.Entities;
    3. using System.Runtime.InteropServices;
    4.  
    5. public class MySystem : SystemBase
    6. {
    7.  
    8.     [DllImport("__Internal")]
    9.     private static extern void PrintHello();
    10.  
    11.     [DllImport("__Internal")]
    12.     private static extern void PrintString(string str);
    13.  
    14.     [DllImport("__Internal")]
    15.     private static extern int AddNumbers(int x, int y);
    16.  
    17.     [DllImport("__Internal")]
    18.     private static extern string ReceiveString();
     
    Solal_ likes this.
unityunity