Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Setting minimum window size?

Discussion in 'Windows' started by User340, Jan 13, 2016.

  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Is there a way to set the minimum size of a WSA window? I'd like to set it to 800x650 for instance, so that the user can't size the window below that resolution. There has to be a way to do it with netfx_core, right?
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,526
    User340 likes this.
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Thanks for the API link. Here's the code that I've come up with (attempting to set the title):
    Code (CSharp):
    1. ApplicationView appView = ApplicationView.GetForCurrentView();
    2. appView.Title = "Custom Title";
    3.  
    It's causing a runtime error when deployed via Visual Studio (see attachment). Any ideas why this error is popping up? I'm calling it from a Unity UI Button click event.
     

    Attached Files:

  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,526
    You need to call it from Windows UI thread. Do this:

    Code (csharp):
    1. UnityEngine.WSA.Application.InvokeOnUIThread(() =>
    2. {
    3.     ApplicationView appView = ApplicationView.GetForCurrentView();
    4.     appView.Title = "Custom Title";
    5. }, false);
     
    Anacin and User340 like this.
  5. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,692
    Or better yet, call it from App.xaml.cs where it will be already in UI thread, so no need for UnityEngine.WSA.Application.InvokeOnUIThread.
     
    User340 likes this.
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Thanks, I didn't realize that you had to call it like that.
     
  7. Namal

    Namal

    Joined:
    Jul 6, 2012
    Posts:
    15
    Hi guys
    We are trying to set a min size for our UWP 10 app but our SetPreferredMinSize seems to be ignored...

    There is the App.waml.cs code :
    Code (CSharp):
    1.  
    2.  
    3. [...]
    4. protected override void OnLaunched(LaunchActivatedEventArgs args)
    5. {
    6.   ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(400, 400)); // test 1 : not working
    7.   splashScreen = args.SplashScreen;
    8.   InitializeUnity(args.Arguments);
    9.   ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(400, 400)); // test 2 : not working
    10. }
    11. [...]
    12.  
    Does we made something wrong ?
     
  8. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,692
    Try calling this just before
    Code (csharp):
    1.  
    2. Window.Current.Activate();
    3.  
     
  9. Namal

    Namal

    Joined:
    Jul 6, 2012
    Posts:
    15
    Its already called in the generated function "InitializeUnity"
    Find here the full code :
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using System.Linq;
    5. using System.Runtime.InteropServices.WindowsRuntime;
    6. using Windows.ApplicationModel;
    7. using Windows.ApplicationModel.Activation;
    8. using Windows.Foundation;
    9. using Windows.Foundation.Collections;
    10. using Windows.UI.Core;
    11. using Windows.UI.ViewManagement;
    12. using Windows.UI.Xaml;
    13. using Windows.UI.Xaml.Controls;
    14. using Windows.UI.Xaml.Controls.Primitives;
    15. using Windows.UI.Xaml.Data;
    16. using Windows.UI.Xaml.Input;
    17. using Windows.UI.Xaml.Media;
    18. using Windows.UI.Xaml.Navigation;
    19. using UnityPlayer;
    20. // The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=234227
    21.  
    22. namespace OutThereOmega
    23. {
    24.     /// <summary>
    25.     /// Provides application-specific behavior to supplement the default Application class.
    26.     /// </summary>
    27.     sealed partial class App : Application
    28.     {
    29.         private AppCallbacks appCallbacks;
    30.         public SplashScreen splashScreen;
    31.    
    32.         /// <summary>
    33.         /// Initializes the singleton application object.  This is the first line of authored code
    34.         /// executed, and as such is the logical equivalent of main() or WinMain().
    35.         /// </summary>
    36.         public App()
    37.         {
    38.             this.InitializeComponent();
    39.             appCallbacks = new AppCallbacks();
    40.         }
    41.  
    42.         /// <summary>
    43.         /// Invoked when application is launched through protocol.
    44.         /// Read more - http://msdn.microsoft.com/library/windows/apps/br224742
    45.         /// </summary>
    46.         /// <param name="args"></param>
    47.         protected override void OnActivated(IActivatedEventArgs args)
    48.         {
    49.             string appArgs = "";
    50.        
    51.             switch (args.Kind)
    52.             {
    53.                 case ActivationKind.Protocol:
    54.                     ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
    55.                     splashScreen = eventArgs.SplashScreen;
    56.                     appArgs += string.Format("Uri={0}", eventArgs.Uri.AbsoluteUri);
    57.                     break;
    58.             }
    59.             InitializeUnity(appArgs);
    60.         }
    61.  
    62.         /// <summary>
    63.         /// Invoked when application is launched via file
    64.         /// Read more - http://msdn.microsoft.com/library/windows/apps/br224742
    65.         /// </summary>
    66.         /// <param name="args"></param>
    67.         protected override void OnFileActivated(FileActivatedEventArgs args)
    68.         {
    69.             string appArgs = "";
    70.  
    71.             splashScreen = args.SplashScreen;
    72.             appArgs += "File=";
    73.             bool firstFileAdded = false;
    74.             foreach (var file in args.Files)
    75.             {
    76.                 if (firstFileAdded) appArgs += ";";
    77.                 appArgs += file.Path;
    78.                 firstFileAdded = true;
    79.             }
    80.  
    81.             InitializeUnity(appArgs);
    82.         }
    83.  
    84.         /// <summary>
    85.         /// Invoked when the application is launched normally by the end user.  Other entry points
    86.         /// will be used when the application is launched to open a specific file, to display
    87.         /// search results, and so forth.
    88.         /// </summary>
    89.         /// <param name="args">Details about the launch request and process.</param>
    90.         protected override void OnLaunched(LaunchActivatedEventArgs args)
    91.         {
    92.            ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(400, 400)); // before Window.Current.Activate();
    93.            splashScreen = args.SplashScreen;
    94.            InitializeUnity(args.Arguments); // calling Window.Current.Activate();
    95.            ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(400, 400));// after Window.Current.Activate();
    96.        }
    97.  
    98.         private void InitializeUnity(string args)
    99.         {
    100. #if UNITY_WP_8_1 || UNITY_UWP
    101.             ApplicationView.GetForCurrentView().SuppressSystemOverlays = true;
    102. #if UNITY_UWP
    103.       if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
    104. #endif
    105. #pragma warning disable 4014
    106.             {
    107.                 StatusBar.GetForCurrentView().HideAsync();
    108.             }
    109. #pragma warning restore 4014
    110. #endif
    111.             appCallbacks.SetAppArguments(args);
    112.             Frame rootFrame = Window.Current.Content as Frame;
    113.  
    114.             // Do not repeat app initialization when the Window already has content,
    115.             // just ensure that the window is active
    116.             if (rootFrame == null && !appCallbacks.IsInitialized())
    117.             {
    118.                 rootFrame = new Frame();
    119.                 Window.Current.Content = rootFrame;
    120.                 Window.Current.Activate();
    121.  
    122.                 rootFrame.Navigate(typeof(MainPage));
    123.             }
    124.  
    125.             Window.Current.Activate();
    126.         }
    127.     }
    128. }
    129.  
    I'm using Unity 5.3.2p4
     
  10. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,692
    I think Unity calls this function as well when initializing, but because you're calling SetPreferredMinSize after InitializeUnity(args.Arguments); you should be able to override the behavior.

    Can you check does it work if bare UWP project (create it from Visual Studio menu)
     
  11. Namal

    Namal

    Joined:
    Jul 6, 2012
    Posts:
    15
    Yep, on new UWP project it's works puting something like this at the end of OnLaunched func :
    Code (CSharp):
    1.  
    2. protected override void OnLaunched(LaunchActivatedEventArgs e) {
    3.   [...]
    4.   ApplicationView.PreferredLaunchViewSize = new Size { Height = 720, Width = 1280 };
    5.   ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
    6.   ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size { Width = 250, Height = 400 });
    7.   // Ensure the current window is active
    8.   Window.Current.Activate();
    9. }
    10.  
    But if I put the same code in the Unity generated project (at the end of OnLaunched or InitializeUnity functions) nothing happen...

    I'll try to update Unity to 5.3.3f1
     
  12. Namal

    Namal

    Joined:
    Jul 6, 2012
    Posts:
    15
    Hi,
    Any news about this ?
    We tried with Unity 5.3.3f1 without any changes... the windows can be resized under the given min size :(
     
  13. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,692
    So it seems Unity is overriding your setting during the initialization sequence, could you try this:
    Code (csharp):
    1.  
    2. ...
    3.             appCallbacks.Initialized += AppCallbacks_Initialized;
    4.             appCallbacks.SetAppArguments(args);
    5.  
    6. ...
    7.  
    8.         private void AppCallbacks_Initialized()
    9.         {
    10.             ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size { Width = 250, Height = 400 });
    11.  
    12.         }
    13.  
    14.  
    AppCallbacks_Initialized will be called after Unity is done initializing.
     
  14. BScrk

    BScrk

    Joined:
    Aug 12, 2014
    Posts:
    6
    Hi Tomas
    This workaround seems to be the good way to do it ;).
    Thanks.
     
  15. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    126
    I know this is an older thread, but there are two things that I wish Unity would do when it comes to UWP games.

    1) This minimum Window Size should be a setting that one can set at build, so that people don't need to edit the generated code. Especially since generated code is hard to edit for those who don't know CPP when using IL2CPP

    So I propose this as one enhancement.

    2) Most UWP games from AAA+ developers, if not all have an resize icon in the title bar. It would also be nice if Unity Games had this as well, so that the game could go back to full screen.

    Examples, check any game from Microsoft Studios as an example.

    Unity, please make these 2 changes to Unity!
     
  16. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,526
    This is fair feedback. However, you don't have to tinker with generated VS project. Just stick this in your Awake() function on some object in the first scene:

    Code (csharp):
    1. #if ENABLE_WINMD_SUPPORT
    2. UnityEngine.WSA.Application.InvokeOnUIThread(() => ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size { Width = 1024, Height = 768 }));
    3. #endif
    As far as I know, that "go to fullscreen button" is reserved for apps targeting Windows 8.1 (rather than Windows 10), as on Windows 8.1 Microsoft did not give app developers an API to resize a window. If you think that's not the case, can you please tell me a name of a game that you've seen this in?
     
  17. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    126
    I tried the code snippet but that throws an error, I think I used the right namespaces


    using Windows.Foundation;
    using Windows.UI.ViewManagement;

    As for the go to full screen, yeah I hadn't noticed it now, but it does appear to have been removed in Windows 10 in favor of ALT+Enter I think it is.
     
  18. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,526
    What kind of errors are those?
     
  19. CyberAngel

    CyberAngel

    Joined:
    Oct 4, 2014
    Posts:
    126
    The following
     

    Attached Files:

  20. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,526
    I made a typo in my code sample... InvokeOnUIThread takes 2 arguments. Pass false as the second argument.
     
  21. dm_bond

    dm_bond

    Joined:
    Sep 19, 2011
    Posts:
    61
    but SetPreferredMinSize only accepts values in the range from 192 x 48 to 500 x 500, which makes it not useful
     
  22. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,526
    That's the only way to do it on UWP unfortunately - it's a limitation of the platform.