using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Google.Maps.Examples.Shared {
///
/// Script for checking if a has been configured, and informing
/// the user if not.
///
///
/// This script is only meant to run in Editor. For the moment it works as a
/// , so it cannot be placed in an Editor folder,
/// and instead uses precompiler flags to make it run in the Unity Editor only.
///
public sealed class ApiKeyChecker : MonoBehaviour {
///
/// Website with instructions for getting an .
///
private const string ApiKeyWebsite =
"https://developers.google.com/maps/documentation/gaming/quick_start" +
"#step_5_set_the_api_key_value";
#if UNITY_EDITOR
// Prompt to get ApiKey from website with press of an EditorUtility.DisplayDialog button.
private const string WebsiteSolution = "Get an API Key to try out the Maps SDK for Unity with?";
#else
// Prompt to tell user (developer) to go to website themselves.
private const string WebsiteSolution = "Please get an API Key by going to: " + ApiKeyWebsite;
#endif
///
/// On starting a scene, see if there is a
/// in the scene missing its , printing a detailed error message
/// if so.
///
private void Awake() {
MapsService[] mapsServices = FindObjectsOfType();
if (mapsServices.Length != 0) {
ShowError(mapsServices);
}
}
///
/// Show an error saying that one or more in-scene
/// were missing an , and offer
/// potential solutions.
///
internal static void ShowError(params MapsService[] mapsServices) {
string mapsServicesWithoutApiKey = "";
string mapsServicesWithApiKey = "";
foreach (MapsService mapsService in mapsServices) {
string apiKey = mapsService.ResolveApiKey();
string messageLine = " " + GameObjectPath(mapsService.gameObject) + "\n";
if (string.IsNullOrEmpty(apiKey)) {
mapsServicesWithoutApiKey += messageLine;
} else {
mapsServicesWithApiKey += messageLine;
}
}
if (mapsServices.Length != 0 && mapsServicesWithoutApiKey.Length == 0) {
// If MapsServices were provided and they all have API keys, there's no error to show.
return;
}
string message;
if (mapsServicesWithoutApiKey.Length == 0) {
message = "No API Key found.\n";
} else {
message = "No API Key found on the MapsService components of these objects:\n\n" +
mapsServicesWithoutApiKey;
}
if (mapsServicesWithApiKey.Length == 0) {
message += "\nThe Maps SDK for Unity needs an API Key to run.\n\n" + WebsiteSolution;
ShowErrorWebsite("API Key Missing", message);
} else {
message += "\nAPI keys were found on MapsService components of the following objects:\n\n" +
mapsServicesWithApiKey +
"\nPlease make sure all Maps Services have their API Keys set.";
ShowErrorNotAll("API Key Missing", message);
}
}
///
/// Returns the path of a GameObject; i.e. the names of its parent objects up to the root,
/// separated by slashes.
///
private static string GameObjectPath(GameObject gameObject) {
string name = gameObject.name;
if (gameObject.transform.parent != null) {
name = GameObjectPath(gameObject.transform.parent.gameObject) + "/" + name;
}
return name;
}
///
/// Show an error linking to website where can get an .
///
private static void ShowErrorWebsite(string errorTitle, string errorMessage) {
#if UNITY_EDITOR
// Show EditorUtility.DisplayDialog with two options, 'Yes' and 'Cancel'.
if (EditorUtility.DisplayDialog(errorTitle, errorMessage, "Yes", "Cancel")) {
// Pressing 'Yes' (in response to question 'Get an ApiKey to try out the Maps SDK for Unity
// with?') stops play and opens website where can get an ApiKey.
EditorApplication.isPlaying = false;
Application.OpenURL(ApiKeyWebsite);
} else {
// Pressing 'Cancel' stops play immediately (as cannot run the scene without a ApiKey.
EditorApplication.isPlaying = false;
}
#else
Debug.LogError(errorMessage);
#endif
}
///
/// Show an error saying that an was found some, but not all
/// s in this scene.
///
private static void ShowErrorNotAll(string errorTitle, string errorMessage) {
#if UNITY_EDITOR
// Show EditorUtility.DisplayDialog and end play session, so when 'Cancel' (only button) is
// pressed, play stops (as the Maps SDK for Unity cannot run without ApiKeys on all elements).
EditorUtility.DisplayDialog(errorTitle, errorMessage, "Cancel");
EditorApplication.isPlaying = false;
#else
Debug.LogError(errorMessage);
#endif
}
}
}