Here's how you can compute the necessary scaling factor:
// Angle the camera can see above the center.
float halfFovRadians = camera.fieldOfView * Mathf.Deg2Rad / 2f;
// How high is it from top to bottom of the view frustum,
// in world space units, at our target depth?
float visibleHeightAtDepth = depth * Mathf.Tan(halfFovRadians) * 2f;
// You could also use Sprite.bounds for this.
float spriteHeight = spriteRenderer.sprite.rect.height
/ spriteRenderer.sprite.pixelsPerUnit;
// How many times bigger (or smaller) is the height we want to fill?
float scaleFactor = visibleHeightAtDepth / spriteHeight;
// Scale to fit, uniformly on all axes.
spriteRenderer.transform.localScale = Vector3.one * scaleFactor;
This matches the height of the sprite to the height the camera can "see" at that depth. I'm assuming here that your sprite's pivot is its center, placed at (0, 0, depth)
in the camera's coordinate system, and that the aspect ratio of the sprite is known to be as wide or wider than the widest supported resolution so it fills the frame.
Alternatively, as I mentioned previously, you could make a shader that does this automatically for a standard Unity quad with vertices running from (-0.5, -0.5) to (0.5, 0.5) by transforming them to the viewport corners:
Shader "Unlit/FillCameraFrame"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
[Toggle(PRESERVE_ASPECT)] _PreserveAspect ("Preserve Aspect Ratio", Float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature PRESERVE_ASPECT
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
#ifdef PRESERVE_ASPECT
float4 _MainTex_TexelSize;
#endif
v2f vert (appdata v)
{
v2f o;
o.vertex = float4(v.vertex.xy * 2.0f, 0.5f, 1);
o.vertex.y *= -1;
float2 uv = v.uv;
#ifdef PRESERVE_ASPECT
float cameraAspect = _ScreenParams.x * (_ScreenParams.w - 1.0f);
float invTextureAspect = _MainTex_TexelSize.w * _MainTex_TexelSize.x;
float conversion = invTextureAspect * cameraAspect;
// Scale from center, rather than from the corner.
// (Delete the 0.5 shifts if you prefer to anchor at left)
uv.x = (v.uv.x - 0.5f) * conversion + 0.5f;
#endif
o.uv = TRANSFORM_TEX(uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col * col.a;
}
ENDCG
}
}
}
This also works for tiling background images, repeating them if a very wide camera viewport exceeds the image's aspect ratio. (Or clamping them and filling the excess with the leftmost/rightmost column of texels, if you set the texture to clamp instead of repeat)