-
-
Save kientux/8bd26e6fa50515b99bd7da15c934a202 to your computer and use it in GitHub Desktop.
Maintains a renamed copy of certain Unity Resources asset files in your project. For example, by default Unity won't include SVG files as text XML for further processing.
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
// Maintains a renamed copy of certain Unity Resources asset files in your project. | |
// E.g. by default Unity won't include SVG files as XML for further processing. | |
// | |
// Put this file in the Assets/Editor folder. | |
// | |
// This script automatically rename resource files with a .txt (text) or .bytes (binary) extension, | |
// to be readable as Resources.Load<TextAssets>(path).text or Resources.Load(path) as byte[]. | |
// | |
// | |
// License: CC0 | |
using UnityEditor; | |
using UnityEngine; | |
using System.IO; | |
public class AssetImportRenamer : AssetPostprocessor | |
{ | |
// lists of file extensions to rename... | |
static string[] textExtensions = { ".svg" }; | |
static string[] binaryExtensions = { }; | |
// Where to put the renamed copies (empty string or "MySubfolder/")... | |
const string putCopiesInFolder = "AutoGenerated/"; | |
public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) | |
{ | |
foreach (string asset in importedAssets) | |
{ | |
string extension; | |
int indexOfDot = asset.LastIndexOf('.'); | |
if (indexOfDot == -1) | |
extension = ""; | |
else | |
extension = asset.Substring(indexOfDot); | |
string newExtension; | |
if (extension.Length == 0) continue; | |
else if (System.Array.IndexOf(textExtensions,extension) != -1) newExtension = ".txt"; | |
else if (System.Array.IndexOf(binaryExtensions,extension) != -1) newExtension = ".bytes"; | |
else continue; | |
string filePath = asset.Substring(0, asset.Length - Path.GetFileName(asset).Length) + putCopiesInFolder; | |
string newFileName = filePath + Path.GetFileName(asset) + newExtension; | |
if (!Directory.Exists(filePath)) | |
Directory.CreateDirectory(filePath); | |
StreamReader reader = new StreamReader(asset); | |
string fileData = reader.ReadToEnd(); | |
reader.Close(); | |
FileStream resourceFile = new FileStream(newFileName, FileMode.OpenOrCreate, FileAccess.Write); | |
StreamWriter writer = new StreamWriter(resourceFile); | |
writer.Write(fileData); | |
writer.Close(); | |
resourceFile.Close(); | |
AssetDatabase.Refresh(ImportAssetOptions.Default); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment