【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);
            }
        }
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发财猫MoneyCat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值