-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSpriteAtlasHelper.cs
131 lines (99 loc) · 4.28 KB
/
SpriteAtlasHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#region Header
/* ============================================
* 작성자 : Strix
* 작성일 : 2020-02-15 오전 10:32:24
* 개요 :
*
* 참고 코드
* https://forum.unity.com/threads/creating-a-spriteatlas-from-code.511400/
============================================ */
#endregion Header
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.U2D;
using UnityEditor.U2D;
using System.IO;
/// <summary>
///
/// </summary>
public class SpriteAtlasHelper
{
private const string const_strSpriteAtlasExtensionName = ".spriteatlas";
/* const & readonly declaration */
/* enum & struct declaration */
/* public - Field declaration */
/* protected & private - Field declaration */
// ========================================================================== //
/* public - [Do] Function
* 외부 객체가 호출(For External class call)*/
// [MenuItem("Assets/Create SpriteAtlas for selected Sprites.")]
public static void DoCreateAtlas_ForSelectedSprites()
{
SpriteAtlas pSpriteAtlas = CreateSpriteAtlas();
UpdateAtlas(pSpriteAtlas, Selection.objects);
}
[MenuItem("Assets/UpdateAtlas_AllFile_InFolder")]
public static void DoUpdateAtlas()
{
Debug.Log(nameof(DoUpdateAtlas));
for (int i = 0; i < Selection.objects.Length; i++)
{
DefaultAsset pFolderAsset = Selection.objects[i] as DefaultAsset; // 폴더인지 체크
if (pFolderAsset == null)
continue;
string strFolderAssetPath = AssetDatabase.GetAssetPath(pFolderAsset);
SpriteAtlas pAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(strFolderAssetPath + const_strSpriteAtlasExtensionName);
if (pAtlas != null)
AssetDatabase.DeleteAsset(strFolderAssetPath + const_strSpriteAtlasExtensionName);
pAtlas = CreateSpriteAtlas(strFolderAssetPath);
SpriteAtlasPackingSettings pSetting = new SpriteAtlasPackingSettings();
pSetting.blockOffset = 0;
pSetting.padding = 4;
pSetting.enableTightPacking = false;
pSetting.enableRotation = true;
pAtlas.SetPackingSettings(pSetting);
string strCurrentDirectoryPath = Directory.GetCurrentDirectory();
DirectoryInfo pDirectory = new DirectoryInfo(Directory.GetCurrentDirectory() + "/" + strFolderAssetPath);
FileInfo[] arrFile = pDirectory.GetFiles();
List<Object> listFile = new List<Object>();
for(int j = 0; j < arrFile.Length; j++)
{
string strFileAssetPath = strFolderAssetPath + "/" + arrFile[j].Name;
Object pAsset = AssetDatabase.LoadAssetAtPath<Sprite>(strFileAssetPath);
if(pAsset != null)
listFile.Add(pAsset);
}
UpdateAtlas(pAtlas, listFile);
}
}
// ========================================================================== //
/* protected - Override & Unity API */
/* protected - [abstract & virtual] */
// ========================================================================== //
#region Private
private static SpriteAtlas CreateSpriteAtlas(string strAssetPath_WithName = "Assets/sample")
{
SpriteAtlas pSpriteAtlas = new SpriteAtlas();
AssetDatabase.CreateAsset(pSpriteAtlas, strAssetPath_WithName + const_strSpriteAtlasExtensionName);
return pSpriteAtlas;
}
private static void UpdateAtlas(SpriteAtlas pSpriteAtlas, IEnumerable<Object> arrObject)
{
Sprite[] arrSprite = new Sprite[pSpriteAtlas.spriteCount];
pSpriteAtlas.GetSprites(arrSprite);
pSpriteAtlas.Remove(arrSprite);
foreach (var pObject in arrObject)
{
Sprite pSprite = pObject as Sprite;
if (pSprite == null)
continue;
if (pSpriteAtlas.GetSprite(pSprite.name) == false)
Debug.Log($"{nameof(SpriteAtlasHelper)} - Added Sprite{pSprite.name} In Sprite Atlas{pSpriteAtlas.name}");
pSpriteAtlas.Add(new Object[] { pSprite });
}
AssetDatabase.SaveAssets();
}
#endregion Private
}