Prefab library

Hello everyone,

I’m trying to make some kind of prefab library. What I need is a class with a method where I can input an int variable, and then with a switch case, return a gameObject of the prefab I want. So, I’ll have:

AssetLibrary.GetAsset(1); //This will return a gameObject 

Now, how is the most efficient way to do this? Should I create a script with a variable for every prefab and assign them from the inspector? Or is there a better (or more professional) way to do this, in terms of performance?

Thank you very much

EDIT: Actually, it could be much better If I could assign those refferences to prefabs in the code itself, I find using the editor a much difficult way for what I’m trying to do.

Resources.Load() should be avoided for large projects. Assets in a Resources folder are always included in the project, no matter if you use them or not.

You can simply create a script that holds your prefab references and make it a singleton.

// C#
using UnityEngine;
using System.Collections;

public class AssetLibrary : MonoBehaviour
{
    public GameObject[] prefabs;

    private static AssetLibrary m_Instance = null;
    private static AssetLibrary Get()
    {
        if (m_Instance == null)
            m_Instance = FindObjectOfType(type(AssetLibrary)) as AssetLibrary;
        return m_Instance;
    }
    public static GameObject GetAsset(int aIndex)
    {
        if (aIndex < 0 || aIndex >= Get().prefabs.Length)
            return null;
        return Get().prefabs[aIndex];
    }
}

You just need to put this script on an empty gameobject somewhere in the scene. There you can add as many prefabs you like to the prefabs array.