Skip to content

Instantly share code, notes, and snippets.

@meng-hui
Last active November 14, 2017 15:17
Show Gist options
  • Save meng-hui/9203332 to your computer and use it in GitHub Desktop.
Save meng-hui/9203332 to your computer and use it in GitHub Desktop.
Populate a Unity generated Xcode project with all icons for a Universal build from a single icon file. In the folder with the xcode project, execute this script and by default looks for a file named icon_1024x1024.png and places all the icons needed into Unity-iPhone/Images.xcassets/AppIcon.appiconset/ . Requires gd and fastimage_resize gem
#!/usr/bin/env ruby
# make sure you have gd and fastimage_resize
# brew install gd
# sudo gem install fastimage_resize
require 'fastimage_resize'
require 'optparse'
require 'json'
require 'fileutils'
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: resize_icons.rb [options]"
opts.separator ""
opts.separator "Specific Options"
options[:iconfile] = "icon_1024x1024.png"
opts.on('--icon [ICONFILE]', 'Path to icon file of size 1024 x 1024. Defaults to icon_1024x1024.png') do |f|
options[:iconfile] = f
end
options[:contentsjson] = "Unity-iPhone/Images.xcassets/AppIcon.appiconset/Contents.json"
opts.on('--xcassets [CONTENTSJSON]', 'Path to Contents.json file in Xcode Project Asset Catalog. Defaults to Unity-iPhone/Images.xcassets/AppIcon.appiconset/Contents.json') do |f|
options[:contentsjson] = f
end
opts.on_tail('-h', '--help', "Show this message") do
puts opts
exit
end
end #optparse
optparse.parse!
raise "Icon file #{options[:iconfile]} does not exist" unless File.file?(options[:iconfile])
raise "Contents.json file #{options[:contentsjson]} does not exist" unless File.file?(options[:contentsjson])
contents = JSON.parse(IO.read(options[:contentsjson]))
dest_folder = File.dirname(options[:contentsjson])
icons = [
#Settings
{:filename => "Icon-Small.png", :size => 29, :index=>0},
#Settings on devices with retina display
{:filename => "Icon-Small@2x.png", :size => 58, :index=>1},
#Spotlight on devices with retina display
{:filename => "Icon-Small-40@2x.png", :size => 80, :index=>2},
#Home screen on iPhone/iPod touch (iOS 6.1 and earlier)
{:filename => "Icon.png", :size => 57, :index=>3},
#Home screen on iPhone/iPod Touch with retina display (iOS 6.1 and earlier)
{:filename => "Icon@2x.png", :size => 114, :index=>4},
#Home screen on iPhone/iPod Touch with retina display
#{:filename => "Icon-60@2x.png", :size => 120, :index=>5},
{:filename => "Icon-120.png", :size => 120, :index=>5},
#Settings
{:filename => "Icon-Small.png", :size => 29, :index=>6},
#Settings on devices with retina display
{:filename => "Icon-Small@2x.png", :size => 58, :index=>7},
#Spotlight
{:filename => "Icon-Small-40.png", :size => 40, :index=>8},
#Spotlight on devices with retina display
{:filename => "Icon-Small-40@2x.png", :size => 80, :index=>9},
#Spotlight on iPad (iOS 6.1 and earlier)
{:filename => "Icon-Small-50.png", :size => 50, :index=>10},
#Spotlight on iPad with retina display (iOS 6.1 and earlier)
{:filename => "Icon-Small-50@2x.png", :size => 100, :index=>11},
#Home screen on iPad (iOS 6.1 and earlier)
{:filename => "Icon-72.png", :size => 72, :index=>12},
#Home screen on iPad with retina display (iOS 6.1 and earlier)
#{:filename => "Icon-72@2x.png", :size => 144, :index=>13},
{:filename => "Icon-144.png", :size => 144, :index=>13},
#Home screen on iPad
{:filename => "Icon-76.png", :size => 76, :index=>14},
#Home screen on iPad with retina display
#{:filename => "Icon-76@2x.png", :size => 152, :index=>15}
{:filename => "Icon-152.png", :size => 152, :index=>15}
]
icons.each do |icon|
FastImage.resize(options[:iconfile], icon[:size], icon[:size], outfile: icon[:filename])
FileUtils.mv(icon[:filename], dest_folder, {:force => true, :verbose =>true})
contents["images"][icon[:index]]["filename"] = icon[:filename]
end
#overwrite these files as well
FileUtils.cp(File.join(dest_folder, "Icon.png"), Dir.pwd, {:verbose =>true})
FileUtils.cp(File.join(dest_folder, "Icon@2x.png"), Dir.pwd, {:verbose =>true})
FileUtils.cp(File.join(dest_folder, "Icon-72.png"), Dir.pwd, {:verbose =>true})
FileUtils.cp(File.join(dest_folder, "Icon-144.png"), Dir.pwd, {:verbose =>true})
File.open(options[:contentsjson], "w") do |f|
f.write(JSON.pretty_generate(contents))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment