Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rename Window Title

Discussion in 'Windows' started by falkenbrew, Feb 18, 2021.

  1. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    I only found this (old) advice with google but have not managed to get it to work:

    Code (CSharp):
    1. //Import the following.
    2.      [DllImport("user32.dll", EntryPoint = "SetWindowText")]
    3.      public static extern bool SetWindowText(System.IntPtr hwnd, System.String lpString);
    4.      [DllImport("user32.dll", EntryPoint = "FindWindow")]
    5.      public static extern System.IntPtr FindWindow(System.String className, System.String windowName);
    6. //Get the window handle.
    7.      windowPtr = FindWindow(null, "Old Window Title");
    8. //Set the title text using the window handle.
    9.      SetWindowText(windowPtr, "New Window Title - Yayyy");
    I was wondering if Unity provides a more elegant solution in the mean time?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    are you renaming unity editor window, or build?

    in that example, did you change "Old Window Title" to correct original title?
     
  3. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    Do you want to change this at runtime? Unity sets it to whatever "Product Name" is set to in player settings. But yeah, that code looks right, as long as you can find the window.
     
  4. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    yes, I did change it to the actual window name. I tried it in the awake, start and update function to no avail. Turns out it does not work with special characters as I just tried now. My app is "sfäre". I renamed it to "sfare" and the code works now. But renaming it to "sfäre - F11 Fullscreen" results in some very weird characters.

     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    It's because it probably ends up calling non-unicode versions of those functions. Try adding CharSet = Unicode to the DllImport attribute.
     
  6. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    Thanks!!! That did the trick :)
     
  7. baize97

    baize97

    Joined:
    Jun 23, 2020
    Posts:
    30
    Hello, I used your answer to solve a problem, but user32.dll cannot be used under OSX. I searched a lot of documents on Google, but still couldn't find the answer I wanted. If you know how to solve this problem, please tell me, it will be very important to me.
     
  8. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    yeah that's only a solution for windows :) (#if UNITY_STANDALONE_WIN)
    I did not try to rename for mac/linux, sorry!
     
    baize97 likes this.
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Did you ever find a solution to this? I have need to change the window title at runtime too (and to make it worse, I need it on Mac, Windows, and Linux!).
     
    ade_epnp likes this.