private static void OrganizeAssets() { // if selection is a folder, organize all assets in that folder, but not in any children folders. if (Selection.objects.Length == 1 && IsAssetAFolder(Selection.activeObject)) { // for each asset in selected folder, organize it. // put all assets in folder in this list List <HoratiusAssetInfo> assetsWithKnownTypes = GetAllAssetsInSelectedFolders(); foreach (var assetInfo in assetsWithKnownTypes) { OrganizeAsset(assetInfo); } } // else, organize selected assets only, and put them in folders within their parent folder (or the root) else { foreach (var objSelected in Selection.objects) { var assetPath = AssetDatabase.GetAssetPath(objSelected); var assetInfo = new HoratiusAssetInfo(objSelected, assetPath, objSelected.GetType()); OrganizeAsset(assetInfo); } } }
/// <summary> /// puts each asset from folder in the list /// </summary> /// <returns></returns> private static List <HoratiusAssetInfo> GetAllAssetsInSelectedFolders() { List <HoratiusAssetInfo> assetsWithKnownTypes = new List <HoratiusAssetInfo>(); foreach (var objSelected in Selection.objects) { if (IsAssetAFolder(objSelected)) { // taken from http://answers.unity3d.com/questions/234935/how-do-i-enumerate-the-contents-of-an-asset-folder.html 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("Path: " + sAssetPath); Object objAsset = AssetDatabase.LoadAssetAtPath(sAssetPath, typeof(Object)); if (objAsset == null) { if (sAssetPath.EndsWith(".meta")) { //Debug.Log("Type: Meta file"); } else { //Debug.Log("Type: Null asset"); } } else { var hai = new HoratiusAssetInfo(objAsset, sAssetPath, objAsset.GetType()); assetsWithKnownTypes.Add(hai); //Debug.Log("Type: " + objAsset.GetType().Name + " a.k.a. " + hai.assetType); } } // add folder too assetsWithKnownTypes.Add(new HoratiusAssetInfo(objSelected, AssetDatabase.GetAssetPath(objSelected), objSelected.GetType())); } else { var hai = new HoratiusAssetInfo(objSelected, AssetDatabase.GetAssetPath(objSelected), objSelected.GetType()); assetsWithKnownTypes.Add(hai); } } return(assetsWithKnownTypes); }
private static void OrganizeAsset(HoratiusAssetInfo assetInfo) { // highest level idea: put asset in a folder named according to the asset type. // if asset is already in such a folder, leave it, and move all other assets around it in appropriate folders, outside of the current folder. // is current folder of any known type? if (assetInfo.assetType != HoratiusAssetKnownTypes.Folder) { // using backslashes or forward slashes? string folderName = Path.GetDirectoryName(assetInfo.path); string directoryOfFolder = Path.GetDirectoryName(folderName); folderName = folderName.Remove(0, directoryOfFolder.Length > 0 ? directoryOfFolder.Length + 1 : 0); //Debug.Log(folderName); // if folder is named like a reserved type folder such as Materials or Prefabs if (AssetTypeToFolder.Values.Contains(folderName)) { Debug.LogError("Folder name " + folderName + " at " + directoryOfFolder + " is reserved. Please rename!"); // reorganize assets into sibling folders outside of the current folder, and only keep appropriate assets inside } else { // folder name is fine. move asset into folder appropriately named. if not exists, create. var appropriateFolderPath = Path.Combine(Path.Combine(directoryOfFolder, folderName), AssetTypeToFolder[assetInfo.assetType]); //Debug.Log(appropriateFolderPath); if (!Directory.Exists(appropriateFolderPath)) { // if directory with same name but lowercase exists, rename to correct case Directory.CreateDirectory(appropriateFolderPath); AssetDatabase.Refresh(); } // move asset there AssetDatabase.MoveAsset(assetInfo.path, appropriateFolderPath + "/" + assetInfo.fileName); } } }