您的位置:首页 > 移动开发 > Unity3D

unity 加载图片文件

2017-07-13 04:35 411 查看
图片文件自动设置为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
7d27
,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;
}*/

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: