Layer or hierarchy problem for GameObject

47 views Asked by At

In my Unity project, I made a prefab to rendering images from web sources by URL. However, when testing the app, it displayed the images like this:

enter image description here

There are two image windows, but you can see, the image in the first opened window is floating over the second opened window, but under the second image.

The window (ImageHolder) is a prefab I made, and in this prefab, I made another prefab (ImageBox) to rendering image. Here is the prefab hierarchy below.

enter image description here

Here is my script to call to open the instances, the look up imageExtensions part is the part to call this image window. the else part is open web sources with browser, this part works fine.

 void OpenLinks(string url, string name)
    {
        int l = 100; int m = -600;

        // Find if the media in URL with type of media via extension
        string extension = Path.GetExtension(url);

        // Array of media extensions
        string[] imageExtensions = { ".jpg", ".png", ".gif", "jpeg", "jfif", "svg", "webp" };
        string[] videoExtensions = { ".mp4", ".avi", ".mov", "webm" };
        string[] audioExtensions = { ".mp3", ".wav", ".aac" };

        if (Array.Exists(imageExtensions, element => extension.Equals(element, StringComparison.OrdinalIgnoreCase)))
        {
            GameObject image = Instantiate(imageHolder, _nextWindowPosition, imageHolder.transform.rotation);
            image.SetActive(true);

            // Find the imageBox component and add a SpriteRenderer component
            GameObject imageBox = image.transform.Find("ImageBackground/ImageBox").gameObject;
            SpriteRenderer spriteRenderer = imageBox.GetComponent<SpriteRenderer>();

            TextMeshProUGUI title = image.GetComponentInChildren<TextMeshProUGUI>();

            image.transform.SetParent(Windows.transform, false);
            image.name = name;
            title.text = name;

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

            GameObject imageBackground = image.transform.Find("ImageBackground").gameObject;
            GameObject controlButtons = image.transform.Find("ControlButtons").gameObject;

            StartCoroutine(LoadImage(url, spriteRenderer, imageBackground, controlButtons));
        }
        else if (Array.Exists(videoExtensions, element => extension.Equals(element, StringComparison.OrdinalIgnoreCase)))
        {

        }
        else if (Array.Exists(audioExtensions, element => extension.Equals(element, StringComparison.OrdinalIgnoreCase)))
        {
        }
        else
        {
            GameObject browser = Instantiate(browserWindow, _nextWindowPosition, browserWindow.transform.rotation);
            browser.SetActive(true);

            // Find the browser2D component
            GameObject browser2D = browser.transform.Find("BrowserBackground/Browser2D").gameObject;

            TextMeshProUGUI title = browser.GetComponentInChildren<TextMeshProUGUI>();

            browser2D.GetComponent<SimpleWebBrowser.WebBrowser2D>().Navigate(url);
            browser.transform.SetParent(Windows.transform, false);
            browser.name = name;
            title.text = name;

            //RectTransform rectTransform = browser.GetComponent<RectTransform>();
            //rectTransform.position = new Vector2(l, m);
            _nextWindowPosition += new Vector2(50f, -50f);
            if (_nextWindowPosition.x > 150)
                _nextWindowPosition.x = 50f;
            if (_nextWindowPosition.y < -600)
                _nextWindowPosition.y = -50f;
        }
    }
0

There are 0 answers