|
1 | 1 | # UnityDeeplinks
|
2 | 2 | A set of tools for Unity to allow handling deeplink activation from within Unity scripts
|
| 3 | + |
| 4 | +# Usage |
| 5 | +From within your Unity script, whenever the app gets activated by a deeplink, the following method will be called: |
| 6 | + |
| 7 | +``` |
| 8 | +public void onDeeplink(string deeplink) { |
| 9 | + Debug.Log("onDeeplink " + deeplink); |
| 10 | +} |
| 11 | +``` |
| 12 | + |
| 13 | +# Integration Instructions |
| 14 | +1. Clone/download the repository |
| 15 | +2. Copy the entire UnityDeeplinks folder into your Unity project Assets folder |
| 16 | + |
| 17 | +## Android |
| 18 | +There are two alternatives to handle a deeplink by a Unity app, depending on how your Unity project is currently built. It's up to you to decide which way to go. |
| 19 | + |
| 20 | +### Alternative 1: Subclassing UnityPlayerActivity |
| 21 | +In this approach, you use a subclass of the default *UnityPlayerActivity*, which contains deeplink-handling code that marshals deeplinks into your Unity script. This is more recommended approach, as it is the least complex. If you have no previous plans to subsclass UnityPlayerActivity, it's a good approach because then the subclassed code would not conflict with anything. If you do have plans to subclass UnityPlayerActivity, however, it would usually also be a good approach as the code changes you need to make are minimal. You will have to use the provided *MyUnityPlayerActivity* subclass as follows: |
| 22 | + |
| 23 | +* Replace the default UnityPlayerActivity in your Assets/Plugins/Android/AndroidManifest.xml with com.trophit.MyUnityPlayerActivity: |
| 24 | + |
| 25 | +``` |
| 26 | +<!-- |
| 27 | +<activity android:name="com.unity3d.player.UnityPlayerActivity" ... |
| 28 | +--> |
| 29 | +<activity android:name="com.trophit.MyUnityPlayerActivity" ... |
| 30 | +``` |
| 31 | + |
| 32 | +* Add the following inside the <activity> tag, assuming your deeplink URL scheme is myapp:// |
| 33 | +``` |
| 34 | + <intent-filter> |
| 35 | + <action android:name="android.intent.action.VIEW" /> |
| 36 | + <category android:name="android.intent.category.DEFAULT" /> |
| 37 | + <category android:name="android.intent.category.BROWSABLE" /> |
| 38 | + <data android:scheme="myapp" /> |
| 39 | + </intent-filter> |
| 40 | +``` |
| 41 | + |
| 42 | +* Optional: by default, *MyUnityPlayerActivity* calls a Unity script method `onDeeplink` on a game object called *UnityDeeplinks*. If you wish to change the name of the object or method, you should edit *Assets/UnityDeeplinks/Android/MyUnityPlayerActivity.java*, change the values of the `gameObject` and/or `deeplinkMethod` static properties and rebuild the *UnityDeeplinks.jar* file as instructed below |
| 43 | + |
| 44 | +### Alternative 2: Adding a Deeplink Activity |
| 45 | +In this approach, a second activity with deeplink-handling code is added to the Unity project, without subclassing the default activity. Use this is case where option #1 is not acceptable (code is too complex, not under your control, cannot be subclassed, etc) |
| 46 | + |
| 47 | +* Add the following activity to your Assets/Plugins/Android/AndroidManifest.xml, assuming your deeplink URL scheme is myapp:// |
| 48 | +``` |
| 49 | +<activity android:name="com.trophit.DeeplinkActivity" android:exported="true"> |
| 50 | + <intent-filter> |
| 51 | + <action android:name="android.intent.action.VIEW" /> |
| 52 | + <category android:name="android.intent.category.DEFAULT" /> |
| 53 | + <category android:name="android.intent.category.BROWSABLE" /> |
| 54 | + <data android:scheme="myapp" /> |
| 55 | + </intent-filter> |
| 56 | +</activity> |
| 57 | +``` |
| 58 | + |
| 59 | +* Optional: by default, *DeeplinkActivity* calls a Unity script method `onDeeplink` on a game object called *UnityDeeplinks*. If you wish to change the name of the object or method, you should edit *Assets/UnityDeeplinks/Android/DeeplinkActivity.java*, change the values of the `gameObject` and/or `deeplinkMethod` static properties and rebuild the *UnityDeeplinks.jar* file as instructed below |
| 60 | + |
| 61 | +### Building the UnityDeeplinks.jar file |
| 62 | +Only perform this step if you made changes to *Assets/UnityDeeplinks/Android/MyUnityPlayerActivity.java* or would like to rebuild it using an updated version of Unity classes, Android SDK, JDK and so on. |
| 63 | + |
| 64 | +#### Prerequisites |
| 65 | +* Go to your Unity => Preferences => External tools menu and find your Android SDK and JDK home folders |
| 66 | +* Edit *UnityDeeplinks/Android/bulid_jar.sh* |
| 67 | +* Ensure ANDROID_SDK_ROOT points to your Android SDK root folder |
| 68 | +* Ensure the BOOTCLASSPATH points to your JDK/jre/lib folder |
| 69 | +* Ensure UNITY_LIBS points to the Unity classes.jar file in your development environment |
| 70 | + |
| 71 | +#### Build instructions |
| 72 | +* Run the build script: |
| 73 | +``` |
| 74 | +cd MY_UNITY_PROJECT_ROOT/Assets/UnityDeeplinks/Android |
| 75 | +./build_jar.sh |
| 76 | +``` |
| 77 | +Example output: |
| 78 | +``` |
| 79 | +Compiling ... |
| 80 | +/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib |
| 81 | +/Applications/Unity/PlaybackEngines/AndroidPlayer/Variations/mono/Release/Classes/classes.jar:/kankado/dev/tools/android-sdk-macosx/platforms/android-23/android.jar |
| 82 | +Creating jar file... |
| 83 | +adding: com/(in = 0) (out= 0)(stored 0%) |
| 84 | +adding: com/trophit/(in = 0) (out= 0)(stored 0%) |
| 85 | +adding: com/trophit/MyUnityPlayerActivity.class(in = 1504) (out= 789)(deflated 47%) |
| 86 | +``` |
| 87 | +This creates/updates a *UnityDeeplinks.jar* file under your Unity project's Assets/UnityDeeplinks folder |
| 88 | + |
| 89 | +* Continue to build and test your Unity project as usual in order for any jar changes to take effect |
| 90 | + |
| 91 | +## iOS |
| 92 | + |
| 93 | +## Testing |
| 94 | + |
| 95 | +* Prepare a dummy html page that is accessible by your mobile device: |
| 96 | +``` |
| 97 | +<body> |
| 98 | +<a href="myapp://?a=b">deeplink test</a> |
| 99 | +</body> |
| 100 | +``` |
| 101 | + |
| 102 | +* Open the web page on the device browser and click the deeplink |
| 103 | +* The Unity app should open and the onDeeplink Unity script method should be invoked, performing whatever it is you designed it to perform |
0 commit comments