0

I would like to split each of my levels into separate scenes.

Now I would like to know how do you load those scenes (i.e. levels) into the main-scene? I just ask because I’d like to know how to load sprites from other scenes into my main-scene (if this is possible). In my main-scene the prefabs should be instantiated when loading is finished.

Do I have to get the sprites via Resources.Load? E.g. like this:

 public List<GameObject> items = new List<GameObject>();

 void Start() {
 GameObject sprite = null;
 int counter = 0;
 bool done = false;
 while(!done)
 {
     sprite = Resources.Load("Item" + counter) as GameObject;
     if(sprite == null) {
         done = true;
     } else {
         items.Add(sprite);
 }
     ++counter;
 }
}

Or shall I instantiate like this (with the help of an array):

public class LevelLoader : MonoBehaviour {

public enum Level {
    Level1,
    Level2,
Level3
}

public Object[] levelPrefabs;

public void Load(Level level)
{
    int levelIndex = (int)level;
    if (levelIndex >= 0 && levelIndex < levelPrefabs.Length) {
        Instantiate(levelPrefabs[levelIndex]);
    } else {
        Debug.LogError("Invalid level index: " + levelIndex);
    }
}
}

My knowledge of C# is limited.

halfer
  • 19,824
  • 17
  • 99
  • 186
drpelz
  • 811
  • 11
  • 43
  • What sprites are you trying to load in? Shouldn't you just be able to keep all sprites from your entire project in your Assets folder and put them in your main scene as needed? – Ginger and Lavender Aug 20 '18 at 15:44
  • [`Resources.Load`](https://docs.unity3d.com/ScriptReference/Resources.Load.html) is deprecated (and loads files from a special `Resources` directory in your assets) in favor of [Asset Bundles](https://docs.unity3d.com/ScriptReference/AssetBundle.html). There is also the [`LoadSceneMode`](https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.Additive.html) parameter of [`LoadScene()`](https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html), which may or may not do what you want. – Draco18s no longer trusts SE Aug 20 '18 at 15:59
  • @Dalsia Someone said that it might be a bad idea if you have all your sprites in your main scene because of memory management. Therefore I ask. – drpelz Aug 20 '18 at 16:13
  • @drpelz it might still be better than changing the entire scene just for switching between a fiew sprites though – derHugo Aug 24 '18 at 05:29

1 Answers1

1

I am not in favor of the two ways to load sprite you mentioned because both have issues. Your first method which uses the Resources API causes loading time to be slow because it requires the use of the Resources folder. The second method doesn't give you option to chose which Object to load. It will automatically serialize the objects in the array.

The recommended way to do this is to use the StreamingAssets folder and AssetBundle. Add the spites and other resources to an AssetBundle group then build the AssetBundle to the StreamingAssets path. During run-time, you can use the AssetBundle and AssetBundleRequest API to load the resources data. The Application.streamingAssetsPath variable points to the StreamingAssets folder path.

I wrote instruction on how to build and load AssetBundle during run-time here and that should get you started.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thank you very much for your reply.:) I'm just swapping things like small sprites and some backgrounds. So basically the scenes stay the same. I think I will use one scene only because it shouldn't need that much memory. But I will try your solution, also!:D – drpelz Aug 20 '18 at 17:35
  • I haven't seen a game that uses just one scene. In fact you should make a loading menu. This makes your game to load faster. If just few sprites, then that's fine otherwise use AssetBundle to speed loading up. Anyways, I've updated the other post. Good luck. – Programmer Aug 20 '18 at 19:14