Search Unity

[Solved] Enforce Aspect Ratio of Game Window standalone for Mac OSx

Discussion in 'macOS' started by methos5k, Feb 27, 2017.

  1. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If anyone is able to help me on this issue, I would appreciate it.
    I am attempting to write a simple plugin that will allow me to set the window's aspect ratio (or the content's aspect ratio).
    I believe I have the proper functions/methods to call for this written in Objective-C, but they are not working as I would hope/imagine.

    -(void) setwindow
    {
    NSWindow *mainWindow = [[[NSApplication sharedApplication] windows] objectAtIndex:0];
    [mainWindow setAspectRatio:NSMakeSize(4, 3)];​
    }
    I have an extern "C" function setup and can confirm that it's calling that (even tested with an int return value).
    my extern...
    extern "C" {
    void setwindow(){
    WindowPlugin *wp = [[WindowPlugin alloc] init];
    [wp setwindow];
    }​
    }
    However, the window is not keeping with the aspect ratio when it's resized.
    Is there a problem to call this function from a Mac .bundle? Does it not know about the main window?
    Or is it something else that I'm missing or could try.
    Any help is appreciated. Thank-you in advance.
     
  2. inkredibl

    inkredibl

    Unity Technologies

    Joined:
    Nov 11, 2015
    Posts:
    21
    Hey, so what you didn't anticipate is that there are more windows than one. Also setting aspect ratio does not resize the window so you need to fix that too. To be more robust you could try something like this:
    Code (ObjC):
    1.     for (NSWindow* window in NSApplication.sharedApplication.windows)
    2.     {
    3.         if (window.isVisible)
    4.         {
    5.             [window setAspectRatio: NSMakeSize(4, 3)];
    6.             NSRect frame = window.frame;
    7.             frame.size.width = frame.size.height * 4 / 3;
    8.             [window setFrame: frame display: YES animate: NO];
    9.         }
    10.     }
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Wow, I appreciate very much your response. I knew that there could be more than 1 window, but did not realize that I had more than 1 window in my game :)
    I did know the part about setting the frame/window ; but I left that as a follow-up, had I gotten the first part done.
    This is working just as I had hoped. Thank-you very much.

    This is the behaviour I thought, before testing, that I would get with the player settings of allowing only certain aspect ratios :)

    Thanks, again. Hopefully this answer may help some other Mac users looking for a similar feat.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Inside : WindowPlugin.mm
    Code (ObjC):
    1. @implementation WindowPlugin
    2. -(void)setwindow
    3. {
    4.  
    5.     for (NSWindow* window in NSApplication.sharedApplication.windows)
    6.     {
    7.         if (window.isVisible)
    8.         {
    9.             [window setAspectRatio: NSMakeSize(4, 3)];
    10.             NSRect frame = window.frame;
    11.             frame.size.width = frame.size.height * 4 / 3;
    12.             [window setFrame: frame display: YES animate: NO];
    13.         }
    14.     }
    15. }
    16. @end
    17. extern "C" {
    18.     void setwindow(){
    19.     WindowPlugin *wp = [[WindowPlugin alloc] init];
    20.     [wp setwindow];
    21.     }
    22. }
    23.  
    Header.h
    Code (ObjC):
    1. @interface WindowPlugin : NSObject
    2. -(void) setwindow;
    3. @end
    4.  
    Inside a script
    Code (CSharp):
    1.  
    2. [DllImport ("UnityMacWindowPlugin", CallingConvention = CallingConvention.Cdecl)]
    3.    private static extern void setwindow ();
    4.  
    5. void Awake(){
    6. setwindow();
    7. }
    8.  
    Compiled this as a bundle in XCode, and added it to a folder "Plugins" in my Unity Project.