如何使用代码将精灵添加到 spriteatlas 中?
How to add a sprite into spriteatlas with code?
我有一个 sprite 和一个 spriteatlas 都带有 "Read/Write Enabled"。
我想用代码将这个精灵添加到 spriteatlas 的 "Objects for Packing" 中。
我发现了这个 U2D.SpriteAtlasExtensions.Add。
但我不知道如何使用它。
下面是我的代码,但它不起作用。
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);
}
}
}
控制台是
AddPic
before add 0
after add 0
添加新内容后,您还需要将其保存回资产中……目前您只能加载资源并进行更改,但在您保存之前,这只是暂时的 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);
}
}
注意:在智能手机上打字,但我希望思路清晰
我有一个 sprite 和一个 spriteatlas 都带有 "Read/Write Enabled"。 我想用代码将这个精灵添加到 spriteatlas 的 "Objects for Packing" 中。 我发现了这个 U2D.SpriteAtlasExtensions.Add。 但我不知道如何使用它。 下面是我的代码,但它不起作用。
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);
}
}
}
控制台是
AddPic
before add 0
after add 0
添加新内容后,您还需要将其保存回资产中……目前您只能加载资源并进行更改,但在您保存之前,这只是暂时的 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);
}
}
注意:在智能手机上打字,但我希望思路清晰