Search Unity

Field in Inspector to point to a Scene to load.

Discussion in 'Immediate Mode GUI (IMGUI)' started by fwalker, Mar 11, 2013.

  1. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    I would like to set up a field in the inspector where the user can drag a Unity Scene from the Project tab.

    Not having much luck.

    I cannot find what this Asset field type should be and fromt he forum I am gathering that there isn't one available. Is this still the case?.

    If it is the case. I was thinking that I could extend the the editor somehow in such a way that the user could drag the Scene asset, then the editor would convert the asset to the assetpath as text and store that.

    It seems like it would work except that the user cannot drag the object into the box because the box is a text field. :(
    Can I override the drop behaviour of the inspector field so it will accept whatever type I tell it to?

    Or is there a better solution for this.

    Thanks
     
  2. kamullis

    kamullis

    Joined:
    Feb 7, 2013
    Posts:
    21
    andrew-lukasik and aimozs like this.
  3. glitchers

    glitchers

    Joined:
    Apr 29, 2014
    Posts:
    64
    fwalker, andrew-lukasik and eses like this.
  4. tomatohorse

    tomatohorse

    Joined:
    Oct 26, 2013
    Posts:
    13
    Here's a simple solution that I just found that worked for my game. I attached this "scene loader" script to an object with a collider on it. This lets you drag a scene from your project files into the inspector. You can rename the scene in your project folder, and it still works with no additional tweaking :)

    * * *

    [SerializeField] Object scene;

    private void OnTriggerEnter(Collider other) {
    if (other.CompareTag("Player")){
    SceneManager.LoadScene(scene.name);
    }
    }

    * * *
     
  5. mohamedkaram09

    mohamedkaram09

    Joined:
    Oct 13, 2019
    Posts:
    7
    This is actually really smart! It worked for me perfectly. good job :)
     
  6. BitPixelGames

    BitPixelGames

    Joined:
    Apr 1, 2018
    Posts:
    2
    This works well in the editor, but stops working in a build.
     
  7. electr1on

    electr1on

    Joined:
    Mar 21, 2018
    Posts:
    28