Unity3d returns all file names under this path according to the directory path under the specified assets

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.