- Home /
How do you make an array out of an Assets subfolder containing Prefabs?
This is in C#. I want to make an array and select random character prefabs forexample to instantiate into my scene at Spawn points.
Answer by Arkaid · Jul 12, 2016 at 05:22 AM
While it's not possible during runtime, you could whip up a small Editor script to parse your directory and set the prefabs into an array for you. This is really dirty code and could use some cleaning up (like getting rid of warnings), but it does the job:
First, your prefab manager:
using UnityEngine;
public class PrefabManager : MonoBehaviour
{
[SerializeField]
string prefabDirectory;
[SerializeField]
GameObject[] prefabs;
public GameObject GetRandomPrefab()
{
return prefabs[Random.Range(0, prefabs.Length - 1)];
}
}
Then your Editor script:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(PrefabManager))]
public class PrefabManagerEditor : Editor
{
Vector2 scrollPosition;
public override void OnInspectorGUI()
{
SerializedProperty prefabDirectory = serializedObject.FindProperty("prefabDirectory");
EditorGUILayout.PropertyField(prefabDirectory);
// find prefabs in that directory and list them up
string[] assetGUIDs = AssetDatabase.FindAssets("t:prefab", new string[] { prefabDirectory.stringValue });
EditorGUILayout.LabelField("Assets found:");
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
if (assetGUIDs != null)
{
SerializedProperty prefabArray = serializedObject.FindProperty("prefabs");
prefabArray.arraySize = assetGUIDs.Length;
for (int i = 0; i < assetGUIDs.Length; i++)
{
string assetPath = AssetDatabase.GUIDToAssetPath(assetGUIDs[i]);
EditorGUILayout.LabelField(assetPath);
prefabArray.GetArrayElementAtIndex(i).objectReferenceValue = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
}
}
EditorGUILayout.EndScrollView();
serializedObject.ApplyModifiedProperties();
}
}
Answer by greatUnityGamer · Jul 12, 2016 at 05:49 AM
"While it's not possible during runtime," Interesting i thought it was possible to instantiate objects during runtime. alot of tutorials have it. Like they'll make grenade or bullet prefabs and then instantiate during runtime. So why wouldn't it be possible for arrays and folders with prefabs inside?
Oh! You can instantiate them during runtime, like so
GameObject newInstance = Instantiate(prefab$$anonymous$$anager.GetRandomPrefab());
Where prefab$$anonymous$$anager is the instance of that class I wrote up there.
What I meant to say is that you can't browse a directory during runtime. So basically use that editor script once before running your game to update the prefab array, then use in game to your heart's content.
Your answer

Follow this Question
Related Questions
Array does not update after changing the content and size of an array 0 Answers
Why unity does not opening ? 0 Answers
Instantiate multiple objects from an array 1 Answer
How to check if one of value is true on an array of objects? 1 Answer
How to all GameObjects in array or 2 objects collide with 1 object at the same time 1 Answer