/*
Yarn Spinner is licensed to you under the terms found in the file LICENSE.md.
*/
#nullable enable
namespace Yarn.Unity.Editor
{
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using Yarn.Unity;
///
/// Adds a menu item to the menu bar for creating an instance of the
/// dialogue runner prefab.
///
public static class CreateDialogueRunnerMenuItem
{
const string DialogueRunnerPrefabGUID = "52571f68872914e24837210513edea1d";
///
/// Instantiates the Dialogue System prefab in the currently active scene,
/// and returns the created .
///
/// A newly created .
/// Thrown when the
/// Dialogue System prefab cannot be found in the Yarn Spinner
/// package.
[MenuItem("GameObject/Yarn Spinner/Dialogue System", priority = 11)]
public static DialogueRunner CreateDialogueRunner()
{
string assetPath = AssetDatabase.GUIDToAssetPath(DialogueRunnerPrefabGUID);
var prefabAsset = AssetDatabase.LoadAssetAtPath(assetPath);
if (prefabAsset == null)
{
throw new System.InvalidOperationException(
$"Can't create a new Dialogue System: Can't find the prefab to create a Dialogue System from."
);
}
var instantiatedPrefab = (GameObject)PrefabUtility.InstantiatePrefab(prefabAsset);
#if UNITY_2023_1_OR_NEWER
var eventSystems = Object.FindObjectsByType(FindObjectsSortMode.None);
#else
var eventSystems = Object.FindObjectsOfType();
#endif
if (eventSystems.Length > 1)
{
// At least one other event system is present in the scene. Turn off
// the one that came with the prefab - it's not needed.
var instantiatedEventSystem = instantiatedPrefab.GetComponentInChildren();
instantiatedEventSystem.gameObject.SetActive(false);
}
return instantiatedPrefab.GetComponent();
}
}
}