All Products
Search
Document Center

Alibaba Cloud DNS:Best practices for integrating Alibaba Cloud Public DNS SDK on Unity

Last Updated:Mar 21, 2024

This topic describes how to integrate Alibaba Cloud Public DNS SDK on Unity.

Overview

Unity is a content creation engine. Developers can use Unity to develop 2D and 3D games, 2D and 3D applications, virtual reality (VR), augmented reality (AR), mobile apps, web frontends, and real-time movie rendering software. Unity can help developers turn ideas into reality.

Alibaba Cloud provides PdnsUnityDemo source code for developers to help them integrate and use Alibaba Cloud Public DNS SDK on Unity. The source code provides the Unity plug-in for Alibaba Cloud Public DNS.

Important

The demo project PdnsUnityDemo uses the Unity 2019.4.21f1c1 version. The Unity plug-in is stored in the Assets/Plugins directory. You can also use Alibaba Cloud DNS SDK to create a plug-in. The plug-in uses custom Gradle and Proguard files. If you use the custom files for other parts of your project, merge the content of the files.

Integration method

  1. Import files to the Plugins folder of your Unity project.

Copy the files in the Plugins folder of the demo project PdnsUnityDemo to the Plugins folder under Assets of your Unity project.

  1. Set parameters.

AlipdnsHelper.setAccountId("accountId");
AlipdnsHelper.setAccessKeyId("accessKeyId");
AlipdnsHelper.setAccesskeySecret("accesskeySecret");
AlipdnsHelper.setCacheEnable(true);
AlipdnsHelper.setSchedulePrefetchEnable(true);
AlipdnsHelper.setIspEnable(true);
AlipdnsHelper.setMaxCacheTTL(3600);
AlipdnsHelper.setMaxNegativeCache(60);
AlipdnsHelper.setScheme(1);
AlipdnsHelper.setShortEnable(false);
AlipdnsHelper.setSpeedTestEnable(true);
AlipdnsHelper.setCacheCountLimit(100);
AlipdnsHelper.setSpeedPort(80);
// Pre-resolve domain names.
List<string> list = new List<string>();
list.Add("Domain name 1");
list.Add("Domain name 2");
AlipdnsHelper.setPreloadDomains(list);

API reference

The interface class is AlipdnsHelper.cs.

  1. Set the accountId parameter. This parameter must be passed to the SDK. The accountId parameter specifies the account ID that is automatically generated in the Alibaba Cloud DNS console after you register your account.

public static void setAccountId(string accountId)
  1. Set the accessKeyId and accesskeySecret parameters. These parameters must be passed to the SDK. These parameters are generated after the authentication feature is enabled in the Alibaba Cloud DNS console. You can enable the authentication feature to ensure user identity security and prevent user identities from being stolen by unauthorized third parties.

public static void setAccessKeyId(string accessKeyId)
public static void setAccesskeySecret(string accesskeySecret)
  1. Enable the caching feature. If this feature is enabled, the data that is obtained the first time a domain name is resolved is saved in the cache and the cached data is preferentially returned for subsequent DNS requests. This can significantly accelerate DNS resolution.

public static void setCacheEnable(bool enable)
  1. Specify whether to enable the scheduled update of expired cached data. If this feature is enabled, the SDK automatically replaces the expired cached data with new data every minute. This way, data in the cache can be updated in a timely manner. However, the number of DNS resolutions and the consumption of client traffic may increase.

public static void setSchedulePrefetchEnable(bool enable)
  1. Specify whether to enable domain name caching based on ISP networks. If this feature is enabled, the cached domain name resolution results are separately stored based on networks. If this feature is not enabled, the same cached domain name resolution results are used in different networks.

public static void setIspEnable(bool enable)
  1. Specify the maximum time to live (TTL) for caching. If you set the maxCacheTTL parameter, the TTL for caching cannot exceed the value of this parameter.

    The default value of this parameter in the SDK is 3600. Unit: seconds.

public static void setMaxCacheTTL(double maxCacheTTL)
  1. Specify the maximum TTL for negative caching. If you set the maxNegativeCache parameter, the TTL for negative caching cannot exceed the value of this parameter.

    The default value of this parameter in the SDK is 30. Unit: seconds.

public static void setMaxNegativeCache(double maxNegativeCache)
  1. Set the scheme parameter to specify whether to use HTTP or HTTPS for DNS resolution. The default value of the scheme parameter is 0. The value 0 indicates that DNS resolution is performed over HTTP. If you set the scheme parameter to 1, DNS resolution is performed over HTTPS. We recommend that you retain the default value because DNS resolution over HTTP is faster than that over HTTPS.

public static void setScheme(int scheme)
  1. Specify whether to enable the short mode. If you enable the short mode, data that is returned by the JSON API for DoH of Alibaba Cloud Public DNS is an array of IP addresses. If you do not enable the short mode, a full JSON structure is returned. By default, the SDK uses a full JSON structure.

public static void setShortEnable(bool enable)
  1. Specify whether to enable the IP precedence feature. After this feature is enabled, the resolution result array is arranged in descending order based on the access speed of IP addresses.

public static void setSpeedTestEnable(bool enable)
  1. Specify the maximum number of domain names whose resolution results can be cached. Valid values: 100 to 500.

public static void setCacheCountLimit(int cacheCountLimit)
  1. Specify the port for access speed testing of IP addresses. This port is the port that you specify for socket detection.

public static void setSpeedPort(int speedPort)
  1. Preload domain names. The caching feature provided by the SDK helps resolve a domain name that has been resolved in an efficient manner. We recommend that you preload the domain names that you want to resolve after you start your app.

public static void setPreloadDomains(List<string> hosts)
  1. Automatically detect the IP address type that is supported by the current network environment, such as IPv4-only, IPv6-only, or IPv4/IPv6 dual-stack, and obtain the array of IP addresses that are supported by the current network environment from the cache without a delay. If no data exists in the cache, or if the data in the cache has expired and the expiredIPEnabled parameter is set to false, an empty array is returned.

@ param host The domain name that you want to resolve.

@expiredIPEnabled Specifies whether to return expired IP addresses.

public static List<string> getIpsByCacheWithDomain(string host, bool expiredIPEnabled)
  1. Obtain the IPv4 address resolution results from the cache without a delay. If no data exists in the cache, or if the data in the cache has expired and the expiredIPEnabled parameter is set to false, an empty array is returned.

@ param host The domain name that you want to resolve.

@expiredIPEnabled Specifies whether to return expired IP addresses.

public static List<string> getIpv4ByCacheWithDomain(string host, bool expiredIPEnabled)
  1. Obtain the IPv6 address resolution results from the cache without a delay. If no data exists in the cache, or if the data in the cache has expired and the expiredIPEnabled parameter is set to false, an empty array is returned.

@ param host The domain name that you want to resolve.

@expiredIPEnabled Specifies whether to return expired IP addresses.

public static List<string> getIpv6ByCacheWithDomain(string host, bool expiredIPEnabled)
  1. Obtain the statistics on successful and failed queries of Alibaba Cloud Public DNS.

public static string getRequestReportInfo()
Note

  1. This topic provides references only for integrating Alibaba Cloud Public DNS SDK for Android or iOS on Unity.

  2. For more information about how to integrate and use Alibaba Cloud Public DNS SDK for Android or iOS, see SDK for Android developer guide or SDK for iOS developer guide.

  3. For the complete code for integrating Alibaba Cloud Public DNS SDK for Android or iOS on Unity, see PdnsUnityDemo source code.