संपादित करें

इसके माध्यम से साझा किया गया


Other Unity APIs

Important

Visual Studio App Center is scheduled for retirement on March 31, 2025. While you can continue to use Visual Studio App Center until it is fully retired, there are several recommended alternatives that you may consider migrating to.

Learn more about support timelines and alternatives.

Adjust the log level

You can control the number of log messages that show up from App Center in the console. Use the LogLevel-API to enable additional logging while debugging. By default, it's set to ASSERT for the App Store environments and WARN otherwise.

To have as many log messages as possible, enable LogLevel.Verbose in the App Center settings or within your code.

AppCenter.LogLevel = LogLevel.Verbose;

Identify installations

The App Center SDK creates a UUID for each device once the app is installed. This identifier remains the same for a device when the app is updated. A new identifier is generated only when the app is reinstalled. The following API is useful for debugging purposes.

System.Guid? installId = await AppCenter.GetInstallIdAsync();

Identify users

The App Center SDK supports setting a user ID that's used to augment crash reports. To use this capability:

  1. Configure the App Center SDK as described in the App Center SDK Getting started guide.
  2. Set a userID in the SDK using the following code:
AppCenter.SetUserId("your-user-id");

After setting a user ID, you can use App Center's search feature to search for specific crash reports for the ID. Learn more in App Center's search documentation.

Note

The value for the user ID is limited to 256 characters. It will be shown with your crash reports but not used for aggregation or counts of affected users. In case you set user ID multiple times, only the last user ID will be used. You need to set the user ID yourself before each application launch, because this value isn't stored by the SDK between launches.

Disable all services at runtime

If you want to disable all App Center services at once, use the Enabled property. When disabled, the SDK won't forward any information to App Center.

AppCenter.SetEnabledAsync(false);

To enable all services at once again, use the same API but pass true as a parameter.

AppCenter.SetEnabledAsync(true);

You don't need to await this call to make other API calls (such as IsEnabledAsync) consistent.

The state is persisted in the device's storage across application launches.

Change state of service in runtime

You can enable or disable the service at the runtime with following code:

Analytics.SetEnabledAsync(true);

Disallow network requests

In the App Center SDK, network requests are allowed by default. If you want to send data that the App Center SDK collects by the user concern you can disallow automatic sending data.

AppCenter.IsNetworkRequestsAllowed = false;

In this case, the App Center SDK continues to collect data but it will be sent only when the network requests will be allowed.

AppCenter.IsNetworkRequestsAllowed = true;

Note

This value is retained between starts.

At any time, you can check whether sending data in the App Center SDK is allowed or not.

AppCenter.IsNetworkRequestsAllowed;

Note

The value saved previously in SharedPreferences is ignored until AppCenter is started on Android platform. It will return the last value set using AppCenter.IsNetworkRequestsAllowed = allowed or true if the value wasn't changed before AppCenter start.

Check if App Center is enabled

You can also check if App Center is enabled or not.

bool enabled = await AppCenter.IsEnabledAsync();

Check App Center SDK version at runtime

You can get the version of App Center SDK that you're currently using.

AppCenter.SdkVersion;

Delay the start of App Center

There may be cases where you want to delay the start of App Center until after AppCenterBehavior starts. For example, if you want to request user consent before starting any App Center services.
To do this, add AppCenterBehaviorAdvanced to the game object and check the Start native SDK from iOS/Android app checkboxes. This starts the SDK at the Start method of AppCenterBehavior and allows you to add a custom dialog or other logic.

Tip

If you generate the settings at build time, another way is to go to Player Settings > Other Settings > Scripting Define Symbols and paste APPCENTER_DONT_USE_NATIVE_STARTER.

Asynchronous APIs in the Unity SDK

Asynchronous APIs return a AppCenterTask object instead of returning the result directly, and return immediately instead of waiting for the action to finish.

There are three ways to interact with these methods.

Method 1: ContinueWith

To perform an action after the AppCenterTask's activity has completed, add a callback using the ContinueWith method.

Example:

AppCenter.IsEnabledAsync().ContinueWith(task =>
{
    // Do something with task.Result
});

In situations where the method has an actual return value, it will return AppCenterTask<{Return Type}> (as in the example above). In these situations, the task parameter in the callback will have a Result property that you can access.

Method 2: Built-in language features

If you're writing code that has access to .NET 4.6 or above, then AppCenterTask can be awaited in an asynchronous context.

Example:

bool isEnabled = await AppCenter.IsEnabledAsync();

Method 3: Coroutines

AppCenterTasks are also suitable for use in coroutines.

Example:

void SomeMethod()
{
    StartCoroutine(IsEnabledCoroutine());
}

IEnumerator IsEnabledCoroutine()
{
    var isEnabled = AppCenter.IsEnabledAsync();
    yield return isEnabled;

    // do something with the isEnabled
}