Still happily using it with Unity 2019.3 (just a minor fix for Editor required, which was straightforward and easy). No real alternative to the great platform support (20% of my Android user base still needing a 2x resolution).
That being said: for iOS I could well just go with 4x graphics. What would be a good way to not deliver 2x atlases for iOS, but 2x and 4x for Android?
That's good to hear about 2019.3. You mentioned you needed a minor fix for the editor - could you post that fix here, to help out other people that might need it?
As for 2x, 4x, etc. - I use the following, use at your own risk! Just place the file in an Editor folder in your project.
If your build fails, this will leave any files that it moved in OnPreprocessBuild in /Assets, so you'll need to move them back manually (easy if using source control). If the build completes normally, it will put everything back automatically.
The way it works is that it moves sprite collection files that aren't wanted for a given build, out of Resources folders, so that they are not included in the build, and then it moves them back after the build completes. You can tell it's working as you'll see a reduced build size.
Feel free to use/modify as you see fit.
If anyone has an easier way to handle this, let me know, thanks! Would be great if there was a quick code change we could make somewhere to exclude certain atlas sizes from builds.
![Smiley :)](https://www.2dtoolkit.com/forum/Smileys/default/smiley.gif)
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
class LocalBuildPrePost : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
struct Tk2dResourceToRestore
{
public string fileName;
public string pathAndFileNameToRestoreTo;
public Tk2dResourceToRestore(string fileName, string pathAndFileNameToRestoreTo)
{
this.fileName = fileName;
this.pathAndFileNameToRestoreTo = pathAndFileNameToRestoreTo;
}
}
public int callbackOrder { get { return 0; } }
private List<Tk2dResourceToRestore> mTk2dResourcesToRestore;
public void OnPreprocessBuild(BuildReport report)
{
BuildTarget target = report.summary.platform;
string path = report.summary.outputPath;
Debug.Log("LocalBuildPrePost.OnPreprocessBuild for target " + target + " at path " + path);
mTk2dResourcesToRestore = new List<Tk2dResourceToRestore>();
// iterate through all tk2dResource instances in Resources/TK2D so that we can exclude any that include "@1x", "@2x", or whatever we're wanting to exclude
tk2dResource[] tk2dResources = Resources.LoadAll<tk2dResource>("TK2D");
//Debug.Log("Resources/TK2D contents:");
foreach (var resource in tk2dResources)
{
//Debug.Log("resource.name: {0} -> objectReference.name: {1}", resource.name, resource.objectReference.name);
// any objectReference name that contains "@#x" (according to checks below) will get moved to /Assets temporarily (along with the corresponding .meta file), then will get moved back to Resources/TK2D when the build is done
#if UNITY_ANDROID
// keep @2x only
if (resource.objectReference.name.Contains("@1x") || resource.objectReference.name.Contains("@4x"))
#else
// keep @2x, @4x
if (resource.objectReference.name.Contains("@1x"))
#endif
{
string resourceAssetPath = AssetDatabase.GetAssetPath(resource);
string resourceFileName = Path.GetFileName(resourceAssetPath);
string resourceMetaFileAssetPath = resourceAssetPath + ".meta";
string resourceMetaFileName = resourceFileName + ".meta";
// cache this resource and it's original path so that we can restore it after the build
mTk2dResourcesToRestore.Add(new Tk2dResourceToRestore(resourceFileName, resourceAssetPath));
// cache the corresponding meta file
mTk2dResourcesToRestore.Add(new Tk2dResourceToRestore(resourceMetaFileName, resourceMetaFileAssetPath));
// move the file from Resources/TK2D to /Assets
FileUtil.MoveFileOrDirectory(resourceAssetPath, resourceFileName);
// move the meta file from Resources/TK2D to /Assets
FileUtil.MoveFileOrDirectory(resourceMetaFileAssetPath, resourceMetaFileName);
}
}
Debug.Log("moved {0} assets out of /Resources/TK2D temporarily", mTk2dResourcesToRestore.Count);
}
public void OnPostprocessBuild(BuildReport report)
{
BuildTarget target = report.summary.platform;
string path = report.summary.outputPath;
BuildResult result = report.summary.result;
Debug.Log("LocalBuildPrePost.OnPostprocessBuild for target " + target + " at path " + path + ", result: " + result);
Debug.Log("moving {0} assets from /Assets back to /Resources/TK2D", mTk2dResourcesToRestore.Count);
// iterate through mTk2dResourcesToRestore and move the files back to their original location
foreach (Tk2dResourceToRestore resource in mTk2dResourcesToRestore)
{
// move the file from /Assets to Resources/TK2D
FileUtil.MoveFileOrDirectory(resource.fileName, resource.pathAndFileNameToRestoreTo);
}
}
}