To add an outline (colored halo) to a GameObject in Unity, you can use a custom shader and a post-processing effect. This approach allows the outline to be visible even when the object is occluded, similar to the outline shown in the Unity Editor. Here's a step-by-step guide to achieve this:
Step 1: Create the Outline Shader
-
Create a new Shader:
- In the Unity Editor, right-click in the
Assets folder, select Create > Shader > Unlit Shader, and name it OutlineShader.
-
Edit the Shader:
- Open the
OutlineShader and replace its content with the following code:
Shader "Custom/OutlineShader"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_OutlineColor("Outline Color", Color) = (1,0,0,1)
_OutlineWidth("Outline Width", Float) = 0.1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct Input
{
float2 uv_MainTex;
};
half _OutlineWidth;
fixed4 _OutlineColor;
void surf(Input IN, inout SurfaceOutput o)
{
half dist = length(IN.screenPos.xy - _ScreenParams.xy * 0.5);
half outline = smoothstep(0.5 - _OutlineWidth, 0.5 + _OutlineWidth, dist);
o.Albedo = lerp(_OutlineColor.rgb, o.Albedo, outline);
o.Alpha = lerp(_OutlineColor.a, o.Alpha, outline);
}
ENDCG
}
FallBack "Diffuse"
}
Step 2: Create a Material Using the Shader
-
Create a new Material:
- Right-click in the
Assets folder, select Create > Material, and name it OutlineMaterial.
-
Assign the Shader:
- Select the
OutlineMaterial, and in the Inspector, change the Shader to Custom > OutlineShader.
-
Configure the Material:
- Set the
Color to the base color of your object.
- Set the
Outline Color to the desired outline color.
- Adjust the
Outline Width to control the thickness of the outline.
Step 3: Apply the Material to Your GameObject
-
Select Your GameObject:
- In the Unity Editor, select the GameObject you want to add an outline to.
-
Assign the Material:
- Drag the
OutlineMaterial onto the GameObject or assign it in the Mesh Renderer component.
Step 4: Implement a Post-Processing Effect (Optional)
To make the outline visible through occluding objects, you can use a post-processing effect. Unity’s Universal Render Pipeline (URP) or High Definition Render Pipeline (HDRP) can help achieve this.
-
Set Up URP:
- If you haven’t already, install the Universal Render Pipeline package from the Unity Package Manager.
- Create a URP Asset (
Assets > Create > Rendering > Universal Render Pipeline > Pipeline Asset (ForwardRenderer)).
- Assign the URP Asset to your Graphics Settings (
Edit > Project Settings > Graphics).
-
Add a Post-Process Volume:
- In the Unity Editor, right-click in the
Scene view, select Volume > Post Process Volume.
- Ensure the
Global checkbox is ticked to apply the post-processing effects to the entire scene.
-
Configure the Post-Process Volume:
- Select the Post Process Volume in the scene.
- In the Inspector, add an
Outline effect if available, or use a custom script to achieve the desired outline effect.
Step 5: Custom Script for Outline (if needed)
If the shader alone doesn’t achieve the desired effect, you might need a custom script to render the outline separately and composite it in a post-processing step.
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class OutlineEffect : MonoBehaviour
{
public Shader outlineShader;
private Material outlineMaterial;
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (outlineMaterial == null && outlineShader != null)
{
outlineMaterial = new Material(outlineShader);
}
if (outlineMaterial != null)
{
Graphics.Blit(source, destination, outlineMaterial);
}
else
{
Graphics.Blit(source, destination);
}
}
}
Attach this script to your camera and assign the OutlineShader to the outlineShader field.
This setup should give you a colored halo outline for your GameObject that can show through occluding objects, similar to the Unity Editor outline.