I have a sprite and a spriteatlas both with "Read/Write Enabled". I want to add this sprite into spriteatlas's "Objects for Packing" with code. And I found this U2D.SpriteAtlasExtensions.Add. But I don't know how to use it. Here is my code below, but it not works.
using UnityEngine;
using UnityEditor;
using UnityEngine.U2D;
using UnityEditor.U2D;
public class SpriteAtlasTool : EditorWindow
{
[MenuItem("Tools/SpriteAtlasTool")]
static void DoIt ()
{
EditorWindow.GetWindow(typeof(SpriteAtlasTool));
//EditorUtility.DisplayDialog("MyTool", "Do It in C# !", "OK", "");
}
private void OnGUI ()
{
if (true == GUILayout.Button("Test to Add"))
{
Debug.Log("AddPic");
SpriteAtlas temp = Resources.Load<SpriteAtlas>("Sprite Atlas/CharacterHeader/123");
Sprite a = Resources.Load<Sprite>("TalkFile/ChoiceCenter/325709-130GH0214652");
Debug.Log("before add " + temp.spriteCount);
SpriteAtlasExtensions.Add(temp, new Object[] { a});
AssetDatabase.Refresh();
Debug.Log("after add " + temp.spriteCount);
}
}
}
And Console is
AddPic
before add 0
after add 0
After adding the new thing you would also need to save it back to the asset ... currently you only load the resources and make a change but until you save it it is only temporary afaik
private void OnGUI ()
{
if (GUILayout.Button("Test to Add"))
{
Debug.Log("AddPic");
SpriteAtlas temp = Resources.Load<SpriteAtlas>("Sprite Atlas/CharacterHeader/123");
Sprite a = Resources.Load<Sprite>("/TalkFile/ChoiceCenter/325709-130GH0214652");
Debug.Log("before add " + temp.spriteCount);
SpriteAtlasExtensions.Add(temp, new Object[] { a});
// save all changed assets (basically the same as CTRL+S)
AssetDataBase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("after add " + temp.spriteCount);
}
}
Note: Typed on smartphone but I hope the idea gets clear