All Products
Search
Document Center

Alibaba Cloud DNS:Best practices for accessing the HTTPDNS SDK on Unity

Last Updated:Aug 15, 2025

This topic describes how to integrate the HTTPDNS SDK in Unity.

Overview

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

To help Unity developers integrate and use the HTTPDNS SDK, we provide the PdnsUnityDemo source code for reference. The source code includes the HTTPDNS Unity plugin.

Important

PdnsUnityDemo is developed using Unity 2019.4.21f1c1. The Assets/Plugins directory contains the plugin (you can also create a plugin based on your requirements using Alibaba Cloud DNS SDK). The plugin uses customized Gradle and Proguard files. If your project also customizes these files, you can merge the content.

Integration methods

  1. Import the Plugins files.

Copy the files in the Plugins folder of PdnsUnityDemo to the Unity project > Assets > Plugins folder.

  1. Configure 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. Configure 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. Configure 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 resolution requests.

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

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 stored separately based on network environments. If this feature is disabled, the same cached domain name resolution results are used across different networks.

public static void setIspEnable(bool enable)
  1. Specify the maximum time to live (TTL) for caching. If you configure 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 configure 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. You can use the scheme property to specify whether to use HTTP or HTTPS to access DNS servers for domain name resolution. The default value of scheme is 0, which indicates that HTTP is used. We recommend that you use HTTP because it provides faster resolution. If you set scheme to 1, HTTPS is used.

public static void setScheme(int scheme)
  1. Enable short mode. The DNS over HTTPS (DoH) JSON API for HTTPDNS returns data in either a full JSON format or a simple IP address array. The SDK uses the full JSON format by default.

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 configure 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 network environment (IPv4-only, IPv6-only, or dual-stack IPv4 and IPv6) and obtain an array of IP addresses that are suitable for the current network environment from the cache without a delay. If no data exists in the cache, or 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 result from the cache without a delay. If no data exists in the cache, or 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 result from the cache without a delay. If no data exists in the cache, or 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. Retrieve statistics on successful and failed requests to HTTPDNS.

public static string getRequestReportInfo()
Note
  1. This document applies only to using the HTTPDNS SDK for Android and iOS in Unity.

  2. For more information about integrating and using the HTTPDNS SDK for Android and iOS, see the Android SDK development guide and the iOS SDK development guide.

  3. For the complete sample code for integrating the HTTPDNS SDK for Android and iOS in Unity, see the PdnsUnityDemo source code.