unity 加载图片文件
2017-07-13 04:35
411 查看
图片文件自动设置为Sprite
添加文件时,自动添加到列表
删除文件时,自动从列表删除
使用方式
该文件名字必须为SpriteAsset
添加文件时,自动添加到列表
删除文件时,自动从列表删除
使用方式
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; }*/ }
相关文章推荐
- unity 文件、图片加载与保存
- unity异步从外部文件加载音频和图片
- Unity--动态加载网络图片和本地图片文件
- Unity--动态加载网络图片和本地图片文件
- Unity 加载本地图片文件
- unity 文件、图片加载与保存
- unity加载文件的方法-用加载图片举例
- 正在加载中 图片大全,【绝对是你做软件急需的,多线程,文件处理,下载】
- VC从文件中加载图片
- VC从文件中加载图片
- 加载上一级目录下的图片文件并得到加载图片的大小
- exe4j打包的exe文件处理预加载图片和一句话提示的试用版
- 读取配置文件加载背景图片,音乐
- 【水晶报表之图片篇-a】 动态加载图片(图片文件版本及数据库版本)
- OpenGL从图片文件加载Texture时的上下颠倒问题
- 加载一个图片文件
- [WM C++]从资源文件中加载显示png/jpg图片
- 在CBitmap类中加载图像/图片文件
- VC从文件中加载图片
- 水晶报表中如何动态加载图片(图片文件版本及数据库版本)