Last active
August 29, 2015 14:14
-
-
Save simonbroggi/e1438a307e4cbbe7f7ae to your computer and use it in GitHub Desktop.
extract all used images from unity project to a destination folder. http://answers.unity3d.com/questions/57909/find-unused-assets-in-project.html
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 UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class AssetExtractor : EditorWindow { | |
Vector2 scrollPos; | |
List<string> usedFileNames; | |
string destinationFolder = ""; | |
[MenuItem ("Window/Asset Extractor")] | |
public static void ShowWindow () { | |
EditorWindow.GetWindow(typeof(AssetExtractor)); | |
} | |
void OnGUI(){ | |
if(GUILayout.Button("Fill list from editor log")){ | |
usedFileNames = parseUsedAssetsFromLog(); | |
} | |
if(GUILayout.Button("Clear editor log")){ | |
File.WriteAllText(getEditorLogFilePath(), string.Empty); | |
} | |
if(usedFileNames != null){ | |
var usedImageFiles = | |
from file in usedFileNames | |
where isImageFile(file) | |
select file; | |
if(GUILayout.Button("Write files to folder")){ | |
destinationFolder = EditorUtility.OpenFolderPanel("Select folder", destinationFolder, ""); | |
Debug.Log(destinationFolder); | |
if(!String.IsNullOrEmpty(destinationFolder)){ | |
foreach(string path in usedImageFiles){ | |
//Debug.Log("copy " + path + " to " + destinationFolder + Path.GetFileName(path)); | |
Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(destinationFolder, path))); | |
File.Copy(Path.Combine(Application.dataPath, path), Path.Combine(destinationFolder, path), true); | |
} | |
} | |
} | |
scrollPos = EditorGUILayout.BeginScrollView(scrollPos); | |
foreach(string path in usedImageFiles){ | |
//GUILayout.Label(filename); | |
GUILayout.Label(Path.Combine(Application.dataPath, path) + " -> " + Path.Combine(destinationFolder, path)); | |
} | |
EditorGUILayout.EndScrollView(); | |
} | |
} | |
private bool isImageFile(string filepath){ | |
return filepath.EndsWith(".png") | |
|| filepath.EndsWith(".psd") | |
|| filepath.EndsWith(".jpg") | |
|| filepath.EndsWith(".jpeg"); | |
} | |
private string getEditorLogFilePath(){ | |
if(Application.platform == RuntimePlatform.OSXEditor){ | |
string home = Environment.GetFolderPath(Environment.SpecialFolder.Personal); | |
return home + "/Library/Logs/Unity/Editor.log"; | |
//return Path.Combine(home, "/Library/Logs/Unity/Editor.log"); | |
} | |
else if(Application.platform == RuntimePlatform.WindowsEditor){ | |
string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); | |
return Path.Combine(localAppData, @"Unity\Editor\Editor.log"); | |
} | |
return null; | |
} | |
private List<string> parseUsedAssetsFromLog() | |
{ | |
Debug.Log("parsing file"); | |
List<string> result = new List<string>(); | |
string logfilePath = getEditorLogFilePath(); | |
try | |
{ | |
// Have to use FileStream to get around sharing violations! | |
FileStream FS = new FileStream(logfilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); | |
StreamReader SR = new StreamReader(FS); | |
string line; | |
while (!SR.EndOfStream && !(line = SR.ReadLine()).Contains("Used Assets,")); | |
Debug.Log(line); | |
while (!SR.EndOfStream && (line = SR.ReadLine()).StartsWith(" ")) | |
{ | |
line = line.Substring(line.IndexOf("% ") + 2); | |
if(line.StartsWith("Assets/")){ | |
line = line.Substring(7); | |
result.Add(line); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
Debug.LogException(e); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment