Unity does not provide an API to load the thumbnails inside the atlas, and here are the loading methods that I learned from the framework course (because this framework course does not encapsulate loadallasset methods).
1. Encapsulate the Uiatlas class and save the serialized atlas
/*******************
* Title:cw_framewark
* AUTHOR:CW
* Scriptname:uiatlas
* Des: Customizing 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>
Get the sprite by the name.
</summary>
<param name= "Spritename" ></param>
<returns></returns>
Public Sprite Getsprite (string spritename)
{
Return Spritelist.find (
(Sprite s) = =
{
return s.name = = Spritename;
}
);
}
<summary>
Set the sprite of an image
</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} you want to set is not in this Atlas" +spritename));
}
}
}
}
2 Writing editor scripts to serialize a set of collections to a preset atlas
/*******************
* Title:cw_framewark
* AUTHOR:CW
* Scriptname:createatlastool
* Des: Create a custom 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;
Determine the type of selection is an atlas
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 ("Currently selected is not an atlas picture!");
}
}
}
}
}
3 So we've basically created a custom atlas, but this atlas in this Inspector panel allows us to assign the image to this atlas at will, easily resulting in "mis-operation", all of which we customize Atlas's Inspector panel.
/*******************
* Title:cw_framewark
* AUTHOR:CW
* Scriptname:uiatlasinspector
* Des: Re-customizing the Atlas Inspector panel
******************/
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 ("Refresh Data"))
{
if (Atlas. Maintexture==null)
{
String path= editorutility.openfilepanel ("Select an Atlas", 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));
}
}
}
}
}
4 below we will use this our custom atlas
void Awake ()
{
Image img=getcomponent<image> (); x
Uiatlas atlas=resources.load<uiatlas> ("1AtlAS")
Atlas. Setsprite (ref img, "1");
}
Unity Custom Atlas to implement a small map in a loaded atlas