Skip to content

Instantly share code, notes, and snippets.

@joonjoonjoon
Last active February 9, 2024 13:25
Show Gist options
  • Save joonjoonjoon/ff13118105f570665e0109f501ad6734 to your computer and use it in GitHub Desktop.
Save joonjoonjoon/ff13118105f570665e0109f501ad6734 to your computer and use it in GitHub Desktop.
ICNS icon resizer/generator for use in Unity editor scripting.
// ICNS icon resizer/generator for use in Unity editor scripting.
// The icon is grabbed from the Player settings main icon, which should be at least 512x512 and square.
// you should change the Output folder to whatever makes sense in your project
// Based on info found in this gist: https://gist.github.com/ansarizafar/6fa64f44aa933794c4d6638eec32b9aa
// - made by joon - feb 9 2024 (joon.be / @joonturbo) - no warranty
// How to use:
// Call ICNSGenerator.Generate();
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace TurboWing
{
public class ICNSGenerator
{
public static void Generate()
{
var iconName = "icontest";
var outputFolder = $"Automation/macos/ICNS";
// create dirs
Directory.CreateDirectory($"{outputFolder}/{iconName}.iconset");
var icon = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Unknown)[0];
var dimensions = new List<int>() { 16, 32, 128, 256, 512 };
for (int i = 0; i < dimensions.Count; i++)
{
var d = dimensions[i];
var filename = $"{outputFolder}/{iconName}.iconset/icon_{d}x{d}.png";
var scaledIcon = ScaleTexture(icon, d);
var pngbytes = ImageConversion.EncodeToPNG(scaledIcon);
File.WriteAllBytes(filename, pngbytes);
Debug.Log("Created " + filename);
var filename2x = $"{outputFolder}/{iconName}.iconset/icon_{d}x{d}@2.png";
var scaledIcon2x = ScaleTexture(icon, d * 2);
var pngbytes2x = ImageConversion.EncodeToPNG(scaledIcon2x);
File.WriteAllBytes(filename2x, pngbytes2x);
Debug.Log("Created " + filename2x);
}
// execute iconutil
Debug.Log("Executing iconutil in " +$"{Directory.GetCurrentDirectory()}/{outputFolder}");
RunTerminalCommand("iconutil", $"--convert icns {iconName}.iconset", $"{Directory.GetCurrentDirectory()}/{outputFolder}");
Debug.Log("Done");
}
static Texture2D ScaleTexture(Texture2D source, int dimension)
{
var scaledIconRt = new RenderTexture(dimension,dimension, 0);
Graphics.Blit(source, scaledIconRt);
RenderTexture.active = scaledIconRt;
var scaledIcon = new Texture2D(dimension, dimension, TextureFormat.RGBA32, false);
scaledIcon.ReadPixels(new Rect(0,0, dimension,dimension), 0,0);
scaledIcon.Apply();
RenderTexture.active = null;
Object.DestroyImmediate(scaledIconRt);
return scaledIcon;
}
static void RunTerminalCommand(string command, string arguments, string workingdirectory)
{
Process process = new Process();
process.StartInfo.FileName = command;
process.StartInfo.Arguments = arguments;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WorkingDirectory = workingdirectory;
process.OutputDataReceived += (sender, e) => Debug.Log(e.Data);
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data != null)
Debug.LogError(e.Data);
else
Debug.Log(e.Data);
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment