Category Archives: Thoughts?

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

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 https://github.com/cmroche/UnityVignetteShader

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);
      }
      else
      {
         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"
{
	Properties
	{
		_Color( "Main Color", Color ) = ( 1, 1, 1, 1 )
		_MainTex( "UI Texture", 2D ) = "white" {}
	}

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

		BindChannels
		{
			Bind "Vertex", vertex
			Bind "TexCoord", texcoord
		}

		SubShader
		{
			Pass
			{
				SetTexture[_MainTex]
				{
					combine texture * constant ConstantColor[_Color]
				}
			}
		}
	}
}