Unity3d returns all file names under the specified Assets folder path, unity3dassets

Source: Internet
Author: User

Unity3d returns all file names under the specified Assets folder path, unity3dassets

 

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> /// return all file names in this path based on the file path in the specified Assets //// </summary> /// <returns> file name array </returns> /// <param name = "path"> "1" level path </param> under Assets /// <param name = "pattern"> filter files suffix. </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 {// return the array or empty array dire of the names of files and subdirectories in the specified directory CtoryEntries = System. IO. directory. getFileSystemEntries (objPath); for (int I = 0; I <directoryEntries. length; I ++) {string p = directoryEntries [I]; // obtain the files or folders in the required directory (level 1) // string [] tempPaths = StringExtention. splitWithString (p, "/Assets/" + path + "\"); // cannot be empty after tempPaths is split, as long as directoryEntries is not empty // if (tempPaths [1]. endsWith (". meta ") continue; string [] patheatmap = StringExtention. splitWithString (tempPat Hs [1], ". "); // file if (patheatmap. length> 1) {nameArray. add (patheatmap [0]);} // recursively traverse sub-directories! Else {GetObjectNameToArray <T> (path + "/" + patheatmap [0], "pattern"); continue ;}} catch (System. IO. directoryNotFoundException) {Debug. log ("The path encapsulated in the" + objPath + "Directory object does not exist. ");}}
 
Void Start () {// TextAsset [] texts = LoadAsset <TextAsset> ("/CreateScriptDialog/Editor", "cs "); // GetObjectNameToArray <string> ("uSequencer/Example Scenes", "xxx"); // nested traversal of GetObjectNameToArray <string> ("uSequencer", "xxx "); // implement nested traversal of foreach (string str in nameArray) {Debug. log (str );}}
 
<Pre class = "csharp" name = "code"> /// <summary> /// custom string segmentation 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) // split {s = sourceString. substring (0, sourceString. indexOf (splitString); sourceString = sourceString. substring (sourceString. indexOf (splitString) + splitString. length); arrayList. add (s);} arrayList. add (sourceString); return arrayList. toArray ();}}



 

How can I obtain the path of the assets folder ??

InputStream inStream = context. getAssets (). open ("path under assets (excluding assets)/file name ");

Unity3D 4x C # How does the script read the file names of all files in the Resources directory?

Unable to read. Unity does not provide this function. You can create a list file, which records the names of all files, and then use Resources. load to read the file to get the names of all files. I think the file name traversal function you need may be used to list all objects of a certain type on Resources. FindObjectsOfTypeAll.
 

Related Article

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.