블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2737)
Unity3D (817)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
04-28 00:03

Unity 2022.3.10f1

NGUI 2022.08.01

----

[추가2] 2023-10-25

NGUI v2023.07.26에서 아래와 같은 업데이트가 올라왔다.

- FIX: NGUI will now change all imported textures to be uncompressed when creating an atlas, matching how it used to work in older versions of Unity.

이 수정 사항 때문에 NGUI Atlas 압축을 유지해주려던 부분이 작동하지 않고 무조건 Uncompressed로 변경되고 있어서 해당 부분인 아래 소스 부분(NGUIEditorTools.cs : line 538)을 주석 처리 했다.

//-------------------------------------------------
//			  NGUI: Next-Gen UI kit
// Copyright © 2011-2023 Tasharen Entertainment Inc
//-------------------------------------------------

using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

/// <summary>
/// Tools for the editor
/// </summary>

static public class NGUIEditorTools
{
    ...
    static public bool MakeTextureReadable (string path, bool force)
    {
        if (string.IsNullOrEmpty(path)) return false;
        var ti = AssetImporter.GetAtPath(path) as TextureImporter;
        if (ti == null) return false;

        var settings = new TextureImporterSettings();
        ti.ReadTextureSettings(settings);

        if (force || !settings.readable || settings.npotScale != TextureImporterNPOTScale.None || ti.textureCompression != TextureImporterCompression.Uncompressed)
        {
            settings.readable = true;

            if (NGUISettings.trueColorAtlas)
            {
                var platform = ti.GetDefaultPlatformTextureSettings();
                platform.format = TextureImporterFormat.RGBA32;
            }

            settings.npotScale = TextureImporterNPOTScale.None;
            
            #region NGUI Atlas 압축 관련 처리 [blueasa / 2023-10-25]
            /// NGUI v2023.07.26 에서 강제로 Uncompressed 하는 소스가 추가됨.
            /// 이 부분 때문에 아틀라스 압축이 유지되지 않고 풀리는 문제가 있어서 주석 처리 함
            /// trueColorAtlas 셋팅 될 때만 Uncompressed 강제 하도록 함
            if (NGUISettings.trueColorAtlas)
            {
                ti.textureCompression = TextureImporterCompression.Uncompressed;
            }
            #endregion
            
            ti.SetTextureSettings(settings);

#if UNITY_5_6
            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
#else
            ti.SaveAndReimport();
#endif
        }
        return true;
    }
    ...
}

 

----

[추가]

아틀라스 압축을 사용하려면 Atlas Maker의 Truecolor를 끄자.

(켜놓으면 압축상태를 무시하고 무조건 Truecolor로 아틀라스를 묶어낸다.)

Truecolor 체크 해제

----

 

[수정] New Atlas 관련 예외처리 추가(2023-04-12)

 

----

게임 최적화를 위해 Andrio/iOS 둘다 설정에서 Texture compression format을 ASTC로 설정하고 사용하고 있다.

NGUI Atlas도 최적화를 위해 Compression을 High Quality로 사용중인데

여기서 문제가 NGUI의 Atlas를 묶을 때는 Compression이 None(비압축)이 아니면 에러가 발생한다.

 

그래서 Atlas 묶을 때는 Compression을 None으로 풀어서 묶은 다음 원래 Compression인 High Quality로 변경하고 있었는데

불편하기도하고 사용하다 실수가 나오기도 해서 이참에 NGUI Atlas Maker를 수정해서 자동으로 되도록 했다.

 

작동 원리는 아래와 같이 간단하다.

(처음 해보는거라 적용 지점이 어딘지 찾는다고 좀 헤멤)

 

  [작동 원리]

----------------------------------------------------

- Atlas가 압축 된 상태면

  1. Compression 별도로 저장

  2. Compression : None으로 변경

  3. Atlas Make(NGUI 원래 소스)

  4. 별도로 저장했던 Compression을 Atlas에 적용해서 원래 압축 상태로 되돌려 줌

- Atlas가 압축 안된 상태(None)면

  1. 원래 소스만 실행(위에서 3.만 실행)

----------------------------------------------------

 

추가된 소스는 NGUI의 UIAtlasMaker.cs - UpdateTexture() 함수에 두 곳 추가 되었다.

소스 위치를 알기 위해 UpdateTexutre() 함수 전체를 올렸고, blueasa를 찾으면 region 정리해 둔 두 곳이 있다.

 

//-------------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2020 Tasharen Entertainment Inc
//-------------------------------------------------

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

/// <summary>
/// Atlas maker lets you create atlases from a bunch of small textures. It`s an alternative to using the external Texture Packer.
/// </summary>
public class UIAtlasMaker : EditorWindow
{
	....
    
    /// <summary>
    /// Combine all sprites into a single texture and save it to disk.
    /// </summary>
    
