For URP, here are the most practical methods to add outlines that show through walls:
Method 1: URP Renderer Feature (Recommended)
This is the most "URP-native" approach and gives you the cleanest results.
Step 1: Create the Renderer Feature Script
Create a new C# script called OutlineFeature.cs:
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class OutlineFeature : ScriptableRendererFeature
{
[System.Serializable]
public class OutlineSettings
{
public LayerMask outlineLayer = -1;
public Material outlineMaterial;
public int outlineWidth = 2;
public Color outlineColor = Color.yellow;
}
public OutlineSettings settings = new OutlineSettings();
private OutlinePass outlinePass;
public override void Create()
{
outlinePass = new OutlinePass(settings);
outlinePass.renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
if (settings.outlineMaterial == null)
{
Debug.LogWarning("Outline Material is missing");
return;
}
outlinePass.Setup(renderer.cameraColorTarget);
renderer.EnqueuePass(outlinePass);
}
class OutlinePass : ScriptableRenderPass
{
private OutlineSettings settings;
private RTHandle source;
private RTHandle tempTexture;
private Material blitMaterial;
public OutlinePass(OutlineSettings settings)
{
this.settings = settings;
}
public void Setup(RTHandle source)
{
this.source = source;
}
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
{
var descriptor = cameraTextureDescriptor;
descriptor.depthBufferBits = 0;
RenderingUtils.ReAllocateIfNeeded(ref tempTexture, descriptor);
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
CommandBuffer cmd = CommandBufferPool.Get("Outline Pass");
// Draw objects on outline layer to temp texture
using (new ProfilingScope(cmd, new ProfilingSampler("Outline Objects")))
{
var drawingSettings = CreateDrawingSettings(
new ShaderTagId("UniversalForward"),
ref renderingData,
SortingCriteria.CommonOpaque
);
var filteringSettings = new FilteringSettings(
RenderQueueRange.all,
settings.outlineLayer
);
// Clear temp texture
cmd.SetRenderTarget(tempTexture);
cmd.ClearRenderTarget(true, true, Color.clear);
// Draw objects
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings);
}
// Apply outline effect
cmd.SetGlobalTexture("_OutlineTexture", tempTexture);
cmd.SetGlobalColor("_OutlineColor", settings.outlineColor);
cmd.SetGlobalFloat("_OutlineWidth", settings.outlineWidth);
Blitter.BlitCameraTexture(cmd, source, source, settings.outlineMaterial, 0);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public override void OnCameraCleanup(CommandBuffer cmd)
{
tempTexture?.Release();
}
}
}
Step 2: Create the Outline Shader
Create a new shader file called OutlinePostProcess.shader:
Shader "Hidden/OutlinePostProcess"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
Pass
{
Name "OutlinePass"
ZWrite Off
ZTest Always
Cull Off
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D(_OutlineTexture);
SAMPLER(sampler_OutlineTexture);
float4 _MainTex_TexelSize;
float4 _OutlineColor;
float _OutlineWidth;
Varyings vert(Attributes input)
{
Varyings output;
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
output.uv = input.uv;
return output;
}
float4 frag(Varyings input) : SV_Target
{
float2 uv = input.uv;
float4 mainColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
// Sample outline texture
float outline = 0;
float2 texelSize = _MainTex_TexelSize.xy * _OutlineWidth;
// Simple edge detection
for(int x = -1; x <= 1; x++)
{
for(int y = -1; y <= 1; y++)
{
float2 offset = float2(x, y) * texelSize;
float4 sample = SAMPLE_TEXTURE2D(_OutlineTexture, sampler_OutlineTexture, uv + offset);
outline = max(outline, sample.a);
}
}
// Check if current pixel is inside object
float4 center = SAMPLE_TEXTURE2D(_OutlineTexture, sampler_OutlineTexture, uv);
outline *= (1.0 - center.a);
// Blend outline with main image
return lerp(mainColor, _OutlineColor, outline * _OutlineColor.a);
}
ENDHLSL
}
}
}
Step 3: Set Up the Feature
-
Create a material using the OutlinePostProcess shader
-
In your URP Renderer asset (usually in Assets/Settings):
- Click "Add Renderer Feature"
- Select "Outline Feature"
- Assign the outline material you created
- Set the outline layer (create a new layer called "Outline" if needed)
- Adjust color and width
-
Put objects you want outlined on the "Outline" layer
Method 2: Simple Two-Material Approach
For a quicker but less flexible solution, use two materials on your object:
Outline Shader (Shell Method)
Shader "URP/SimpleOutline"
{
Properties
{
_OutlineColor ("Outline Color", Color) = (1,1,0,1)
_OutlineWidth ("Outline Width", Range(0.0, 0.1)) = 0.03
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
Pass
{
Name "Outline"
Cull Front
ZWrite Off
ZTest Always // This makes it show through walls
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
};
float4 _OutlineColor;
float _OutlineWidth;
Varyings vert(Attributes input)
{
Varyings output;
float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
positionWS += normalWS * _OutlineWidth;
output.positionCS = TransformWorldToHClip(positionWS);
return output;
}
float4 frag(Varyings input) : SV_Target
{
return _OutlineColor;
}
ENDHLSL
}
}
}
To use this:
- Add this shader to a material
- On your GameObject's Mesh Renderer, add a second material slot
- Put your normal material in slot 0, outline material in slot 1
Method 3: Asset Store Solutions (Easiest)
For production use, I'd recommend these URP-compatible outline assets:
- Highlight Plus - Very popular, lots of features
- Quick Outline - Simple and effective
- Easy Performant Outline - Good performance
These handle edge cases, provide inspector controls, and work reliably across different URP versions.
Which Method to Choose?
- Renderer Feature: Best for multiple objects, most control, cleanest outlines
- Two-Material: Quick and simple, good for prototyping
- Asset Store: Best for production, saves development time
The Renderer Feature approach is closest to how Unity's editor does selection outlines internally, giving you the most professional results.