I am writing an editor script to create a text asset containing optimised level information based on a combination of game objects in the editor and assets in an asset folder.
I can get at the game-objects in the editor easily enough but I can’t find a way to enumerate my way though all the assets in the currently selected asset folder.
I can get the currently selected ‘Object’ easily enough using ‘Object objSelected = Selection.activeObject;’ but I am stuck as to how I can
a) cast that to folder or get some kind of folder interface from it
b) get the list of assets inside the folder
c) determine the type of those assets and cast them to useful types (game-objects text assets etc…)
Thanks for this. It was the lead I needed. What I wanted to do was get access to all the assets in the currently selected asset folder. This is the code I ended up with.
I hope somebody else finds this useful.
if (Selection.activeObject != null ){
Object objSelected = Selection.activeObject;
string sAssetFolderPath = AssetDatabase.GetAssetPath(objSelected);
// Construct the system path of the asset folder
string sDataPath = Application.dataPath;
string sFolderPath = sDataPath.Substring(0 ,sDataPath.Length-6)+sAssetFolderPath;
// get the system file paths of all the files in the asset folder
string[] aFilePaths = Directory.GetFiles(sFolderPath);
// enumerate through the list of files loading the assets they represent and getting their type
foreach (string sFilePath in aFilePaths) {
string sAssetPath = sFilePath.Substring(sDataPath.Length-6);
Debug.Log(sAssetPath);
Object objAsset = AssetDatabase.LoadAssetAtPath(sAssetPath,typeof(Object));
Debug.Log(objAsset.GetType().Name);
}
}
Its worth noting that the 'AssetDatabase.LoadAllAssetsAtPath ’ call loads all the assets in a single FILE. It DOES NOT load all the assets in a directory which is why you have to get your own list of folder contents.