Reading parameters when game is opened from a custom URL scheme

I’ve scoured the forums here and the rest of the internet, but i’m still not 100% on how to get this working. I can’t find any plugins or good examples of how to do this.

I have a url like : myapp://parameter=1234

and I need to be able to get this parameter from inside my unity game. The game opens fine when opening from the link, so the url scheme is set up correctly.

I’m already doing

 (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation{
UnitySendMessage("ReceiveMessages", "openURLComplete", "Message to send");
}

in my UnityAppController.mm, which works fine when the app is already running in the background. But if it has to start from fresh, it doesn’t work.

Is there an easy way to accomplish this? I’m more than happy to buy a plugin but none seem to exist :frowning:

from the docs

So maybe there are some complications (like you need to return YES from both willLaunch and didLaunch)
Another thing is - unity do not start immediately in didFinishLaunchingWithOptions. If you check the code we do it from applicationDidBecomeActive (or rather we schedule starting up) so what most likely happens is that UnitySendMessage cannot send your message because nothing is ready yet. I would advise to subclass appcontroller (or smth) and store the fact that you were launched this way and post after startUnity: was called (you can override startUnity, call super and then send message)

I’ve heard about subclassing and categories but my objective c knowledge is literally zero (I’m a C# man) so I’m not really sure where to start. I tried putting in a UnitySendMessage in willFinishLaunchingWithOptions, but you’re correct that it is too early to call this (unity hasn’t fully loaded and crashes).

I’m not sure how to save the arguments, then call UnitySendMessage later when unity has actually started.

on second thought - it sounds like it “should” work. When you call
UnitySendMessage(“ReceiveMessages” …
you send message to object called ReceiveMessages - does it exist in first scene right away? Or you create it from code?
if it does exist - please bug report with small repro project and description on how to hit the bug. If you create it (or it is not in th every first scene) - try make it an object in first scene - UnitySendMessage delays sending messages to next frame - so it should actually happen, but it will be on the very first frame, when Start methods are called etc.