Manage Default Asset Import Settings in Unity

Example source code and project, Defaults Importer

If you’ve worked on Unity in a more production like environment, or perhaps had to tackle optimizing a project to run better on mobile devices, you will have no doubt experienced the problem of having to manage the import settings on all of your assets. Unfortunately this process is subject to errors, as often the defaults provided don’t always match your needs and when working with other team members tends to be overlooked.

Fortunately it is only a small amount of effort to write a set of asset importers to handle most of this work for you using the AssetPostProcessor class (it preprocesses too).

Creating a project for your importer

Before getting to the preprocessing functions themselves it is worth mentioning some important points about how importing works, and how we will setup our importer project.

In the Unity Scripting Reference entry for the AssetPostProcessor it mentions that you should create your importers in a separate DLL which is then placed somewhere within the Editor folder. This is very important as Unity checks assets first to see if they have been changed so that it can skip the import if it is already done. Along with the contents of an asset, the importers and their versions will also be checked and an asset reimported if a change is found. If, for example, you place an importer script directly into your project Unity will import the assets with that script. If afterwards you should write or change another script in the Editor folder and it fails to compile, the postprocessor will also not be compiled. To Unity this will appear as a change requiring all assets to be reimported–an often very time consuming process. Using a DLL guarantees the availability of the importer avoiding this problem.

Creating the DLL will mean creating an external project. I personally use Xamarin with a mono backend to ensure no issues (though technically any .NET 3.5 lib should be supported). In Xamarin studio create a new C# library project targeting the Mono / .NET 3.5 framework and import the UnityEngine and UnityEditor assemblies into the project references. Refer to the images below.

Target Framework Settings ImageReference Unity Assemblies Image


Unity can be a bit picky about the target framework, I found problems importing when using the MS build engine in Xamarin Studio so be sure to check these settings if you get any errors loading the assembly.

Assigning defaults during preprocessing

There are three types of assets than can be preprocessed using this approach: Models, Textures and Audio.


 public void OnPreprocessTexture()
     if (!isAssetProcessed)
         var textureImporter = assetImporter as TextureImporter;
         textureImporter.compressionQuality = 100;
         textureImporter.filterMode = FilterMode.Bilinear;
         textureImporter.maxTextureSize = 2048;
         textureImporter.textureFormat = TextureImporterFormat.AutomaticCompressed;


 public void OnPreprocessAudio()
     if (isAssetProcessed)
         var audioImporter = assetImporter as AudioImporter;
         audioImporter.forceToMono = false;
         audioImporter.compressionBitrate = 128000;
         audioImporter.format = AudioImporterFormat.Compressed;
         audioImporter.hardware = false;
         audioImporter.loadType = AudioImporterLoadType.DecompressOnLoad;
         if (assetPath.Contains("Streaming"))
             audioImporter.loadType = AudioImporterLoadType.StreamFromDisc;

When importing audio you may want to separate out the settings for sounds effects and streaming audio (music). Here we do this simply by checking the asset path for the word Streaming. You can use this same approach on any asset for finer control of how defaults should be applied.


 public void OnPreprocessModel()
     if (!isAssetProcessed)
         var modelImporter = assetImporter as ModelImporter;
         modelImporter.addCollider = false;
         modelImporter.animationCompression = ModelImporterAnimationCompression.KeyframeReduction;
         modelImporter.generateAnimations = ModelImporterGenerateAnimations.None;
         modelImporter.importAnimation = false;
         modelImporter.importMaterials = false;
         modelImporter.tangentImportMode = ModelImporterTangentSpaceMode.None;
         modelImporter.globalScale = 1f;
         modelImporter.isReadable = false;
         modelImporter.meshCompression = ModelImporterMeshCompression.Low;
         modelImporter.optimizeMesh = true;

Overriding the default settings

When using preprocessors you may notice that every time an asset is changed (or reimported) that the settings will go back to their defaults. This might be very handy if you do not want the settings to be changed, however sometimes it is necessary to make customizations beyond the defaults. To do this we simply need to track, using assetImporter.userData field, when an initial import is done so that further changes to these settings are not overwritten.

 static readonly string DEFAULTS_KEY = "DEFAULTS_DONE";
 static readonly uint DEFAULTS_VERSION = 2;
 public bool isAssetProcessed
         string key = string.Format("{0}_{1}", DEFAULTS_KEY, DEFAULTS_VERSION);
         return assetImporter.userData.Contains(key);
         string key = string.Format("{0}_{1}", DEFAULTS_KEY, DEFAULTS_VERSION);
         assetImporter.userData = value ? key : string.Empty;

