- Home /
Scene object scripting
Is it possible to script something using a Scene object?
I want to have a "Scene" object that I can have an array of, that serves as a sort of "Level List". I could get the names of the scenes, and call Application.LoadLevel(MySceneObject);
to load a scene.
If that's not possible, can someone provide a suggestion of how I might go about scripting a dynamic scene loader? Say I have a folder in my Assets called "Scenes", and any scene inside there is a "level", but the user can choose which one they'd like to load from a GUI.
Any tips or information would be appreciated.
Answer by Mike 3 · May 14, 2010 at 01:59 AM
Easiest way I can tell:
using UnityEngine; using System.Collections.Generic;
public class LevelManager : MonoBehaviour { [SerializeField] private Object[] levels; private string[] levelNames;
public string[] getLevels() { if (levelNames == null) { if (levels == null) levelNames = new string[0]; else { List<string> tempLevels = new List<string>(); foreach(Object o in levels) { if (o.GetType() == typeof(Object)) tempLevels.Add(o.name); }
levelNames = tempLevels.ToArray();
}
}
return levelNames;
}
}
that'll let you assign scenes to an array, and will filter out anything which isn't only UnityEngine.Object - they you can grab the levels as strings with getLevels
you can then feed those into Application.LoadLevel
I modified the script a bit to suit my needs, but this worked, thanks.
Your answer
Follow this Question
Related Questions
Scene inside a Scene 3 Answers
scene loading 100's of scenes 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
how to move between scenes 1 Answer
How do i Make more levels in my game 2 Answers