Last active
April 12, 2017 15:43
-
-
Save kimsama/aa6f20c7ae99dbffb26e to your computer and use it in GitHub Desktop.
Retrieves certain type of assets with the specified file extension at the given path.
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
/// <summary> | |
/// Used to get assets of a certain type and file extension from entire project | |
/// Usage: | |
/// UnityEngine.Object[] pagePrefabs = GetAssetsOfType("Resources/Prefabs/PAGE", typeof(GameObject), ".prefab"); | |
/// </summary> | |
/// <param name="type">The type to retrieve. eg typeof(GameObject).</param> | |
/// <param name="fileExtension">The file extention the type uses eg ".prefab".</param> | |
/// <returns>An Object array of assets.</returns> | |
public static UnityEngine.Object[] GetAssetsOfType(string path, System.Type type, string fileExtension) | |
{ | |
List<UnityEngine.Object> tempObjects = new List<UnityEngine.Object>(); | |
DirectoryInfo directory = new DirectoryInfo(Application.dataPath + "/"+ path); | |
FileInfo[] goFileInfo = directory.GetFiles("*" + fileExtension, SearchOption.AllDirectories); | |
int i = 0; int goFileInfoLength = goFileInfo.Length; | |
FileInfo tempGoFileInfo; string tempFilePath; | |
UnityEngine.Object tempGO; | |
for (; i < goFileInfoLength; i++) | |
{ | |
tempGoFileInfo = goFileInfo[i]; | |
if (tempGoFileInfo == null) | |
continue; | |
tempFilePath = tempGoFileInfo.FullName; | |
tempFilePath = tempFilePath.Replace(@"\", "/").Replace(Application.dataPath, "Assets"); | |
Debug.Log(tempFilePath + "\n" + Application.dataPath); | |
tempGO = AssetDatabase.LoadAssetAtPath(tempFilePath, typeof(UnityEngine.Object)) as UnityEngine.Object; | |
if (tempGO == null) | |
{ | |
Debug.LogWarning("Skipping Null"); | |
continue; | |
} | |
else if (tempGO.GetType() != type) | |
{ | |
Debug.LogWarning("Skipping " + tempGO.GetType().ToString()); | |
continue; | |
} | |
tempObjects.Add(tempGO); | |
} | |
return tempObjects.ToArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment