Beispiel #1
0
 public void Start()
 {
     Debug.Log("****INIT()");
     instance = this;
     ULBridge.ulbridge_init(false);
     ULBridge.ulbridge_set_command_callback(this.ProcessCallback);
     RegisterCallback("log", val => Debug.Log("JS: " + val));
 }
Beispiel #2
0
 public void ExecJavascript(string js)
 {
     if (created)
     {
         ULBridge.ulbridge_view_eval_script(gameObject.name, js);
     }
     else
     {
         javascript        = js;
         execJavascriptNow = true;
     }
 }
Beispiel #3
0
    private ViewData GetView(bool force = false)
    {
        ViewData res = new ViewData {
        };
        var rawDirty = ULBridge.ulbridge_view_is_dirty(gameObject.name);

        if (!force && !rawDirty)
        {
            res.changed = false;
            return(res);
        }
        // The texture can have a different size than the one requested,
        // but it will be black(unfilled) outside our requested size.
        int nw;
        int nh;
        int stride;
        var pixels = ULBridge.ulbridge_view_get_pixels(gameObject.name, out nw, out nh, out stride);

        unsafe {
            if (pixels.ToPointer() == null)
            {
                return(null);
            }
        }
        res.w          = width;
        res.h          = height;
        res.viewWidth  = width;
        res.viewHeight = height;
        res.data       = new Color32[res.w * res.h];
        unsafe {
            var scolors = (Color32 *)pixels.ToPointer();
            for (int y = 0; y < res.viewHeight; ++y)
            {
                var start = scolors + y * stride / 4;
                for (int x = 0; x < res.viewWidth; ++x)
                {
                    res.data[x + (res.viewHeight - y - 1) * res.viewWidth] = start[x];
                }
            }
        }
        ULBridge.ulbridge_view_unlock_pixels(gameObject.name);
        res.changed = true;
        return(res);
    }
Beispiel #4
0
 public void Update()
 {
     ULBridge.ulbridge_update();
     ULBridge.ulbridge_render();
 }
Beispiel #5
0
    void OnGUI()
    {
        if (!isGui || !created)
        {
            return;
        }
        var vd = GetView(tex == null);

        if (vd == null || vd.data == null || !vd.changed)
        {
            if (tex == null)
            {
                return;
            }
        }
        else
        {
            vd.changed = false;
            if (tex == null || tex.width != vd.w || tex.height != vd.h)
            {
                Debug.Log($"Creating texture {vd.w} {vd.h}");
                tex = new Texture2D(vd.w, vd.h);
            }
            tex.SetPixels32(vd.data);
            tex.Apply();
        }
        x = 0;
        y = 0;
        if (anchorX == Anchor.AnchorCenter)
        {
            x = Screen.width / 2 - tex.width / 2;
        }
        if (anchorX == Anchor.AnchorEnd)
        {
            x = Screen.width - tex.width;
        }
        if (anchorY == Anchor.AnchorCenter)
        {
            y = Screen.height / 2 - tex.height / 2;
        }
        if (anchorY == Anchor.AnchorEnd)
        {
            y = Screen.height - tex.height;
        }
        x += offsetX;
        y += offsetY;
        GUI.DrawTexture(new Rect(x, y, width, height), tex, ScaleMode.StretchToFill, true);

        var mp   = Input.mousePosition;
        var isIn = (mp.x >= x && mp.y >= y && mp.x <= x + tex.width && mp.y <= y + tex.height);

        if (isIn)
        {
            var ev = Event.current;
            if (ev.type == EventType.KeyDown || ev.type == EventType.KeyUp)
            {
                Debug.Log("key event");
                int vcode = 0;
                if (keycodeMap.TryGetValue(ev.keyCode, out vcode))
                { // we can map this
                    var mods = 0;
                    if (ev.shift)
                    {
                        mods |= (1 << 3);
                    }
                    if (ev.alt)
                    {
                        mods |= (1 << 0);
                    }
                    if (ev.control)
                    {
                        mods |= (1 << 1);
                    }
                    if (ev.command)
                    {
                        mods |= (1 << 2);
                    }
                    ULBridge.ulbridge_view_key_event(gameObject.name,
                                                     ev.type == EventType.KeyDown ? 1 : 0,
                                                     vcode, mods);
                }
            }
        }
    }
Beispiel #6
0
 void Update()
 {
     if (!created)
     {
         created = true;
         ULBridge.ulbridge_view_create(gameObject.name, width, height);
         ULBridge.ulbridge_view_load_url(gameObject.name, url);
     }
     if (loadUrlNow)
     {
         loadUrlNow = false;
         ULBridge.ulbridge_view_load_url(gameObject.name, url);
     }
     if (execJavascriptNow)
     {
         execJavascriptNow = false;
         ULBridge.ulbridge_view_eval_script(gameObject.name, javascript);
     }
     if (!isGui)
     {
         var vd = GetView();
         if (vd != null && vd.data != null && vd.changed)
         {
             vd.changed = false;
             if (tex == null || tex.width != vd.w || tex.height != vd.h)
             {
                 tex = new Texture2D(vd.w, vd.h);
             }
             tex.SetPixels32(vd.data);
             tex.Apply();
             SpriteRenderer sr = GetComponent <SpriteRenderer>();
             sr.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height),
                                       new Vector2(0, 0), 70.0f);
         }
     }
     if (tex != null && isGui)
     {
         var mp = Input.mousePosition;
         mp.y = Screen.height - mp.y;
         var isIn = (mp.x >= x && mp.y >= y && mp.x <= x + tex.width && mp.y <= y + tex.height);
         if (isIn)
         {
             var emited = false;
             for (int b = 0; b < 3; ++b)
             {
                 if (Input.GetMouseButtonDown(b))
                 {
                     Debug.Log("button down");
                     ULBridge.ulbridge_view_mouse_event(gameObject.name, (int)mp.x - x, (int)mp.y - y, 1, b + 1);
                     emited = true;
                 }
                 if (Input.GetMouseButtonUp(b))
                 {
                     Debug.Log("button up");
                     emited = true;
                     ULBridge.ulbridge_view_mouse_event(gameObject.name, (int)mp.x - x, (int)mp.y - y, 2, b + 1);
                 }
             }
             if (!emited)
             {
                 ULBridge.ulbridge_view_mouse_event(gameObject.name, (int)mp.x - x, (int)mp.y - y, 0, 0);
             }
             if (Input.mouseScrollDelta.y != 0)
             {
                 ULBridge.ulbridge_view_scroll_event(gameObject.name, 0, (int)Input.mouseScrollDelta.y, 1);
             }
         }
     }
 }
Beispiel #7
0
 public void LoadURL(string url)
 {
     ULBridge.ulbridge_view_load_url(gameObject.name, url);
 }