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.
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:
for (NSWindow* window in NSApplication.sharedApplication.windows)
{
if (window.isVisible)
{
[window setAspectRatio: NSMakeSize(4, 3)];
NSRect frame = window.frame;
frame.size.width = frame.size.height * 4 / 3;
[window setFrame: frame display: YES animate: NO];
}
}
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.
Inside : WindowPlugin.mm
@implementation WindowPlugin
-(void)setwindow
{
for (NSWindow* window in NSApplication.sharedApplication.windows)
{
if (window.isVisible)
{
[window setAspectRatio: NSMakeSize(4, 3)];
NSRect frame = window.frame;
frame.size.width = frame.size.height * 4 / 3;
[window setFrame: frame display: YES animate: NO];
}
}
}
@end
extern "C" {
void setwindow(){
WindowPlugin *wp = [[WindowPlugin alloc] init];
[wp setwindow];
}
}
Header.h
@interface WindowPlugin : NSObject
-(void) setwindow;
@end
Inside a script
[DllImport ("UnityMacWindowPlugin", CallingConvention = CallingConvention.Cdecl)]
private static extern void setwindow ();
void Awake(){
setwindow();
}
Compiled this as a bundle in XCode, and added it to a folder “Plugins” in my Unity Project.