Unity will talk about SpriteAtlas

Congratulations to Mr. Panda, after half a year of being free-range, he finally started working again. (Whispering Bibi, it’s really hard to find a job this year. Ambiguous ability, bumping into walls everywhere)

SpriteAtlas is a new content introduced by unity2017. In fact, it is an atlas. The biggest advantage is that when rendering pictures in the same atlas, only one DrawCall will be generated. (probably probably maybe so)

Let me talk about a few things about this thing today. Searching for other people's articles is really too difficult, I can't find all kinds of...

Based on the API of unity2018, let's talk about a few.

First, there is the issue of storage .

Atlas and the Texture or Sprite to be imported should not be placed in the Resources directory. Because the files in the Resources directory will not be entered into the atlas.

Then, there is the issue of loading .

In short:

SpriteAtlas sa = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(path);

SpriteAtlas is a class in UnityEngine.U2D.

In the official description, the method given is to make a script that inherits Mono, and then add SpriteAtlas to its List<SpriteAtlass>. But in this case, wouldn't it be very troublesome. Every time you create a new one, you need to mount it, or write an automatic addition.

Automated addition sounds great, but I mainly don't want to make a script like this. [dog head]

It must be solved with code. Think about it, atlas cannot be placed in Resources, so it is definitely not the Resources.Load() method. So you need to use AssetDatabase, a static class that can manage the files of the entire project.

Finally, let me talk about the attributes of the platform.

insert image description here
Some people's Unity does not have the following two small icons, which are the development support packages for IOS and Android. It can be downloaded in Build Settings.
insert image description here
Taking Android as an example, the default is like this. Override for Android means that when the Android package is released, various parameters use the data selected here.
insert image description here
However, I received a task yesterday, because every time I create a new atlas, this is not selected by default, so I have to change it manually, which is very troublesome. In fact, the trouble is second to none. The most important thing is that it is easy to forget. If you forget, you will not be able to achieve the ideal appearance when you deliver the package. It is necessary to re-(jia) out (ban).

So I need to automatically check override when someone creates a new atlas, and change the Format to RGBA ASTC 4x4.

In short:

