baobao
7/13/2018 - 12:35 AM

AssetDataBaseUtil.cs

using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public static class AssetDataBaseUtil
{
    /// <summary>
    /// URLパスリストから該当オブジェクト配列を取得する
    /// </summary>
    static IList<T> GetObjectsFromPathList<T>(IList<string> pathList) where T : UnityEngine.Object
    {
        List<T> result = new List<T>();
        foreach (var path in pathList)
        {
            var obj = AssetDatabase.LoadAssetAtPath<T>(path);
            if (obj != null)
            {
                result.Add(obj);
            }
        }

        return result;
    }

    /// <summary>
    /// アセットパスリストから該当オブジェクトを取得する
    /// </summary>
    static T GetObjectFromPathArray<T>(IList<string> pathList) where T : UnityEngine.Object
    {
        return pathList.Select(AssetDatabase.LoadAssetAtPath<T>).FirstOrDefault();
    }

    /// <summary>
    /// 所属するアセット配列を取得する
    /// </summary>
    public static IList<T> GetBelongObjects<T>(UnityEngine.Object obj) where T : UnityEngine.Object
    {
        var path = AssetDatabase.GetAssetPath(obj);
        var result = AssetDatabase
            .LoadAllAssetsAtPath(path).OfType<T>().ToArray();
        return result;
    }
}