unity 加载图片文件

  • 图片文件自动设置为Sprite
  • 添加文件时,自动添加到列表
  • 删除文件时,自动从列表删除


使用方式

using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour {

    public Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();

    private void Awake(){
        var spriteList = Resources.Load<SpriteAsset>("Assets/SpriteAsset").SpriteList;
        int count = spriteList.Count;
        for(int i = 0; i < count; i++)
        {
            spriteDic.Add(spriteList[i].name, spriteList[i]);
        }

        Debug.Log(spriteDic["3"]);
    }
}



该文件名字必须为SpriteAsset

using System.Collections.Generic;
using UnityEngine;
 
public class SpriteAsset : ScriptableObject
{
    public List<Sprite> SpriteList;
}


using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO;
 
public class CreateSprite : AssetPostprocessor {

    private static bool isChanaged = false;
    private static new string assetPath = "Assets/Resources/Assets/SpriteAsset.asset";
    /*private static string[] suffixs = new string[]
    {
        ".gif", ".jpg", ".png",
        ".bmp", ".jpeg", ".psd",
        ".tiff", ".tga", ".iff", ".pict"
    };*/

    [MenuItem("Tool/Load")]
    static void create()
    {
        SpriteAsset data =  ScriptableObject.CreateInstance<SpriteAsset>();
        //data.SpriteList = GetAllSprite();
        data.SpriteList = new List<Sprite>(Resources.LoadAll<Sprite>(""));

        int index = assetPath.LastIndexOf('/');
        Directory.CreateDirectory(assetPath.Remove(index,assetPath.Length-index));

        AssetDatabase.CreateAsset(data, assetPath);
    }

    private void OnPreprocessTexture()
    {
        TextureImporter impor = this.assetImporter as TextureImporter;
        impor.textureType = TextureImporterType.Sprite;
        impor.spriteImportMode = SpriteImportMode.Single;
    }

    public static void OnPostprocessAllAssets(
        string[] add,string[] deleted,string[] moved,string[]movedFrom)
    {
        var asset = AssetDatabase.LoadAssetAtPath<SpriteAsset>(assetPath);
        if(asset==null)
        {
            create();
            return;
        }
        foreach (var path in add)
        {
            var sprite= AssetDatabase.LoadAssetAtPath<Sprite>(path);
            if (sprite != null)
            {
                isChanaged = true;
                asset.SpriteList.Add(sprite);
            }
        }

        foreach(var path in deleted)
        {
            var sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
            if (asset.SpriteList.Remove(sprite))
            {
                isChanaged = true;
            }
        }

        if (isChanaged)
        {
            isChanaged = false;
            UpAsset(asset.SpriteList);
        }
    }

    private static void UpAsset(List<Sprite> list)
    {
        var asset = ScriptableObject.CreateInstance<SpriteAsset>();
        asset.SpriteList = list;

        AssetDatabase.CreateAsset(asset, assetPath);
    }
    
    /*
    private static List<Sprite> GetAllSprite()
    {
        List<Sprite> list = new List<Sprite>();
        DirectoryInfo direction = new DirectoryInfo("Assets");
        FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
        int index = Application.dataPath.IndexOf("Assets");

        foreach(FileInfo info in files)
        {
            foreach (var suffix in suffixs)
            {
                if (info.Name.EndsWith(suffix))
                {
                    var currentPath = info.FullName.Remove(0, index);
                    var sprite = AssetDatabase.LoadAssetAtPath<Sprite>(currentPath);
                    list.Add(sprite);
                }
            }
        }
        return list;
    }*/

}   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小鱼游戏开发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值