using System;
using UnityEngine;
namespace Framework.Dialogue {
/// Helper methods for retrieving and checking dialogue layer index
public static class DialogueLayerHelper {
/// Index of the layer on which the dialogue UI and hints are placed
public static int LayerIndex {
get {
if(!layerIndexLookedUp) {
layerIndex = LayerMask.NameToLayer("Dialogue");
if(layerIndex == -1) {
Debug.LogWarning(
"No 'Dialogue' layer has been set up. The DialogueManager requires this " +
"layer to locate placements. Dialogue will likely not function."
);
}
layerIndexLookedUp = true;
}
return layerIndex;
}
}
///
/// Checks that the specified game object is set up correctly as a dialogue placement
///
/// Game object that will be checked
public static void CheckDialoguePlacement(GameObject gameObject) {
ensureColliderPresent(gameObject);
ensureDialogueLayerUsed(gameObject);
}
/// Makes sure that the game object is on the dialogue layer
/// Game object that will be checked and fixed
private static void ensureDialogueLayerUsed(GameObject gameObject) {
int dialogueLayerIndex = LayerIndex;
if(dialogueLayerIndex != -1) {
if(gameObject.layer != dialogueLayerIndex) {
Debug.LogWarning(
"GameObject '" + gameObject.name + "' has a DialogueOwner but " +
"is not on the 'Dialogue' layer. Forcefully moving game object to " +
"that layer. If the GameObject is used for more than just dialogue " +
"this will likely break collision for it."
);
gameObject.layer = dialogueLayerIndex;
}
}
}
/// Makes sure that the game object has a collider
/// Game object that will be checked and fixed
private static void ensureColliderPresent(GameObject gameObject) {
Collider collider = gameObject.GetComponent();
if(collider == null) {
Debug.LogWarning(
"DialogueOwner on '" + gameObject.name + "' could not find a collider " +
"on its GameObject (required by the DialogueManager to locate placements), " +
"so a default SphereCollider was added."
);
SphereCollider sphereCollider = gameObject.AddComponent();
sphereCollider.radius = DialogueManager.DefaultMaximumDistance;
}
}
/// Whether we attempted to look up the dialogue layer index yet
private static bool layerIndexLookedUp;
/// Index of the layer on which dialogue placements and UI are
private static int layerIndex;
}
} // namespace Framework.Dialogue