    static public bool UpdateTexture (INGUIAtlas atlas, List<SpriteEntry> sprites)
    {
        // Get the texture for the atlas
        var tex = atlasTexture;
        var oldPath = (tex != null) ? AssetDatabase.GetAssetPath(tex.GetInstanceID()) : "";
        var newPath = NGUIEditorTools.GetSaveableTexturePath(atlas as UnityEngine.Object, atlasTexture);
    
        // Clear the read-only flag in texture file attributes
        if (System.IO.File.Exists(newPath))
        {
            System.IO.FileAttributes newPathAttrs = System.IO.File.GetAttributes(newPath);
            newPathAttrs &= ~System.IO.FileAttributes.ReadOnly;
            System.IO.File.SetAttributes(newPath, newPathAttrs);
        }
    
        bool newTexture = (tex == null || oldPath != newPath);
    
        if (newTexture)
        {
            // Create a new texture for the atlas
            tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
        }
        else
        {
            // Make the atlas readable so we can save it
            tex = NGUIEditorTools.ImportTexture(oldPath, true, false, false);
        }
    
        #region NGUI Atlas 압축 관련 처리 [blueasa / 2023-04-07]
        /// NGUI Atlas가 압축돼 있으면,
        /// 압축 해제한 다음 Atlas 만든 후 원래 압축으로 돌리도록 함
    
        // TextureImporter 가져오기
        string strPath = AssetDatabase.GetAssetPath(tex);
        TextureImporter tiAtlas = AssetImporter.GetAtPath(strPath) as TextureImporter;
        TextureImporterCompression ticTextureCompression_Original = TextureImporterCompression.Uncompressed;
        bool bTextureCompressed = false;
    
        // New Atlas 관련 예외처리(New Atlas는 로직 타지 않도록 수정)
        if (false == newTexture)
        {
            Debug.LogWarningFormat("[Atlas TextureCompression] {0}", tiAtlas.textureCompression);
    
            if (tiAtlas.textureCompression != TextureImporterCompression.Uncompressed)
            {
                // Atlas Texutre 압축 여부
                bTextureCompressed = true;
                // 압축 설정 백업
                ticTextureCompression_Original = tiAtlas.textureCompression;
                // 압축 설정 변경
                tiAtlas.textureCompression = TextureImporterCompression.Uncompressed;
                // 변경 내용 적용
                AssetDatabase.ImportAsset(strPath);
            }
        }
        #endregion
    
        // Pack the sprites into this texture
        if (PackTextures(tex, sprites))
        {
            var bytes = tex.EncodeToPNG();
            System.IO.File.WriteAllBytes(newPath, bytes);
            bytes = null;
        
            // Load the texture we just saved as a Texture2D
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
            tex = NGUIEditorTools.ImportTexture(newPath, false, true, !premultipliedAlpha);
        
            // Update the atlas texture
            if (newTexture)
            {
                if (tex == null)
                {
                    Debug.LogError("Failed to load the created atlas saved as " + newPath);
                    EditorUtility.ClearProgressBar();
                }
                else
                {
                    var mat = spriteMaterial;
        
                    if (mat == null)
                    {
                        var matPath = newPath.Replace(".png", ".mat");
                        var shader = Shader.Find(NGUISettings.atlasPMA ? "Unlit/Premultiplied Colored" : "Unlit/Transparent Colored");
                        mat = new Material(shader);
	    
                        // Save the material
                        AssetDatabase.CreateAsset(mat, matPath);
                        AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
        
                        // Load the material so it`s usable
                        mat = AssetDatabase.LoadAssetAtPath<Material>(matPath);
                        spriteMaterial = mat;
                    }
        
                    mat.mainTexture = tex;
                }
        
                ReleaseSprites(sprites);
        
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
            }
        
            #region NGUI Atlas 압축 관련 처리 [blueasa / 2023-04-07]
            if (true == bTextureCompressed)
            {
                // 압축 설정 복원
                tiAtlas.textureCompression = ticTextureCompression_Original;
                // 변경 내용 적용
                AssetDatabase.ImportAsset(strPath);
            }
            #endregion
        
            return true;
        }
        else
        {
            if (!newTexture) NGUIEditorTools.ImportTexture(oldPath, false, true, !premultipliedAlpha);
    
            //Debug.LogError("Operation canceled: The selected sprites can`t fit into the atlas.\n" +
            //	"Keep large sprites outside the atlas (use UITexture), and/or use multiple atlases instead.");
    
            EditorUtility.DisplayDialog("Operation Canceled", "The selected sprites can`t fit into the atlas.\n" +
                    "Keep large sprites outside the atlas (use UITexture), and/or use multiple atlases instead", "OK");
            return false;
        }
    }
    
    ....
}

 

 

반응형
Posted by blueasa
, |