-1

I'm developing an Unity app which can use bookmark buttons open multiple internal browser windows. For now, I make the browser Prefab, and instantiate it for open multiples simultaneously. But the windows will overlay together, users won't know if it was opened a new window, or redirect in the existed window, unless they drag one away.

I tried a `for' loop to open multiple windows, but all the windows in the for loop count come out by one click, same url. Luckily, in the desired offset positions I want.

But it should be one click open a new window, another click open a new window offset to the previous one.

enter image description here

Here is my bookmark button script:

// Open the browser window instance with bookmark button
public void OpenLinks(string link)
{
    for (int i = 0; i<=10; i++)
    {
        GameObject browser = Instantiate(browserWindow, new Vector2(i * 50 + 50, -(i * 50) - 50), browserWindow.transform.rotation);
        browser.SetActive(true);
        browser.GetComponent<SimpleWebBrowser.WebBrowser2D>().Navigate(link);
        browser.transform.parent = Windows.transform;
        browser.name = "Page - " + link;
    }
}

I assume that maybe there needs an if statement rather than for loop. But I don't know how to. because I need to keep the instantiate in the OpenLinks(), so that I can destroy the instances later to release the memory, and reopen again.

Can anyone help me?

Milan Egon Votrubec
  • 3,696
  • 2
  • 10
  • 24
Chenchen
  • 35
  • 6

1 Answers1

0

It looks like what you want to do is open a single window at a time, and store that window instance. Each window is then opened in the next position, so you need to store the value where the next window needs to open, either by storing the last position, or the next. In the example code below, we store where the window should open next.

There's a lot of extra code that will need to go in, such as what happens when the next window to open is off the screen and really needs to code back around to the top of the screen again, but we'll leave that for now with just a quick check for hard coded positions.

With that being said, here's a an example piece of code that should do what you're looking for:

public class Test : MonoBehaviour
{
    // Stubs ...
    public GameObject browserWindow;
    public GameObject Windows;

    private Vector2 _nextWindowPosition = new ( 50f, -50f );
    private Dictionary <int, SimpleWebBrowser.WebBrowser2D> _openWindows = new();

    // Open the browser window instance with bookmark button
    public void OpenLink ( string link )
    {
        var browser = Instantiate(browserWindow, _nextWindowPosition, browserWindow.transform.rotation);
        browser.SetActive ( true );
        if ( !browser.TryGetComponent<SimpleWebBrowser.WebBrowser2D> ( out var webBrowser ) )
        {
            Debug.LogError ( $"Could not get SimpleWebBrowser.WebBrowser2D for the new window." );
            Destroy ( browser );
            return;
        }

        browser.transform.parent = Windows.transform;
        browser.name = "Page - " + link;
        webBrowser.Navigate ( link );

        _nextWindowPosition += new Vector2 ( 50f, -50f );
        if ( _nextWindowPosition.x > 500 )
            _nextWindowPosition.x = 50f;
        if ( _nextWindowPosition.y < -500 )
            _nextWindowPosition.y = -50f;

        _openWindows.Add ( browser.GetInstanceID (), webBrowser );
    }

    public void CloseWindow ( int guid )
    {
        if ( !_openWindows.TryGetValue ( guid, out var webBrowser ) )
        {
            Debug.LogError ( $"Could not find window with GUID {guid}." );
            return;
        }

        Debug.Log ( $"Destroying window '{webBrowser.name}'." );
        _openWindows.Remove ( guid );
        Destroy ( webBrowser );
    }
}
Milan Egon Votrubec
  • 3,696
  • 2
  • 10
  • 24
  • instead of that `TryGetComponent` you should rather`directly make `browserWindow` of type `SimpleWebBrowser.WebBrowser2D` so it is ensured that the prefab actually has that component and you don't need to use `GetComponent` at all since `Instantiate` will already return the component – derHugo Dec 02 '22 at 08:39
  • also `> 500` / `< -500` are quite arbitrary .. either expose them in the Inspector or rather use something based on the `Screen.width` and `Screen.height` and the actual dimensions of the browser ... so e.g. like `if(_nextWindowPosition.x > Screen.width - browser.GetComponent().rect.width)` and `if(_nextWindowPosition.y < browser.GetComponent().rect.height)` .. or depending on the use case instead of `Screen` rather based on the `Windows.GetComponent().rect` – derHugo Dec 02 '22 at 08:42
  • @derHugo “ There's a lot of extra code that will need to go in, such as what happens when the next window to open is off the screen and really needs to code back around to the top of the screen again, but we'll leave that for now with just a quick check for hard coded positions.” – Milan Egon Votrubec Dec 02 '22 at 11:54