- Home /
How to load Assets as GameObjects?
I'm writing an Editor script that takes assets from a directory path, and builds them into an asset bundle.
I want to use: BuildPipeline.BuildAssetBundle(Object main, Object [] assets, string outputPath, ...);
That means I need an array of Object[]. But how do I get the array of Object[], given an asset path that contains all the fbx that I need in bundled?
Additional Analysis:
- I can't use Editor's "Selection" API. Because, I can't control what folder is selected in the Editor window.
- This doesn't work:
Object[] objects = AssetDatabase.LoadAllAssetsAtPath("Assets/Art")
, it doesn't load the assets as GameObjects or Texture2D's if I try to cast the Object afterwards. I checked using C# code: if (objects[i] is GameObject), it will return false for all objects in the array. This API works differently from what I assumed. It takes the file path of an asset (ie: /character.fbx), not a folder path, and outputs all children in the fbx as GameObjects.
Answer by ckfinite · Nov 30, 2010 at 03:35 AM
Why does AssetDatabase.LoadAllAssetsAtPath not work for you? The problem you are havaing with it should be a non-issue. In unity, a asset does not come in as a GameObject. It comes in as the most appropriate class, so a FBX should come in as a Mesh. What I would reccomend doing is instead of checking for a GameObject, print the class name. Try
foreach (object o in objects) Debug.Log(object.GetType().ToString());
which should give you the class names of all the imported objects.
Is your output asset bundle working? I find that is the really important question.
Inside that foreach loop you can do things like "if (o is $$anonymous$$esh)" to find the objects of the type(s) you're interested in.
Your answer
Follow this Question
Related Questions
"Object reference not set to an instance of an object" but it is. 2 Answers
How to add and remove installed packages more easily? E.g. Google Admob. Package control in Unity? 0 Answers
WebPlayer dataPath 2 Answers
How to use asset bundle as database instead of PlayerPrefs 0 Answers
I don't know how to use AssetDatabase.GetTextMetaDataPathFromAssetPath() 0 Answers