You will need to make certain that no other importers in your project write to the userData field or use a more elaborate way of sharing the same field such as through a JSON structure. Then at the end of each preprocess function simply add the following line:

 isAssetProcessed = true;

That’s it! With this simple system you can enforce some sane import settings to help take the need for management out of the process.


Creative Commons License
Asset Defaults Importer by Clifford Roche is licensed under a Creative Commons Attribution 4.0 International License.

Unity Debugger for MonoDevelop 5 (Xamarin 2) Updated

I’ve update the Unity Soft Debugger add-in so that it is compatible with MonoDevelop 5 (or Xamarin Studio 2). It is available in the Beta channel of the add-in manager.

As this is a direct update from the previous version of MonoDevelop the plugin should still work for Unity 3.5 users. Similarly some the start/stop bug may also exist. Soon I’ll merge in any updates/bug fixes from the Unity repository.

More information about this add-in can be found on the original blog post: MonoDevelop 4 (Xamarin Studio) Debugging for Unity

GitHub: MonoDevelop forkMonoDevelop.Debugger.Soft.Unity forkDebuggerLibs

Update 2015-07-22

After speaking to on of the kind developers at Unity3D who works on the MonoDevelop port it turns out that they have released some of their plugins for the latest version of Xamarin Studio through their website, as a beta, so that developers can use them.

For this reason I will no longer be maintaining this project. The sources in Github will remain available for users of Unity 3.5 and Unity 4.

Unity Workflow for Alfred 2

I’m spending too much time trying to launch multiple instances of Unity recently so I’ve created a workflow for Alfred 2 (great program, BTW, you should get it) that allows you to easily list all Unity projects and launch the selected one. The best part is that the workflow will always launch Unity in a new instance so you can have multiple projects open at the same time.

Download it here

MonoDevelop 4 (Xamarin Studio) Debugging for Unity

This weekend I took some time to try and port the MonoDevelop.Debugger.Soft.Unity addins to Xamarin Studio. As it turns out it wouldn’t work, even when the addin compiled and loaded, because it relied on functionality that was added to the base MonoDebugger.Debugger.Soft namespace by Levi Bard; this add-in is built with MonoDevelop/Xamarin so instead I elected to fork the MonoDevelop 4.0 project from github, reintegrate these changes and build a MonoDevelop 4.0 package with the Unity soft debugger included.

Refer to GitHUB if you are interested in the source.

GitHub: MonoDevelop fork, MonoDevelop.Debugger.Soft.Unity fork, DebuggerLibs, Boo, Boo-Extensions, Boo-MD-Addins, UnityScript

Update 2015-07-22

After speaking to on of the kind developers at Unity3D who works on the MonoDevelop port it turns out that they have released some of their plugins for the latest version of Xamarin Studio through their website, as a beta, so that developers can use them.

For this reason I will no longer be maintaining this project. The sources in Github will remain available for users of Unity 3.5 and Unity 4.

Thanks for the support everyone.

MonoDevelop-Unity Attach to Process Screen Shot 2013-05-18 at 17.22.00For those who are curious, this is the project I’ve been working on Globulous available on the OUYA, Android, and iOS.


Fast Platform Switching in Unity

Multi platform titles in Unity are a bit of a pain due to the amount of time it can take to switch from one Platform to another (especially iOS). Unfortunately, Unity’s cache server is fairly expensive as it requires purchasing an Asset Server license and lets face it, Asset Server leaves a lot to be desired for a source control solution. Checking out multiple projects is an option, but makes it a bit more work to test changes across multiple platforms. Fortunately there is a very simple solution to this problem, filesystem junctions.

Lets say we have a project checked out in “/Projects/GameWindows”, create the folder “/Projects/GameAndroid”, then open a command line prompt in this directory to make a file system link like so:

mklink /j Assets ../GameWindows/Assets
mklink /j ProjectSettings ../GameWindows/ProjectSettings

Open the project at your newly created and linked directory and switch to the Android platform. All your assets and settings will now be linked across both workspaces, but the platform specific data in Library remains unique to each workspace. Switching from one platform to the other is now as simple as switching projects, you won’t have to reimport the entire library every time you do this, only the assets that have changed since you last opened the workspace. It is even possible to have both locations opened at the same time, generally Unity will notify you when an a shared asset has changed and reimport the asset with no issues.

This approach is also great for multiplayer testing.

Note: Deleting a linked folder in Explorer WILL NOT delete the target folder.

Unity Vertex Lighting with Lightmaps

