Skip to content

Instantly share code, notes, and snippets.

@mstevenson
Created December 27, 2012 08:52
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mstevenson/4386682 to your computer and use it in GitHub Desktop.
Save mstevenson/4386682 to your computer and use it in GitHub Desktop.
A small file utility class for Unity. Transform an absolute path into an Assets folder relative path, and get a list of all Resources directories in the project.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class FileUtility {
/// <summary>
/// Determine whether a given path is a directory.
/// </summary>
public static bool PathIsDirectory (string absolutePath)
{
FileAttributes attr = File.GetAttributes(absolutePath);
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
return true;
else
return false;
}
/// <summary>
/// Given an absolute path, return a path rooted at the Assets folder.
/// </summary>
/// <remarks>
/// Asset relative paths can only be used in the editor. They will break in builds.
/// </remarks>
/// <example>
/// /Folder/UnityProject/Assets/resources/music returns Assets/resources/music
/// </example>
public static string AssetsRelativePath (string absolutePath)
{
if (absolutePath.StartsWith(Application.dataPath)) {
return "Assets" + absolutePath.Substring(Application.dataPath.Length);
}
else {
throw new System.ArgumentException("Full path does not contain the current project's Assets folder", "absolutePath");
}
}
/// <summary>
/// Get all available Resources directory paths within the current project.
/// </summary>
public static string[] GetResourcesDirectories ()
{
List<string> result = new List<string>();
Stack<string> stack = new Stack<string>();
// Add the root directory to the stack
stack.Push(Application.dataPath);
// While we have directories to process...
while (stack.Count > 0) {
// Grab a directory off the stack
string currentDir = stack.Pop();
try {
foreach (string dir in Directory.GetDirectories(currentDir)) {
if (Path.GetFileName(dir).Equals("Resources")) {
// If one of the found directories is a Resources dir, add it to the result
result.Add(dir);
}
// Add directories at the current level into the stack
stack.Push(dir);
}
}
catch {
Debug.LogError("Directory " + currentDir + " couldn't be read from.");
}
}
return result.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment