Unity

📘

iOS 14 实施

为了帮助您全面管理用户体验,Branch SDK 将不会触发 IDFA 许可弹窗。

然而,如果您选择触发这一弹窗,在可行的情况下,Branch 仍然可以收集并使用 IDFA。

了解更多

🚧

iOS 11.2 上 Universal Links 行为不一致

将设备更新到iOS 11.2后,我们发现在安装应用后,该应用的 AASA 文件不再可靠地下载到您用户的设备上。因此,点击 Universal Links 将不再始终打开该应用。

You can set forced uri redirect mode on your Branch links to open the app with URI schemes. View details of the issue on the Apple Bug report.

🚧

Google Play Services 17+版本

If you reference Google Play Services version 17 or higher, you MUST complete Google's update instructions here.

由于 Google Play Services 于2019年6月进行了重大更改,未完成更新步骤将导致 Branch 的 Android SDK(以及其他各种跨平台 SDK,例如 Unity)停止收集 Android AID,而我们需要使用该 ID 来确保准确的深度链接和归因。

如果您运行的 Google Play Services 版本低于17,则无需更新。

NOTE: To use Google Play Services versions below 17 for future app builds, follow these workaround implementation steps.

🚧

Using you own Android Manifest

If you use your own Manifest, and not the one provided with the Branch SDK, make sure you change the android:name field of your launcher activity you are referencing from com.unity3d.player.UnityPlayerActivity to BranchUnityActivity. BranchUnityActivity extends the UnityPlayerActivity but also contains code responsible for initializing the Branch SDK on Android.

Note: Not doing so will result in the callback in init() never getting hit

配置 Branch

1689
  • 自定义您的 app.link domain:
1286

配置 Branch SDK

配置应用

  • Add the Branch prefab asset to the first scene of your Unity project

更新您的 Branch prefab

850

📘

注意事项

  • 完成后,请不要忘记点击更新 iOS 包装器”更新 Android Manifest(清单)键。
  • In case you cannot input the Branch Link Domain into the Live App Links and Test App Links, find the Branch Data Asset file and edit it directly.

Prefab Fields

Field

描述

Simulate Fresh Installs

此复选框启用调试模式。这使您可以在每次卸载并重新安装该应用时进行全新模拟。在发布您的应用之前,请确保取消选中此框。

测试模式

此复选框从您的 Branch prefab 中选择 test key。如果未选中此框,则默认情况下,将使用实时的 Branch key。

测试 Branch Key

This is the test Branch key found on the App Settings page of your Test Branch app.

测试 Branch URI

This is the test URI scheme that you have set for your app on the Link Settings page for your Test Branch app

测试 Android 路径前缀

This field is only applicable if you are on the bnc.lt domain of your Test Branch app. You can find it underneath the field labeled SHA256 Cert Fingerprints on the Link Settings page once you’ve enabled App Links. It will look something like this: /WSuf (the initial / character should be included).

测试 App Links

This field is applicable if you want to enable APPLINKS and UNIVERSAL LINKS for your domain. Please make sure to add the correct domain found on the bottom of the Link Settings page of your Test Branch app. Add the -alternate domain to have your Branch links deeplink from your Deepviews and Journeys. If you are not using a app.links domain please contact our support team

实时 Branch Key

This is the Live Branch key found on the App Settings page of your Live Branch app

实时 Branch URI

This is the Live URI scheme that you have set for your app on the Link Settings page for your Live Branch app

实时 Android 路径前缀

This field is only applicable if you are on the bnc.lt domain Link Settings page for your Live Branch app. You can find it underneath the field labeled SHA256 Cert Fingerprints on the Link Settings page once you’ve enabled App Links. It will look something like this: /WSuf (the initial / character should be included).

实时 App Links

This field is applicable if you want to enable APPLINKS and UNIVERSAL LINKS for your domain. Please make sure to add the correct domain found on the bottom of the Link Settings page of your Live Branch app. Add the -alternate domain to have your Branch links deeplink from your Deepviews and Journeys. If you are not using a app.links domain please contact our support team.

📘

Android 注意事项

Occasionally, Android will barf after you add our library due to generic issues unrelated to Branch. Please see this Android troubleshooting section

初始化 Branch

  • 将 Branch 添加到您的 Monobehavior script of your first Scene
using UnityEngine;
    using System.Collections;

    public class Spin : MonoBehaviour {

        // Use this for initialization
        void Start () {
            Branch.initSession(CallbackWithBranchUniversalObject);
        }

        void CallbackWithBranchUniversalObject(BranchUniversalObject buo,
                                                BranchLinkProperties linkProps,
                                                string error) {
            if (error != null) {
                Debug.LogError("Error : "
                                        + error);
            } else if (linkProps.controlParams.Count > 0) {
                Debug.Log("Deeplink params : "
                                        + buo.ToJsonString()
                                        + linkProps.ToJsonString());
            }
        }

        // Update is called once per frame
        void Update () {
            //rotate 90 degress per second
            transform.Rotate(Vector3.up * Time.deltaTime*90);
        }
    }

测试深度链接

  • Create a Quick link on the Branch Dashboard
  • 从设备中删除您的应用
  • Paste Quick link in Google Hangouts (Android) or Notes (iOS)
  • 点击 Quick Link(快速链接)以打开您的应用
  • 编译您的应用并将其下载到设备
  • 您应该在应用中看到延迟深度链接数据显示

实现功能

创建内容参考

  • The Branch Universal Object encapsulates the thing you want to share with your link
BranchUniversalObject universalObject = new BranchUniversalObject();
    // Content index mode: 0 - private mode, 1 - public mode
    universalObject.contentIndexMode = 1;
    //Identifier that helps Branch dedupe across many instances of the same content.
    universalObject.canonicalIdentifier = "id12345";
    // OG title
    universalObject.title = "id12345 title";
    // OG Description
    universalObject.contentDescription = "My awesome piece of content!";
    // OG Image
    universalObject.imageUrl = "https://s3-us-west-1.amazonaws.com/branchhost/mosaic_og.png";
    // User defined key value pair
    universalObject.metadata.Add("foo", "bar");

创建深度链接

  • After you have created a Branch Universal Object, Define Link Properties
BranchLinkProperties linkProperties = new BranchLinkProperties();
    linkProperties.tags.Add("tag1");
    linkProperties.tags.Add("tag2");
    // Feature link is associated with. Eg. Sharing
    linkProperties.feature = "invite";
    // The channel where you plan on sharing the link Eg.Facebook, Twitter, SMS etc
    linkProperties.channel = "Twitter";
    // The Campaign that drives this link
    linkProperties.campaign = "referral";
    linkProperties.stage = "2";
    // Parameters used to control Link behavior
    linkProperties.controlParams.Add("$desktop_url", "http://example.com");
  • 生成 Branch Link
Branch.getShortURL(universalObject, linkProperties, (params, error) => {
        if (error != null) {
            Debug.LogError("Branch.getShortURL failed: " + error);
        } else if (params != null) {
            Debug.Log("Branch.getShortURL shared params: " + url);
        }
    });

分享深度链接

  • 在用户和应用之间分享深度链接
Branch.shareLink(universalObject, linkProperties, "Sharing link: ", (parameters, error) => {
        if (error != null) {
            Debug.LogError("Branch.shareLink failed: " + error);
        } else if (parameters != null) {
            Debug.Log("Branch.shareLink: " + parameters["sharedLink"].ToString() + " " + parameters["sharedChannel"].ToString());
  }
    });

读取深度链接

public void CallbackWithBranchUniversalObject(BranchUniversalObject universalObject, BranchLinkProperties linkProperties, string error) {
        if (error != null) {
            Debug.LogError("Branch Error: " + error);
        } else {
            Debug.Log("Branch initialization completed: ");
            Debug.Log("Universal Object: " + universalObject.ToJsonString());
            Debug.Log("Link Properties: " + linkProperties.ToJsonString());
        }
    }

📘

注意

这与每个会话(应用安装和应用打开)相关

Retrieve link data from anywhere after Branch initsession

//get the latest referring params (last Branch link click)
BranchUniversalObject obj = Branch.getLatestReferringBranchUniversalObject();
BranchLinkProperties link = Branch.getLatestReferringBranchLinkProperties();

//get the first referring params (open or install)
BranchUniversalObject obj = Branch.getFirstReferringBranchUniversalObject();
BranchLinkProperties link = Branch.getFirstReferringBranchLinkProperties();

NativeLink™ Deferred Deep Linking (iOS Only)

  • Use iOS pasteboard to enable deferred deep linking via Branch NativeLink™

🚧

先决条件

Make sure the underlying iOS SDK Version is v1.39.4+

To use this feature you must:

Implement one of the pasteboard opt-in options in the native iOS SDK code.

导航和显示内容

  • 使用链接参数来确定要加载的视图
public class MyCoolBehaviorScript : MonoBehaviour {
        void Start () {
            Branch.initSession(delegate(Dictionary<string, object> parameters, string error) {
                if (parameters.ContainsKey("picture_id") {
                    // load the Scene to show the picture
                    SceneManager.LoadSceneAsync("ImageScene", LoadSceneMode.Additive);
                } else {
                    // load your normal Scene
                    SceneManager.LoadSceneAsync("NormalScene", LoadSceneMode.Single);
                }
            });
        }
    }

追踪内容

  • 如果要追踪用户查看 BUO 内容的次数
Branch.registerView(universalObject);

追踪用户

在登录/注册时 Set User-Ids

Sets the identity of a user (ID, UUID, etc) for events, deep links, and referrals

Branch.setIdentity("your user id");

注销时 UnSet User-Ids

Branch.logout();

追踪用户操作和事件

使用 BranchEvent 类可追踪特殊的用户操作或特定于应用的事件,而不仅限于追踪应用的安装,打开和分享。您可以追踪用户何时将商品添加到在线购物车中等事件或搜索关键字。BranchEvent 提供一个接口来添加由 BranchUniversalObject 代表的内容,以便将内容与事件连接。您可以查看在 Branch 操作后台 (Dashboard)上触发的 BranchEvent 的分析。 BranchEventType 枚举最常追踪的事件以及可以与 BranchEvent 一起使用以获得最佳结果的事件参数。您始终可以使用自定义事件名称和事件参数。

BranchEvent e01 = new BranchEvent (BranchEventType.COMPLETE_REGISTRATION);

e01.SetAffiliation("my_affilation");
e01.SetCoupon("my_coupon");
e01.SetCurrency(BranchCurrencyType.USD);
e01.SetTax(10.0f);
e01.SetRevenue(100.0f);
e01.SetShipping(1000.0f);
e01.SetDescription("my_description");
e01.SetSearchQuery("my_search_query");
e01.AddCustomData("custom_data_key01", "custom_data_value01");
e01.AddContentItem(universalObject);

Branch.sendEvent (e01);


BranchEvent e02 = new BranchEvent ("MY_CUSTOM_EVENT");

e02.SetAffiliation("my_affilation");
e02.SetCoupon("my_coupon");
e02.SetCurrency(BranchCurrencyType.USD);
e02.SetTax(10.0f);
e02.SetRevenue(100.0f);
e02.SetShipping(1000.0f);
e02.SetDescription("my_description");
e02.SetSearchQuery("my_search_query");
e02.AddCustomData("custom_data_key01", "custom_data_value01");
e02.AddContentItem(universalObject);

Branch.sendEvent (e02);

问题排查

iOS + Unity 4.6 注意事项

Branch requires ARC, and we don’t intend to add checks thoughout the SDK to try and support pre-ARC.
However, you can add flags to the project to compile the Branch files with ARC. If you wish to do this
add -fobjectivec-arc to all Branch files.

📘

注意

By default this flag is checked, but please check before building for iOS.

Android—使用您自己的自定义 Application 类

如果您使用自己的 Custom Application 类,则需要在您的项目中添加 Branch android library,并在 OnCreate()方法中调用以下代码:

Branch.getAutoInstance(this.getApplicationContext());

If you are using your own custom Activity class, please make sure that you are overriding the following functions:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onNewIntent(Intent intent) {
    this.setIntent(intent);
}

支持 Branch 与不同的插件

The Branch SDK has its own custom activity and application class. Other plugins that use their own
custom activity and application classes can cause "conflicts" between these classes. To resolve these conflicts:

  1. 创建一个空的 android library
  2. 将 Branch 插件以及其他插件添加到您的项目中
  3. 创建一个自定义的 Activity 和 Application 类,其中将包含所有插件的自定义逻辑
  4. 建立你的 library
  5. 将您的 library 添加到 Unity 项目
  6. Change android:name to name of your custom Application class in the application tag of your Manifest
  7. Change android:name to name of your custom Activity class in the activity tag of your Manifest

支持几个 IMPL_APP_CONTROLLER_SUBCLASS

Branch Unity SDK 插件使用其自己的 UnityAppController 来扩展默认 AppController。这用于捕获Universal Links。

@interface BranchAppController : UnityAppController
{
}
@end

@implementation BranchAppController

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler {
    BOOL handledByBranch = [[BranchUnityWrapper sharedInstance] continueUserActivity:userActivity];
    return handledByBranch;
}

@end

IMPL_APP_CONTROLLER_SUBCLASS(BranchAppController)

某些插件会扩展默认的 AppController,就像 Branch 的 Cardboard SDK 插件一样。解决冲突的方法:

  1. 将所有自定义 AppController 合并到一个。
  2. 注释其他 AppController 中的代码(或删除其他 AppController)。

示例应用

使用 Google Play Services 低于版本17 的替代办法实现步骤

  1. Install BranchUnityWrapper.unitypackage
  2. 删除 android-support-customtabs-23.3.0.jar from the Assets->Plugins->Branch->Android->libs.
  3. 创建一个 Assets-> Plugins-> Branch-> Editor 文件夹。
  4. Add the attached BranchPluginDependencies.xml to the new Editor folder.
  5. Install the latest Google Play Resolver from https://github.com/googlesamples/unity-jar-resolver
  6. 应该自动解决依赖项,但是您可能需要从 play resolver 菜单中列出 library 来进行确认。您应该看到以代码块:
dependencies {implementation 'com.android.installreferrer:installreferrer:1.0' // Assets/Plugins/Editor/BranchPluginDependencies.xml:11implementation 'com.android.support:customtabs:23.3.0' // Assets/Plugins/Editor/BranchPluginDependencies.xml:13implementation 'com.google.android.gms:play-services-ads:16.0.0' // Assets/Plugins/Editor/BranchPluginDependencies.xml:12}