Created
July 13, 2020 12:37
-
-
Save Nrjwolf/b83597b54af566c01e20f752f6c52d4a to your computer and use it in GitHub Desktop.
Move scripts or whatever in new default folders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEngine; | |
/// Thanks @JoshuaMcKenzie for idea http://answers.unity.com/answers/1213863/view.html | |
public class AssetsDistributor : UnityEditor.AssetModificationProcessor | |
{ | |
//only evaluate files imported into these paths | |
static string[] pathsToMoveFrom = new string[] | |
{ | |
"Assets" | |
}; | |
static Dictionary<string, string> defaultFileLocationByExtension = new Dictionary<string, string>() | |
{ | |
// {".mp4", "Assets/StreamingAssets/"},//for IOS, movies need to be in StreamingAssets | |
// {".anim", "Assets/Art/Animations/"}, | |
// {".mat", "Assets/Art/Materials/"}, | |
// {".fbx", "Assets/Art/Meshes/"}, | |
// //Images has subfolders for Textures, Maps, Sprites, etc. | |
// // up to the user to properly sort the images folder | |
// {".bmp", "Assets/Art/Images/"}, | |
// {".png", "Assets/Art/Images/"}, | |
// {".jpg", "Assets/Art/Images/"}, | |
// {".jpeg", "Assets/Art/Images/"}, | |
// {".psd", "Assets/Art/Images/"}, | |
// {".mixer", "Assets/Audio/Mixers/"}, | |
// //like images, there are sub folders that the user must manage | |
// {".wav", "Assets/Audio/Sources/"}, | |
//like images, there are sub folders that the user must manage | |
{".cs", "Assets/Game/Scripts/"}, | |
// {".shader", "Assets/Dev/Shaders/"}, | |
// {".cginc", "Assets/Dev/Shaders/"} | |
}; | |
[MenuItem("Tools/Nrjwolf/Default Folder Changer/Force")] | |
static void ForceMove() | |
{ | |
foreach (var path in pathsToMoveFrom) | |
{ | |
var filesInPath = Directory.GetFiles(Path.Combine(Application.dataPath.Replace("/Assets", string.Empty), path)); | |
foreach (var p in filesInPath) | |
{ | |
TryMoveAssets(p); | |
} | |
} | |
} | |
private static void TryMoveAssets(string assetPath) | |
{ | |
assetPath = assetPath.Replace(".meta", string.Empty); | |
// Is folder dirrectory valid | |
string directory = Path.GetFileName(Path.GetDirectoryName(assetPath)); | |
if (!pathsToMoveFrom.Contains(directory)) | |
return; | |
// Ss extension valid | |
string extension = Path.GetExtension(assetPath).ToLower(); | |
if (!defaultFileLocationByExtension.ContainsKey(extension)) | |
return; | |
string filename = Path.GetFileName(assetPath); | |
string oldPath = GetPathWithNewRoot(assetPath, "Assets"); // convert to lacal path | |
string newPath = defaultFileLocationByExtension[extension] + filename; | |
var moveAssetStatus = AssetDatabase.MoveAsset(oldPath, newPath); | |
if (string.IsNullOrEmpty(moveAssetStatus)) | |
{ | |
AssetDatabase.Refresh(); | |
Debug.Log($"Moving asset {oldPath} to path {newPath} with status : {moveAssetStatus}"); | |
} | |
} | |
private static string GetPathWithNewRoot(string fullPath, string rootFolder) | |
{ | |
var intLength = fullPath.Length; | |
var intLocation = fullPath.IndexOf(rootFolder); | |
return fullPath.Substring(intLocation, intLength - intLocation); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment