- Home /
How to get a player's desktop background and use it in the game?
So I'm working on trying to make a computer in my game similar to that of the player's desktop. I'm trying to get the player's background image and turn it into an image I can use. Does anyone know how to do that? For reference to what I am doing it is similar to that in PC Building Simulator or When the Darkness comes.
Answer by Hellium · May 16, 2019 at 02:57 PM
Based on this forum thread to know how to retrieve the path to the current desktop image + the documentation, I wrote the following code
Note that this code I've been tested on Windows only, in the editor.
For testing purpose, I've just put the wallpaper into a raw image, but you can do whatever you want with the resulting Texture2D.
 using System;
 using System.Runtime.InteropServices;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Wallpaper : MonoBehaviour
 {
     public RawImage RawImage;
     private const UInt32 SPI_GETDESKWALLPAPER = 0x73;
     private const int MAX_PATH = 260;
 
     [DllImport( "user32.dll", CharSet = CharSet.Auto )]
     private static extern int SystemParametersInfo( UInt32 uAction, int uParam, string lpvParam, int fuWinIni );
 
     void Start()
     {
         RawImage.texture = LoadTextureFromPath( GetCurrentDesktopWallpaperPath() );
     }
 
     private Texture2D LoadTextureFromPath( string path )
     {
         Texture2D texture = new Texture2D(2,2);
         texture.LoadImage( System.IO.File.ReadAllBytes( path ) );
         return texture;
     }
 
     private string GetCurrentDesktopWallpaperPath()
     {
         string currentWallpaper = new string('\0', MAX_PATH);
         SystemParametersInfo( SPI_GETDESKWALLPAPER, currentWallpaper.Length, currentWallpaper, 0 );
         return currentWallpaper.Substring( 0, currentWallpaper.IndexOf( '\0' ) );
     }
 }
Another way to get the path to the wallpaper is to retrieve it from the Windows registry, I don't know which way is better. (Credits to NICK RUSSLER )
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Wallpaper : MonoBehaviour
 {
     public RawImage RawImage;
     void Start()
     {
         RawImage.texture = LoadTextureFromPath( GetCurrentDesktopWallpaperPath() );
     }
 
     private Texture2D LoadTextureFromPath( string path )
     {
         Debug.Log( path );
         Texture2D texture = new Texture2D(2,2);
         texture.LoadImage( System.IO.File.ReadAllBytes( path ) );
         return texture;
     }
 
     private string GetCurrentDesktopWallpaperPath()
     {
         byte[] path = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop").GetValue( "TranscodedImageCache" ) as byte[];
         return System.Text.Encoding.Unicode.GetString(SliceByteArray(path, 24)).TrimEnd("\0".ToCharArray());
     }
 
     private static byte[] SliceByteArray( byte[] input, int index )
     {
         byte[] output = new byte[input.Length - index];
         System.Array.Copy( input, index, output, 0, output.Length );
         return output;
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                