Using unityengine;using system.collections;using system.collections.generic;using System.IO;
<pre class= "CSharp" name= "code" >public static list<string> NameArray = new list<string> ();
<summary>///returns all file names under this path under the specified assets file path/////</summary>///<returns> file name Array </returns >///<param name= "path" >assets under "one" level path </param>///<param name= "pattern" > Criteria for filtering file suffix names. </param >///<typeparam name= "T" > Function template type name t</typeparam>void getobjectnametoarray<t> (string path, string Pattern) {String objpath = Application.datapath + "/" + path; String[] directoryentries; try {//returns an array of names of files and subfolders in the specified folder or an empty array of directoryentries = System.IO.Directory.GetFileSystemEntries (Objpath); for (int i = 0; i < directoryentries.length; i + +) {string p = directoryentries[i]; Get the file or folder under the required folder (level)//string[] temppaths = stringextention.splitwithstring (P, "/assets/" +path+ "\ \"); Temppaths cannot be empty after cutting, just directoryentries not empty//if (temppaths[1]. EndsWith (". Meta")) continue;string[] Pathsplit = stringextention.splitwithstring (Temppaths[1], "."); File if (Pathsplit.length > 1) {namearray.add (pathsplit[0]);} Go through the sub-folders recursively!Else{getobjectnametoarray<t> (path+ "/" +pathsplit[0], "pattern"); continue;}} } catch (System.IO.DirectoryNotFoundException) {Debug.Log ("The path encapsulated in the" + Objpath + "Directory obje CT does not exist. ");}}
void Start () {//textasset[] texts = loadasset<textasset> ("/createscriptdialog/editor", "CS");// Getobjectnametoarray<string> ("Usequencer/example Scenes", "xxx"); Ability to implement nested traversal getobjectnametoarray<string> ("Usequencer", "xxx"); Ability to implement nested traversal of foreach (String str in NameArray) {Debug.Log (str);}}
<pre class= "CSharp" name= "code" >///<summary>///own definition of string cutting method//</summary>public class stringextention {public static string[] splitwithstring (String sourcestring, String splitstring) {string tempsourcestring = sourcestring; list<string> arrayList = new list<string> (); string s = String. Empty; while (Sourcestring.indexof (splitstring) >-1) //Cut { s = sourcestring.substring (0, Sourcestring.indexof ( splitstring)); sourcestring = sourcestring.substring (Sourcestring.indexof (splitstring) + splitstring.length); Arraylist.add (s); } Arraylist.add (sourcestring); return Arraylist.toarray (); }
Unity3d returns all file names under this path according to the directory path under the specified assets