Last active
September 16, 2017 13:04
-
-
Save natsupy/43ceb91938e22a7cb9f1eeee1035f0f4 to your computer and use it in GitHub Desktop.
Search type in root folder
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; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using Newtonsoft.Json; | |
using UnityEngine; | |
public IEnumerator GetLevels() | |
{ | |
string[] levelFiles; | |
#if UNITY_STANDALONE | |
var pathGame = Directory.GetParent(Application.dataPath).FullName; | |
#elif UNITY_EDITOR | |
var pathGame = SimpleFileBrowser.FileBrowser.tempPathDefault; | |
#elif UNITY_ANDROID && !UNITY_EDITOR | |
var pathGame = SimpleFileBrowser.FileBrowser.tempPathDefault; | |
#endif | |
print(pathGame); | |
try | |
{ | |
//pathGame is root folder where game was installed Ex: /storage/sdcard ==> it ok when testing my game | |
levelFiles = | |
SearchTypeInParentFolder(pathGame, "cytoidlevel"); | |
} | |
catch (Exception) | |
{ | |
Debug.LogException(e); | |
yield break; | |
} | |
} | |
string[] SearchTypeInParentFolder(string path = null, params string[] types) | |
{ | |
if (path == null) | |
path = DEFAULT_PATH; | |
var length = types.Length; | |
string[] directories = new string[] { }; | |
List<string> pathFiles = new List<string>(); | |
for (int index = 0; index < length; index++) | |
{ | |
var typeFile = "*." + types[index]; | |
try | |
{ | |
directories = Directory.GetDirectories(path); | |
var tempPathFiles = Directory.GetFiles(path, typeFile, SearchOption.TopDirectoryOnly); | |
pathFiles.AddRange(tempPathFiles); | |
} | |
catch (Exception e) | |
{ | |
Debug.LogException(e); | |
} | |
for (int i = 0; i < directories.Length; i++) | |
{ | |
try | |
{ | |
var tempPathFiles = Directory.GetFiles(directories[i], typeFile, SearchOption.TopDirectoryOnly); | |
pathFiles.AddRange(tempPathFiles); | |
} | |
catch (Exception e) | |
{ | |
Debug.LogException(e); | |
} | |
} | |
} | |
return pathFiles.ToArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment