Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by greatUnityGamer · Jul 12, 2016 at 03:57 AM · unity 5editorarraysarray of gameobjects

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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();
     }
 }
 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arkaid · Jul 12, 2016 at 05:52 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

80 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges