介绍

在打包 iOS 与 Android 游戏时,经常需要自定义不同版本的图标。

在网上找到如下几种方案:

环境

  • Python 3.7
  • Xcode 11
  • Android Studio 2.3

需求

  • 如果使用一张大图标生成所有小图标,有可能会出现缩放算法导致的效果不佳,因此决定不在程序中动态生成小尺寸图标,而是由用户提前准备好。
  • iOS 与 Android 尽可能统一使用一套图标。

方案

最终采用的方案是预先生成所有图标后

  • Android 直接替换图标文件。
  • Xcode 更新 images.xcassets 资源。

需要提前准备以下尺寸图标并按下表命名后放到单独的图标目录中:

  • 20x20.png
  • 29x29.png
  • 36x36.png
  • 40x40.png
  • 48x48.png
  • 50x50.png
  • 57x57.png
  • 58x58.png
  • 60x60.png
  • 72x72.png
  • 76x76.png
  • 80x80.png
  • 87x87.png
  • 96x96.png
  • 100x100.png
  • 114x114.png
  • 120x120.png
  • 144x144.png
  • 152x152.png
  • 167x167.png
  • 180x180.png
  • 192x192.png
  • 1024x1024.png

代码

  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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import json
import shutil
from math import trunc


def generate_ios_icons(xcode_project_path, icons_dir):
    # 从 Xcode 11 工程 Images.xcassets/AppIcon.appiconset/Contents.json 转换生成
    # 排列顺序调整为与 Unity 生成的一致,方便自动修改后比较文件内容
    images_template = [
        {
            "size": 57,
            "idiom": "iphone",
            "scale": 1
        },
        {
            "size": 57,
            "idiom": "iphone",
            "scale": 2
        },
        {
            "size": 60,
            "idiom": "iphone",
            "scale": 2
        },
        {
            "size": 60,
            "idiom": "iphone",
            "scale": 3
        },
        {
            "size": 29,
            "idiom": "iphone",
            "scale": 1
        },
        {
            "size": 29,
            "idiom": "iphone",
            "scale": 2
        },
        {
            "size": 29,
            "idiom": "iphone",
            "scale": 3
        },
        {
            "size": 40,
            "idiom": "iphone",
            "scale": 2
        },
        {
            "size": 40,
            "idiom": "iphone",
            "scale": 3
        },
        {
            "size": 20,
            "idiom": "iphone",
            "scale": 2
        },
        {
            "size": 20,
            "idiom": "iphone",
            "scale": 3
        },
        {
            "size": 29,
            "idiom": "ipad",
            "scale": 1
        },
        {
            "size": 29,
            "idiom": "ipad",
            "scale": 2
        },
        {
            "size": 40,
            "idiom": "ipad",
            "scale": 1
        },
        {
            "size": 40,
            "idiom": "ipad",
            "scale": 2
        },
        {
            "size": 50,
            "idiom": "ipad",
            "scale": 1
        },
        {
            "size": 50,
            "idiom": "ipad",
            "scale": 2
        },
        {
            "size": 72,
            "idiom": "ipad",
            "scale": 1
        },
        {
            "size": 72,
            "idiom": "ipad",
            "scale": 2
        },
        {
            "size": 76,
            "idiom": "ipad",
            "scale": 1
        },
        {
            "size": 76,
            "idiom": "ipad",
            "scale": 2
        },
        {
            "size": 20,
            "idiom": "ipad",
            "scale": 1
        },
        {
            "size": 20,
            "idiom": "ipad",
            "scale": 2
        },
        {
            "size": 83.5,
            "idiom": "ipad",
            "scale": 2
        },
        {
            "size": 1024,
            "idiom": "ios-marketing",
            "scale": 1
        }
    ]

    appicon_dir = os.path.join(xcode_project_path, 'Unity-iPhone/Images.xcassets/AppIcon.appiconset')
    cleared = False
    images = []
    for image_template in images_template:
        icon_name = '{0}x{0}.png'.format(trunc(image_template['size'] * image_template['scale']))
        icon_path = os.path.join(icons_dir, icon_name)
        if not os.path.exists(icon_path):
            continue

        if not cleared:
            for root, dirs, files in os.walk(appicon_dir):
                for f in files:
                    os.unlink(os.path.join(root, f))
                for d in dirs:
                    shutil.rmtree(os.path.join(root, d))
            cleared = True

        shutil.copy2(icon_path, appicon_dir)

        image = {'filename': icon_name, 'idiom': image_template['idiom'],
                 'scale': '{0}x'.format(image_template['scale']), 'size': '{0}x{0}'.format(image_template['size'])}
        images.append(image)

    contents = {
        'images': images,
        'info': {
            'author': 'xcode',
            'version': 1
        },
        'properties': {
            'pre-rendered': False
        }
    }

    contents_path = os.path.join(xcode_project_path, 'Unity-iPhone/Images.xcassets/AppIcon.appiconset/Contents.json')
    with open(contents_path, 'w', encoding='utf-8') as openfile:
        json.dump(contents, openfile, indent=4, sort_keys=False, ensure_ascii=False)


# 36x36.png src/main/res/drawable-ldpi/app_icon.png
# 48x48.png src/main/res/drawable-mdpi/app_icon.png
# 72x72.png src/main/res/drawable/app_icon.png
# 72x72.png src/main/res/drawable-hdpi/app_icon.png
# 96x96.png src/main/res/drawable-xhdpi/app_icon.png
# 144x144.png src/main/res/drawable-xxhdpi/app_icon.png
# 192x192.png src/main/res/drawable-xxxhdpi/app_icon.png

def generate_android_icons(gradle_project_path, icons_dir):
    icons_template = [
        {
            'size': 36,
            'name': 'drawable-ldpi'
        },
        {
            'size': 48,
            'name': 'drawable-mdpi'
        },
        {
            'size': 72,
            'name': 'drawable'
        },
        {
            'size': 72,
            'name': 'drawable-hdpi'
        },
        {
            'size': 96,
            'name': 'drawable-xhdpi'
        },
        {
            'size': 144,
            'name': 'drawable-xxhdpi'
        },
        {
            'size': 192,
            'name': 'drawable-xxxhdpi'
        },
    ]

    for icon_template in icons_template:
        icon_name = '{0}x{0}.png'.format(trunc(icon_template['size']))
        icon_path = os.path.join(icons_dir, icon_name)
        if not os.path.exists(icon_path):
            continue

        appicon_dir = os.path.join(gradle_project_path, 'src/main/res/{0}'.format(icon_template['name']))
        if not os.path.exists(appicon_dir):
            continue

        appicon_path = os.path.join(appicon_dir, 'app_icon.png')
        print(icon_path, appicon_path)
        shutil.copy2(icon_path, appicon_path)

使用

直接调用 generate_ios_iconsgenerate_android_icons 传递工程目录与图标目录。

注意

在格式化数字为字符串时,需要截断数字为整型,否则会出现 167.0x167.0.png 这种问题。