如何使用代码将子画面添加到子画面中?

问题描述 投票:0回答:1

我有一个Sprite和一个Spriteatlas,它们都具有“ Read / Write Enabled”。我想用代码将此精灵添加到spriteatlas的“打包对象”中。我找到了这个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
unity3d sprite
1个回答
0
投票

添加新事物后,您还需要将其保存回资产...当前,您仅加载资源并进行更改,但是在保存之前,这只是暂时的原因

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);
    }
}

请参见AssetDatabase.SaveAssets


注:在智能手机上键入,但我希望这个想法能清楚明白

© www.soinside.com 2019 - 2024. All rights reserved.