bkeiren / AwesomiumUnity

Third-party Awesomium wrapper for Unity3D, wrapping Awesomium's C++ API with a custom C# API and accompanying Unity MonoBehaviour components.
108 stars 74 forks source link

Webtexture with Canvas Renderer & AwesomiumUnity.DLL #6

Open TheMajorO opened 9 years ago

TheMajorO commented 9 years ago

Hey @bkeiren , thank you so much for this awesome refactored wrapper and for the decent features you implemented. Your instructions in README.MD led to a successful attempt of integrating Awesomium with Unity using your awesome wrapper on my 64-bit windows 7 and 32-bit Unity 5.1.

Though I would like to ask you the following things: -1. Would kindly allow integration of AwesomiumUnityWebTexture.CS component with Canvas Renderer? I can only use either two methods: (Plane or any object with mesh renderer, material and collider OR deprecated GUITexture) I tried changing AwesomiumUnityWebTexture.CS, namely "void Initalize()", by creating a sprite using Texture2D attribute "m_texture" and assigning that sprite to Image component in a Canvas-child-object but that didn't work properly; image was uninteractive(it did view the page though). Perhaps I should also change OnMouseDown()?

EDIT: webpage is indeed loadable after making changes to both "Initialize()" and "OnMouseDown()", but uninteractive. Here's the code for AwesomiumUnityWebTexture.Initialize() & AwesomiumUnityWebTexture.OnMouseDown()

    public void Initialize()
    {
    m_HasBeenInitialized = true;

    AwesomiumUnityWebCore.EnsureInitialized();

    // Call resize which will create a texture and a webview for us if they do not exist yet at this point.
    Resize(m_Width, m_Height);
    var h = GetComponent<Image>();

    if (h)
    {
        var piv = GetComponent<RectTransform>().pivot;
        var myRect = new Rect();//GetComponent<RectTransform>().rect; produces a an error (current bug in unity 5.1.1f1)
        myRect.x = myRect.y = 0;
        myRect.width = m_Texture.width;
        myRect.height = m_Texture.height;
        var img = GetComponent<Image>();
        img.sprite = Sprite.Create(m_Texture, myRect, piv);
    }
    }
    //------------------------------------------OnMouseOver()------------------------------------------
    void OnMouseOver()
    {
    if (!m_Interactive) return;

    if (m_WebView != null /*&& !m_WebView.IsLoading*/)
    {
        //!--Maj
        var img = GetComponent<Image>();
        if (img)
        {
            float rectWidth =  img.rectTransform.rect.width;
            float rectHeight =  img.rectTransform.rect.height;
            float rectX =  img.rectTransform.rect.x;
            float rectY =  img.rectTransform.rect.y;

            int MouseX = (int)Input.mousePosition.x;
            int MouseY = Screen.height - (int)Input.mousePosition.y;

            MouseX = (int)(((MouseX - rectX) / rectWidth) * m_Width);
            MouseY = (int)(((MouseY - rectY) / rectHeight) * m_Height);

            m_WebView.InjectMouseMove(MouseX, MouseY);

        }
    }
    }

-2. Is it possible we view AwesomiumUnity.DLL? I couldn't decompile it since it's unmanaged C++ code. If it's a no then that is totally fine.

Again thank you so much for the hard work! I really hope you see this. I know you are really preoccupied but I believe in the potential of Awesomium and delighted with the easily understandable wrapper you wrote.

TheMajorO commented 8 years ago

Hey @HodgsonSDAS , Okay so after lots of researching and many attempts, it appears that we are going to have to stick to OnGUI for KB input injection. I REALLY wanted to do it just like how I did the mouse capture events and scroll events so we would inject KB input ONLY when the user actually presses a key. But unfortunately there is no support for that to this date in the new EventSystem (currently there are handlers for mouse events only, and other GUI-wise stuff).

Any hope with EventSystem class for KB input?

