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

 

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 2013-08-19 

The Add-In has been released on the MonoDevelop Add-In Alpha channel. You will need to enable this channel to install the plugin. Also there is a UnityUtilities plugin which will give you syntax highlighting for shader lab, cg, and quick access to the Script Reference (in the Help Menu).

Barring a few exceptions which you can safely ignore that will be resolved in a future release of MonoDevelop (changes were merged and just need to be released). The add-in works well in Unity 3.5.

Unity 4.0+ has a few issues that I am currently investigating. You must RUN the application before attaching the debugger, and detach before stopping the application or you will crash Unity. There seem to be minute differences in the way the debugging protocol is being used between Xamarin and MonoDevelop 2.0 at the heart of this (I think).

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 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]
				}
			}
		}
	}
}