Search Unity

Help with changing my Unity application Icon (Top left on main window) on runtime

Discussion in 'Editor & General Support' started by FinalBoss69, May 29, 2016.

  1. FinalBoss69

    FinalBoss69

    Joined:
    Feb 12, 2016
    Posts:
    32
    Hello Guys,

    I'm trying to change the application icons on runtime based on my clients. I looked for days for a solution and only came up with using winapi and loading image but that works only for .ico and .bmp files but not for png.
    What I ended up using is the code below.

    It reads the icons at runtime from the disc. And it actually works "fine". I'm not sure that is the most ideal solution to my problem so I thought I get advise from bigger and better programmers.

    Edit: I just need it for windows standalone, Were using Unity pro

    Code (CSharp):
    1.     [DllImport("user32.dll")]
    2. private static extern IntPtr GetForegroundWindow();
    3.  
    4.     [DllImport("user32.dll", EntryPoint = "LoadImage", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    5.     static extern IntPtr LoadImage(IntPtr hinst, string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);
    6.  
    7.     [DllImport("user32.dll")]
    8.     public static extern int SendMessage(IntPtr hwnd, int message, int wParam, IntPtr lParam);
    9.  
    10.     private const int WM_SETICON = 0x80;
    11.     private const int ICON_SMALL = 0;
    12.     private const int ICON_BIG = 1;
    13.  
    14.  
    15.     void Awake()
    16.     {
    17.         if (Instance == null)
    18.         {
    19.             Instance = this;
    20.         }
    21.     }
    22.  
    23.     void Start()
    24.     {
    25.         IntPtr smallIconHandle = LoadImage(IntPtr.Zero, @"C:\Icons\16x16.ico", 1, 16, 16, 0x00008010);
    26.         IntPtr bigIcontHandle = LoadImage(IntPtr.Zero, @"C:\Icons\32x32.ico", 1, 32, 32, 0x00008010);
    27.         IntPtr handle = GetForegroundWindow();
    28.  
    29.         SendMessage(handle, WM_SETICON, ICON_SMALL, smallIconHandle);
    30.         SendMessage(handle, WM_SETICON, ICON_BIG, bigIcontHandle);
    31.     }
    Every help would be appreciated,
    Thanks!
     
    Last edited: May 30, 2016
  2. FinalBoss69

    FinalBoss69

    Joined:
    Feb 12, 2016
    Posts:
    32
    Anyone ?
     
  3. FinalBoss69

    FinalBoss69

    Joined:
    Feb 12, 2016
    Posts:
    32
    Bump please help
     
  4. FinalBoss69

    FinalBoss69

    Joined:
    Feb 12, 2016
    Posts:
    32
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Afaik, there's no API for this, so you have probably found the ideal solution.

    It would be nice to have the API, though.
     
  6. FinalBoss69

    FinalBoss69

    Joined:
    Feb 12, 2016
    Posts:
    32
    Ok, thanks! that'll keep my mind peaceful