public void OnApplicationQuit()
 {
     // NOTE: Not really shutting down berkelium - it cant restart after destroy in the same process
     // (this happens every time you start/stop the game in the editor)
     //BerkeliumApi.destroy();
     BerkeliumContext.instance = null;
 }
    void Awake()
    {
        // force init of global berkelium context
        BerkeliumContext c = BerkeliumContext.Instance;

        // Create the texture that will represent the website (with optional transparency and without mipmaps)
        TextureFormat texFormat = transparency ? TextureFormat.ARGB32 : TextureFormat.RGB24;

        m_Texture = new Texture2D(width, height, texFormat, false);

        // Create the pixel array for the plugin to write into at startup
        m_Pixels = m_Texture.GetPixels(0);
        // "pin" the array in memory, so we can pass direct pointer to it's data to the plugin,
        // without costly marshaling of array of structures.
        m_PixelsHandle = GCHandle.Alloc(m_Pixels, GCHandleType.Pinned);

        // Save the texture ID
        m_TextureID = m_Texture.GetInstanceID();

        // Improve rendering at shallow angles
        m_Texture.filterMode = FilterMode.Trilinear;
        m_Texture.anisoLevel = 2;


        // Assign texture to the renderer
        if (renderer)
        {
            renderer.material.mainTexture = m_Texture;

            // Transparency?
            if (transparency)
            {
                renderer.material.shader = Shader.Find("Transparent/Diffuse");
            }
            else
            {
                renderer.material.shader = Shader.Find("Diffuse");
            }

            // The texture has to be flipped
            renderer.material.mainTextureScale = new Vector2(-1, 1);
        }
        // or gui texture
        else if (GetComponent(typeof(GUITexture)))
        {
            GUITexture gui = GetComponent(typeof(GUITexture)) as GUITexture;
            gui.texture = m_Texture;
        }
        else
        {
            Debug.Log("Game object has no renderer or gui texture to assign the generated texture to!");
        }

        // Create new web window
        BerkeliumApi.Window.create(m_TextureID, m_PixelsHandle.AddrOfPinnedObject(), transparency, width, height, url);

        // Paint callbacks
        m_setPixelsFunc    = new BerkeliumApi.SetPixelsFunc(this.SetPixels);
        m_applyTextureFunc = new BerkeliumApi.ApplyTextureFunc(this.ApplyTexture);
        BerkeliumApi.Window.setPaintFunctions(m_TextureID, m_setPixelsFunc, m_applyTextureFunc);

        // Set the external host callback (for calling Unity functions from javascript)
        m_externalHostFunc = new BerkeliumApi.ExternalHostFunc(this.OnExternalHost);
        BerkeliumApi.Window.setExternalHostCallback(m_TextureID, m_externalHostFunc);

        m_loadingStateChangedFunc = new BerkeliumApi.LoadingStateChangedFunc(this.OnLoadingStateChanged);
        BerkeliumApi.Window.setLoadingStateChanged(m_TextureID, m_loadingStateChangedFunc);
    }