There is actually a way to do it using EventSystem (that still requires performing a loop :/ ), but it would interfere with other uGUI's components. Basically uGUI uses a loop to pop the current event (yes pop, meaning it's no longer there) from the event queue to do its operations on it for key detection and other stuff. Sadly there must be only ONE place in the code where you can do this, and as you can infer uGUI does this. https://bitbucket.org/Unity-Technologies/ui/src/fadfa14d2a5cb8d6462db067e669b4b2bc11a018/UnityEngine.UI/UI/Core/InputField.cs?at=4.6#InputField.cs-1014

Solution using OnGUI and Event class:

So bottom line is... we gonna have to inject KB events just like how @bkeiren did it:

    void OnGUI()
    {
        Event e = Event.current;

        if (e != null)
            switch (e.type)
            {
                case EventType.KeyDown:
                    {
                        if (!m_Interactive)
                            break;

                        if (m_HasFocus)
                        {
                            if (e.character == 0)
                            {
                                AwesomiumUnityWebKeyboardEvent keyEvent = new AwesomiumUnityWebKeyboardEvent();
                                keyEvent.Type = AwesomiumUnityWebKeyType.KeyDown;
                                keyEvent.VirtualKeyCode = MapKeys(e);
                                keyEvent.Modifiers = MapModifiers(e);
                                m_WebView.InjectKeyboardEvent(keyEvent);
                            }
                            else
                            {
                                AwesomiumUnityWebKeyboardEvent keyEvent = new AwesomiumUnityWebKeyboardEvent();
                                keyEvent.Type = AwesomiumUnityWebKeyType.Char;
                                keyEvent.Text = new ushort[] { e.character, 0, 0, 0 };
                                keyEvent.Modifiers = MapModifiers(e);
                                m_WebView.InjectKeyboardEvent(keyEvent);
                            }
                        }
                        break;
                    }
                case EventType.KeyUp:
                    {
                        if (!m_Interactive)
                            break;

                        if (m_HasFocus)
                        {
                            AwesomiumUnityWebKeyboardEvent keyEvent = new AwesomiumUnityWebKeyboardEvent();
                            keyEvent.Type = AwesomiumUnityWebKeyType.KeyUp;
                            keyEvent.VirtualKeyCode = MapKeys(e);
                            keyEvent.Modifiers = MapModifiers(e);
                            m_WebView.InjectKeyboardEvent(keyEvent);
                        }
                        break;
                    }
            }
    }

Pretty much this is the only way for the time being. I just tried it, and it worked perfectly... Solved the issues I had with my method. Those issues include the inability to perform keyrepeat nor use alternative keys (e.g. Shift-a is expected to produce an A).

I will be submitting a new pull request containing my code for uGUI's support including the other features I implemented in the following days after cleaning things up.

On another note, I'm quite excited about Awesomium's promised support for 64-bit architecture! And @bkeiren your JS delegation is just superb and out of this world! I had so much fun and success experimenting with very neat and advanced stuff. This is an area where multiple languages and technologies meet :D if it weren't you who took this initiative for building this professional and advanced native Awesomium wrapper we wouldn't be able to do anything from simply loading a web page to modifying the wrapper itself and adding features supported in the engine. Thank you man! :)

lighthousand commented 7 years ago

Hi, I'm using Unity 5.6.0 32-bit version on Windows 10 Enterprise. I wanted to ask if Unity integration needs the OpenGL to be installed. I'm trying to render the web page browser on a plane that has a Mesh Collider, a Mesh Renderer and the Awesomium Unity Web Texture script attached to it, but it does not render. I followed the "Getting it to work in the Unity Editor" part but no results. I'm losing my will.....If you see this please at least let me know if there is no more support for this, today.

StephenHodgson commented 7 years ago

Pretty sure Awesomium support died off a few years ago.

There's still no support for 64 bit, which is now the default Unity Editor type.