public class AssetsPostManager : AssetPostprocessor {
    
    
    static void OnPostprocessAllAssets(string[] importedAsset, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
    
    

        if (0 < importedAsset.Length)
        {
    
    
            foreach (string filePath in importedAsset)
            {
    
    
                //判断是否是文件 跳过文件夹
                if (filePath.Contains("."))
                {
    
    
                    //通过后缀名做判断文件类型
                    string suffix = filePath.Substring(filePath.LastIndexOf('.') + 1);

                    switch (suffix)
                    {
    
    
                        case "spriteatlas":
                            OnPostprocessSpriteAtlas(filePath);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }

    //SpriteAtlas
    private static void OnPostprocessSpriteAtlas(string path)
    {
    
    
        SpriteAtlas sa = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(path);
        SpriteAtlasExtensions.SetPlatformSettings(sa, new TextureImporterPlatformSettings
        {
    
    
            name = "Android",
            overridden = true,
            format = TextureImporterFormat.ASTC_RGBA_4x4,
        });
    }

}

Next, let me talk about this code and the pit I encountered at that time.

AssetPostprocessor

AssetPostprocessor lets you hook into the import pipeline and run scripts prior or after importing assets.
Allows you to hook into the import pipeline and run scripts before or after importing assets.

You can also take a look at this article, it is very intuitive.
https://blog.csdn.net/qq826364410/article/details/86515209

It sounds like a good editor tool class. (I feel that the road to tool man has been opened)

public static void OnPostprocessAllAssets(string[] importedAsset, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)

All resource imports, deletions, and moves will call this method. Note that this method is static. You can see its parameters, imported assets, deleted assets, moved assets, and the path of assets before moving.

So I just spent ten minutes and did a bit of classification. Extract the SpriteAtlas and modify it in a method.

As for OnPostprocessSpriteAtlas, it is not native, and I hope people who see it will not be misled.

Next, is the part that took me several hours to modify its parameters.

First of all, it took more than 20 minutes to find a static class that can modify parameters, SpriteAtlasExtensions, in UnityEditor.

F12 go in and have a look, there are three main methods.

        // 摘要:
        //     Set the SpriteAtlasPackingSettings to use when packing this SpriteAtlas
        public static void SetPackingSettings(this SpriteAtlas spriteAtlas, SpriteAtlasPackingSettings src);
        // 摘要:
        //     Set the platform specific settings.
        public static void SetPlatformSettings(this SpriteAtlas spriteAtlas, TextureImporterPlatformSettings src);
        // 摘要:
        //     Set the SpriteAtlasTextureSettings for the packed texture generated by this SpriteAtlas.
        public static void SetTextureSettings(this SpriteAtlas spriteAtlas, SpriteAtlasTextureSettings src);

Look at the panel:
insert image description here
the red part, Packing. So its parameters are all processed in SetPackingSettings.

The blue part, Texture. So its parameters are all processed in SetTextureSettings.

The bottom one is about the settings of each platform. So its parameters are all processed in SetPlatformSettings.

At this time, I went to look at the api and found that which platform was not set? ? ?

I had to go to the Unity Api website to take a look. As mentioned above, TextureImporterPlatformSettings is a variant of TextureImporter.

Well, I'll take a look at TextureImporter. Not bad, found several useful methods.

        [Obsolete("Use UnityEditor.TextureImporter.SetPlatformTextureSettings(TextureImporterPlatformSettings) instead.")]
        public void SetPlatformTextureSettings(string platform, int maxTextureSize, TextureImporterFormat textureFormat);
        // 摘要:
        //     Set specific target platform settings.
        public void SetPlatformTextureSettings(TextureImporterPlatformSettings platformSettings);
        // 摘要:
        //     Set specific target platform settings.
        [Obsolete("Use UnityEditor.TextureImporter.SetPlatformTextureSettings(TextureImporterPlatformSettings) instead.")]
        public void SetPlatformTextureSettings(string platform, int maxTextureSize, TextureImporterFormat textureFormat, int compressionQuality, bool allowsAlphaSplit);
        // 摘要:
        //     Set specific target platform settings.
        [Obsolete("Use UnityEditor.TextureImporter.SetPlatformTextureSettings(TextureImporterPlatformSettings) instead.")]
        public void SetPlatformTextureSettings(string platform, int maxTextureSize, TextureImporterFormat textureFormat, [DefaultValue(false)] bool allowsAlphaSplit);
        

string platform, you can set the platform. Suddenly, Obsolete? ? ?

What the hell, it's been deprecated. feel lost.

Started surfing and added to see the API. After an hour, I surrendered.

I have to wait for the main program to have time to see what to do.

At this time, I can only go back to the place where the dream started, and then look at the TextureImporterPlatformSettings class.

Another sudden, I saw a name field. What is this for?

At first, I thought there would be many sets of configurations, so it would be useful as a note. Do you think it is necessary now?

Open the abstract and have a look:

// 摘要:
        //     Name of the build target.
        public string name {
    
     get; set; }

the build target

Let's see what the file looks like:

platformSettings:
    - serializedVersion: 2
      m_BuildTarget: Android
      m_MaxTextureSize: 2048
      m_ResizeAlgorithm: 0
      m_TextureFormat: 54
      m_TextureCompression: 1
      m_CompressionQuality: 50
      m_CrunchedCompression: 0
      m_AllowsAlphaSplitting: 0
      m_Overridden: 1
      m_AndroidETC2FallbackOverride: 0

m_BuildTarget:Android

I……

insert image description here
Ok, let's take a look at the API
insert image description here
.

Disaster. really difficult. Tools are too difficult.

Guess you like

Origin blog.csdn.net/qql7267/article/details/106681537