Hello Guest

Author Topic: Leaving 1x assets out of builds  (Read 172 times)

baconbanditgames

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Bacon Bandit Games
Leaving 1x assets out of builds
« on: June 08, 2018, 11:46:31 PM »
I've been using 1x, 2x, and 4x assets for my sprite collections.

I don't need the 1x assets for any devices any more - is there a way to leave the 1x sprite collection texture and material out of builds?

I can't simply remove all 1x assets from my project at this point as I use the 1x resolution as my design resolution, so it will mess everything up if I try to remove it from the project entirely.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9673
    • View Profile
Re: Leaving 1x assets out of builds
« Reply #1 on: June 18, 2018, 10:10:14 PM »
Hi, it is possible to do this but needs a bit of procesing trickery.
Basically the assets that are included in the build are in Resources/TK2D/...

If you run through the assets, find the ones you dont want to include and mvoe them out of resources - do your build and then move them back you should be able to automate this.

baconbanditgames

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Bacon Bandit Games
Re: Leaving 1x assets out of builds
« Reply #2 on: June 25, 2018, 05:51:41 PM »
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).

Code: [Select]
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);
}
}
}