Hi
I’m currently working on a 2D 2-4 players game and i need to make +100 levels, but making one level per scene is not very optimized and need a loading time.
So how can i do plz ?
(sry for my bad english)
Hi
I’m currently working on a 2D 2-4 players game and i need to make +100 levels, but making one level per scene is not very optimized and need a loading time.
So how can i do plz ?
(sry for my bad english)
Fine, then this is what you should do… its a little bit heavy but it is probably the best way to do it.
In order to do it you’ll need to create classes (non-monobehaviours) for most objects. Some examples:
public class Block {
public block (Vector2 pos, Vector2 _size, string tag, string layer) {
position = pos;
size = _size;
tagName = tag;
layerName = layer;
}
public Vector2 position;
public Vector2 size;
public string tagName; // it's the fastest way i found to save tags and layers,
public string layerName;
}
public class Manager {
public Manager (Color color, float gravity) {
gravitySpeed = gravity;
backGroundColor = color;
}
public float gravitySpeed;
public Color backGroundColor;
}
And finally you need one more class for each level:
public class Level {
public level (Block[] blocks, Manager manager, Players[] players, Etc.) {
allBlocks = blocks;
levelManager = manager;
allPlayers = players;
}
public Block[] allBlocks;
public Manager levelManager;
public Player[] allPlayers;
//All data for each level
}
With all that you simply create a scriptableObject class. More info on scriptableObjects. It is to save and mantain all that data through open and closing of unit and the game. And in that file you need to have something like this:
[CreateAssetMenu ()] //This will let you to create the file.
public class LevelSaver : ScriptableObject {
public Level[] allLevels; //or...
public List<Level> allLevels //I'd recommend this one here.
public void Awake () {
SetLevels ();
}
void SetLevels () {
allLevels.Add (new Level ( new Block[] { new Block (/*data*/), new Block (/*data*/)}, new Manager (/*Manager data*/), new Player (/*Player data*/)));
//I know it looks messy but remember you are saving a whole level here...
// That Add () method only works for lists though.
}
}
Since you have the [CreateAssetMenu ()]
you can create this file through Assets/Create/LevelSaver, but remember to have only one. Everytime you change the scripts delete the previous file and create a new one.
then simply put this file in the Resources foulder and you can access it with Resources.FindObjectsOfTypeAll<LevelSaver> ()
. Then simply write levelSaver.allLevels [1].levelManager.gravitySpeed
and should work. Hope this is useful, i know it seems like a lot of wirk but this things come in handy in the end. Good luck! ;D