Unity没有提供加载图集里面小图的API,下面是我学习框架课程(由于这个框架课程没有封装LoadAllAsset的方法)所学到的加载方法。框架
1.封装UIAtlas类,保存序列化的图集编辑器
/*******************
* Title:CW_FrameWark
* Author:CW
* ScriptName: UIAtlas
* Des:自定义Atlas
******************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace CW_FrameWark
{
public class UIAtlas : ScriptableObject
{
public Texture2D MainTexture;
public List Sprite SpriteList = new List Sprite
/// summary
/// 根据名字去获取Sprite
/// /summary
/// param name=\"spriteName\" /param
/// returns /returns
public Sprite GetSprite(string spriteName)
{
return SpriteList.Find(
(Sprite s)=
{
return s.name == spriteName;
}
);
}
/// summary
/// 设置Image的Sprite
/// /summary
/// param name=\"image\" /param
/// param name=\"spriteName\" /param
/// param name=\"isSetNative\" /param
public void SetSprite(ref Image image,string spriteName,bool isSetNative=false)
{
if(image==null)
{
return;
}
Sprite sp = GetSprite(spriteName);
if(sp!=null)
{
image.sprite = sp;
if(isSetNative)
{
image.SetNativeSize();
}
}
else
{
Log.Error(string.Format(\"你要设置的SpriteName:{0}没有在这个图集里面\"+spriteName));
}
}
}
}
2 编写编辑器脚本将图集序列化预设Atlaside
/*******************
* Title:CW_FrameWark
* Author:CW
* ScriptName: CreateAtlasTool
* Des:建立自定义的Atlas
******************/
using UnityEngine;
using System.Collections;
using UnityEditor;
namespace CW_FrameWark
{
public class CreateAtlasTool
{
[MenuItem(\"Assets/Create/MyAtlas prefab\")]
public static void CreateAtlasPrefab()
{
if(Selection.activeObject!=null)
{
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
TextureImporter impoter = AssetImporter.GetAtPath(path) as TextureImporter;
//判断选择的类型是一个图集
if(impoter!=null impoter.textureType==TextureImporterType.Sprite
impoter.spriteImportMode==SpriteImportMode.Multiple
)
{
UIAtlas atlas = ScriptableObject.CreateInstance UIAtlas
Object[] objs = AssetDatabase.LoadAllAssetsAtPath(path);
atlas.SpriteList.Clear();
foreach (Object obj in objs)
{
if(obj.GetType()==typeof(Texture2D))
{
atlas.MainTexture = obj as Texture2D;
}else if(obj.GetType()==typeof(Sprite))
{
atlas.SpriteList.Add(obj as Sprite);
}
}
AssetDatabase.CreateAsset(atlas, path.Replace(\".png\", \"_Atlas.prefab\"));
AssetDatabase.Refresh();
}
else
{
Log.Error(\"当前选中的不是图集图片!\");
}
}
}
}
}
3 这样咱们就大致的建立了一个自定义的Atlas了,可是这个Atlas在这个Inspector面板中咱们能够随意将图片指定给这个Atlas,容易形成“误操做”,全部咱们自定义Atlas的Inspector面板。学习
/*******************
* Title:CW_FrameWark
* Author:CW
* ScriptName: UIAtlasInspector
* Des:从新自定义Atlas Inspector面板
******************/
using UnityEngine;
using System.Collections;
using UnityEditor;
namespace CW_FrameWark
{
[CustomEditor(typeof(UIAtlas))]
public class UIAtlasInspector : Editor
{
public override void OnInspectorGUI()
{
UIAtlas atlas = target as UIAtlas;
atlas.MainTexture = EditorGUILayout.ObjectField(\"MainTexture\", atlas.MainTexture, typeof(Texture2D)) as Texture2D;
if(GUILayout.Button(\"刷新数据\"))
{
if(atlas.MainTexture==null)
{
string path= EditorUtility.OpenFilePanel(\"选着一张图集\", Application.dataPath, \"png\");
if(!string.IsNullOrEmpty(path))
{
atlas.MainTexture = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
}
}
if(atlas.MainTexture!=null)
{
string path = AssetDatabase.GetAssetPath(atlas.MainTexture);
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer != null importer.textureType == TextureImporterType.Sprite
importer.spriteImportMode == SpriteImportMode.Multiple)
{
Object[] objs = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(atlas.MainTexture));
atlas.SpriteList.Clear();
foreach (Object o in objs)
{
if (o.GetType() == typeof(Texture2D))
{
atlas.MainTexture = o as Texture2D;
}
else if (o.GetType() == typeof(Sprite))
{
atlas.SpriteList.Add(o as Sprite);
}
}
}
else
{
atlas.MainTexture = null;
}
}
}
if(atlas.SpriteList.Count 0)
{
foreach (Sprite s in atlas.SpriteList)
{
EditorGUILayout.ObjectField(s.name, s, typeof(Sprite));
}
}
}
}
}
spa
4 下面咱们就如何使用这个咱们自定义的Atlasorm
void Awake()
{
Image img=GetComponent Image x
UIAtlas atlas=Resources.Load UIAtlas (\"1AtlAS\")
atlas.SetSprite(ref img,\"1\");
}图片
本文链接: http://atlasref.immuno-online.com/view-696179.html