【Unity3D自学记录】Editor模式下遍历并赋值

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEditor;
using System.IO;
using System;
using System.Collections.Generic;
using UnityEngine.Networking;

public class DJH_Unity : MonoBehaviour
{

    private static string _extension = "*.png";
    [MenuItem("Tools/FileSeachWithExtension")]
    static void Search()
    {
        // 获取所有选中 文件、文件夹的 GUID
        string[] guids = Selection.assetGUIDs;
        foreach (var guid in guids)
        {
            // 将 GUID 转换为 路径
            string assetPath = AssetDatabase.GUIDToAssetPath(guid);
            // 判断是否文件夹
            if (Directory.Exists(assetPath))
            {
                SearchDirectory(assetPath);
            }
        }
    }

    static void SearchDirectory(string directory)
    {
        DirectoryInfo dInfo = new DirectoryInfo(directory);
        // 获取 文件夹以及子文件加中所有扩展名为  _extension 的文件
        FileInfo[] fileInfoArr = dInfo.GetFiles(_extension, SearchOption.AllDirectories);
        for (int i = 0; i < fileInfoArr.Length; ++i)
        {
            string fullName = fileInfoArr[i].FullName;
            //Debug.Log(fullName);
            string paht = fullName.Substring(fullName.IndexOf("Assets"));
            Sprite sp=AssetDatabase.LoadAssetAtPath<Sprite>(paht);
            GameObject go = GameObject.Find("Image");
            print(go.name);
            go.GetComponent<FrameAnimator>().Frames[i] = sp;
        }
    }
   

    [MenuItem("Tools/Change")]
    static void CheckSceneSetting()
    {
        string path = "D:/UnityProject/FrameAnimation/Assets/1";
        List<string> dirs = new List<string>();
        GetDirs(path, ref dirs);
    }
    //参数1 为要查找的总路径, 参数2 保存路径
    private static void GetDirs(string dirPath, ref List<string> dirs)
    {
        foreach (string path in Directory.GetFiles(dirPath))
        {
            //获取所有文件夹中包含后缀为 .prefab 的路径
            if (System.IO.Path.GetExtension(path) == ".png")
            {
                dirs.Add(path.Substring(path.IndexOf("Assets")));
                Debug.Log(path.Substring(path.IndexOf("Assets")));
            }
        }

        if (Directory.GetDirectories(dirPath).Length > 0)  //遍历所有文件夹
        {
            foreach (string path in Directory.GetDirectories(dirPath))
            {
                GetDirs(path, ref dirs);
            }
        }
    }


}

猜你喜欢

转载自blog.csdn.net/hackdjh/article/details/125230409