yasirkula / UnitySimpleFileBrowser

A uGUI based runtime file browser for Unity 3D (draggable and resizable)
MIT License
817 stars 111 forks source link

How to get the SimpleFileBrowserCanvas(Clone) reference after it spawns on my scene in script? #79

Closed sstamatis01 closed 1 year ago

sstamatis01 commented 1 year ago

I have been using the SimpleFileBrowser just fine my VR project, however one minor setback is the fact that I cant edit SimpleFileBrowserCanvas(Clone) position in runtime because I cannot find its refence in my script. Basically I always want it to spawn in front of my player so I need its reference to change its position whenever I call yield return FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.FilesAndFolders, true, null, null, "Load Files and Folders", "Load");

The reason why this happens is because it spawns as a new scene (DontDestroyOnLoad) which unfortunately is created dynamically and thus I cannot register it in my build in order to switch between scenes with SceneManager.LoadScene package. Searching the object with Findwont work since I m looking in my main scene.

This should be possible to do somehow but I am not very experienced with Unity to do it myself. I would appreciate if someone knows who to do this. filebrowser_issue

yasirkula commented 1 year ago

Hi! If you wish, you can put an instance of SimpleFileBrowserCanvas to your scene and reference it in your scripts. Or, you can get the file browser via FindObjectOfType<FileBrowser>() after calling WaitForLoadDialog for the first time.

sstamatis01 commented 1 year ago

The funny thing is that I found a workaround with a solution proposed by you here (https://forum.unity.com/threads/editor-script-how-to-access-objects-under-dontdestroyonload-while-in-play-mode.442014/). The proposed method works and I cant edit the position during runtime:

public static GameObject[] GetDontDestroyOnLoadObjects()
{
    GameObject temp = null;
    try
    {
        temp = new GameObject();
        Object.DontDestroyOnLoad( temp );
        UnityEngine.SceneManagement.Scene dontDestroyOnLoad = temp.scene;
        Object.DestroyImmediate( temp );
        temp = null;

        return dontDestroyOnLoad.GetRootGameObjects();
    }
    finally
    {
        if( temp != null )
            Object.DestroyImmediate( temp );
    }
}

Thanks for quick response! I guess your suggestions could also work.