Awesome, thank you for the answer!
Got this working, using pre/post process build functions. Tested and working in Unity 2017.4.3f1. Also works with Unity Cloud Build.
Code attached for anyone else that might want to do this (as a file and embedded in the post).
using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;
using System.Collections.Generic;
class LocalBuildPrePost : IPreprocessBuild, IPostprocessBuild
{
// this class excludes all 1x 2D Toolkit sprite collections from builds
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(BuildTarget target, string path)
{
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"
tk2dResource[] tk2dResources = Resources.LoadAll<tk2dResource>("TK2D");
foreach (var resource in tk2dResources)
{
// any objectReference name that contains "@1x" 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 (so that those files aren't included in the build)
if (resource.objectReference.name.Contains("@1x"))
{
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.LogFormat("Moved {0} assets out of /Resources/TK2D temporarily", mTk2dResourcesToRestore.Count);
}
//[PostProcessBuildAttribute(1)]
public void OnPostprocessBuild(BuildTarget target, string path)
{
Debug.Log("LocalBuildPrePost.OnPostprocessBuild for target " + target + " at path " + path);
Debug.LogFormat("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);
}
}
}