If you’ve ever tried this before, using non-important (or vertex lighting) on a mesh that is also lightmapped in Unity you will already know that it doesn’t work. According to this thread on the Unity forums the reason for this is that if you select Vertex Lighting you’ve indicated that you want things to run as a fast as possible, and that generally means you don’t want lightmaps.

Well, it is not wrong. Generally speaking this is an accurate assessment, but as an experienced game developer sometimes you have to do things that seems counterintuitive. Similarly, in the mobile space, this method is used as a way to generate good looking lighting via lightmaps without resorting to relatively expensive per pixel dynamic lights.

Using Unity’s builtin shader source as a point of reference I’ve come up with something that appears to work.

Vertex Lit with Lightmapping Shader

Move the runtime light in the editor to see the effect it has on the lightmapped cube.

VertexLit with Lightmapping Sample

VertexLit with Lightmapping Sample

Note that the Main Color has been removed from the lightmap combiner, you can add it back if you want this. The primary (vertex light) color is added to the lightmap color, so I removed the DOUBLE from the main texture and lightmap combination to reduce oversaturation, ultimately you can change this to your needs.

Film Vignette Effect in Unity

Sample of a flickering (film) vignette effect for Unity.

The complete code and assets for this example can be found at

The process to doing this is fairly simple, first we create a texture and import it to unity as an Alpha 8 type texture. The alpha channel will be opaque on the outer edges and transparent on the inner area. Use this material on a quad in your camera’s view frustum and select the vignette shader.

The idea behind producing the flicker is fairly straight forward. Select a random value in a range, apply a slight bias, and blend from the current value to the next value. Once we reach our target choose a new random value and repeat the process. To make the shader look like the vignette is moving in and out of the edges of the frame we want to select intensity values that completely saturate part of the texture’s alpha.

There are a few tuneable variables we’ll use for polishing the effect:

public float IntensityBias = 0.6f;
public float MinimumIntensity = 1.0f;
public float MaximumIntensity = 1.4f;
public float BlendSpeed = 1.0f;

If you want a linear range of intensity values, set your IntensityBias to 0.5, if you would like to bias towards a more opaque frame move your IntensityBias upwards, values between 0.6 and 0.8 seem to work well.

A simple coroutine to select intensity values and blend continuously:

IEnumerator DoBlending()
   while (true)
      float rate = 1f / BlendSpeed * BlendUpdateRate;

      if (currentValue == targetValue)
         targetValue = Random.Range(MinimumIntensity, MaximumIntensity);
         float dir = Mathf.Sign(targetValue - currentValue);
         currentValue += dir * rate;

         if (dir != Mathf.Sign(targetValue - currentValue))
            currentValue = targetValue;

         float intensity = Bias(IntensityBias, currentValue);
         Shader.SetGlobalFloat("_Intensity", intensity);
         //UnityEngine.Debug.Log("Current intensity " + intensity.ToString() + " target intensity " + targetValue.ToString());

      yield return new WaitForSeconds(BlendUpdateRate);

The vignette shader to apply the effect, the 0.8f here is the saturation point of the alpha channel this way we can still see through the frame at the highest intensity. You can obviously change this to your needs.

half4 frag(v2f i) : COLOR
   half4 c = i.color;
   c.a = clamp(tex2D(_MainTex, i.uv).a * _Intensity, 0f, 0.8f);
   return c; 

All the code and assets in this example are public domain. You may freely reuse or modify in any way and for any purpose (even commercial) without the need to attribute or notify me. Of course if you would like to credit me this simple work, I certainly won’t complain.

A Much Simpler UIToolkit Shader (Unity)

A much simpler, even slightly more efficient, shader that works with Prime31’s UIToolkit (for Unity). This shaders removes the alpha test (an expensive operation on iDevices), replaces tint colour with a simple main colour (allows you to control a sprite’s alpha), and uses premultiplied alpha in the source texture. Premultiplied alpha’s help to reduce colour bleeding giving you a better visual result, are slightly less computationally expensive, and are supported by TexturePacker.

You may reuse or distribute this code in any way without either notification or credit.

Shader "Custom/UI Premultiplied Alpha"
		_Color( "Main Color", Color ) = ( 1, 1, 1, 1 )
		_MainTex( "UI Texture", 2D ) = "white" {}

		Tags { "Queue" = "Transparent" }
		Blend One OneMinusSrcAlpha
		ColorMask RGB
		Cull Off
		Lighting Off
		ZWrite On

			Bind "Vertex", vertex
			Bind "TexCoord", texcoord

					combine texture * constant ConstantColor[_Color]