/*
Yarn Spinner is licensed to you under the terms found in the file LICENSE.md.
*/
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
#nullable enable
namespace Yarn.Unity.Editor
{
///
/// Contains utility methods for working with Yarn Spinner content in
/// the Unity Editor.
///
public static class YarnEditorUtility
{
// GUID for editor assets. (Doing it like this means that we don't
// have to worry about where the assets are on disk, if the user
// has moved Yarn Spinner around.)
const string DocumentIconTextureGUID = "0ed312066ea6f40f6af965f21c818b34";
const string ProjectIconTextureGUID = "f6a533d9225cd40ea9ded31d4f686e3b";
const string LocalizationIconTextureGUID = "2cbba4ddd142149b0a38697070990deb";
const string YarnScriptTemplateFileGUID = "4f4ca4a46020a454f80e2ac78eda5aa1";
const string DialoguePresenterTemplateFileGUID = "4a168359cda6140c0bddcd5955a326e4";
///
/// Returns a that can be used to represent
/// Yarn files.
///
/// A texture to use in the Unity editor for Yarn
/// files.
public static Texture2D GetYarnDocumentIconTexture()
{
string textureAssetPath = AssetDatabase.GUIDToAssetPath(DocumentIconTextureGUID);
return AssetDatabase.LoadAssetAtPath(textureAssetPath);
}
///
/// Returns a that can be used to represent
/// Yarn project files.
///
/// A texture to use in the Unity editor for Yarn project
/// files.
public static Texture2D GetYarnProjectIconTexture()
{
string textureAssetPath = AssetDatabase.GUIDToAssetPath(ProjectIconTextureGUID);
return AssetDatabase.LoadAssetAtPath(textureAssetPath);
}
///
/// Returns a that can be used to represent
/// built-in Localization objects.
///
/// A texture to use in the Unity editor for Yarn built-in
/// Localization files.
public static Texture2D GetLocalizationIconTexture()
{
string textureAssetPath = AssetDatabase.GUIDToAssetPath(LocalizationIconTextureGUID);
return AssetDatabase.LoadAssetAtPath(textureAssetPath);
}
///
/// Returns the path to a text file that can be used as the basis
/// for newly created Yarn scripts.
///
/// A path to a file to use in the Unity editor for
/// creating new Yarn scripts.
/// Thrown if the template
/// text file cannot be found.
public static string GetTemplateYarnScriptPath()
{
var path = AssetDatabase.GUIDToAssetPath(YarnScriptTemplateFileGUID);
if (string.IsNullOrEmpty(path))
{
throw new System.IO.FileNotFoundException($"Template file for new Yarn scripts couldn't be found. Have the .meta files for Yarn Spinner been modified or deleted? Try re-importing the Yarn Spinner package to fix this error.");
}
return path;
}
///
/// Returns the path to a text file that can be used as the basis
/// for newly created C# Dialogue Presenter scripts.
///
/// A path to a file to use in the Unity editor for
/// creating new C# Dialogue Presenter.
/// Thrown if the template
/// text file cannot be found.
public static string GetTemplateDialoguePresenterPath()
{
var path = AssetDatabase.GUIDToAssetPath(DialoguePresenterTemplateFileGUID);
if (string.IsNullOrEmpty(path))
{
throw new System.IO.FileNotFoundException($"Template file for Dialogue View scripts couldn't be found. Have the .meta files for Yarn Spinner been modified or deleted? Try re-importing the Yarn Spinner package to fix this error.");
}
return path;
}
///
/// Begins the interactive process of creating a new Yarn file in
/// the Editor.
///
[MenuItem("Assets/Create/Yarn Spinner/Yarn Script", false, 10)]
public static void CreateYarnAsset()
{
// This method call is undocumented, but public. It's defined
// in ProjectWindowUtil, and used by other parts of the editor
// to create other kinds of assets (scripts, textures, etc).
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
default,
ScriptableObject.CreateInstance(),
"NewYarnScript.yarn",
GetYarnDocumentIconTexture(),
GetTemplateYarnScriptPath());
}
///
/// Creates a new Yarn Project asset in the current folder, and begins
/// interactively renaming it.
///
[MenuItem("Assets/Create/Yarn Spinner/Yarn Project", false, 101)]
public static void CreateYarnProject()
{
// This method call is undocumented, but public. It's defined
// in ProjectWindowUtil, and used by other parts of the editor
// to create other kinds of assets (scripts, textures, etc).
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
default,
ScriptableObject.CreateInstance(),
"NewProject.yarnproject",
GetYarnProjectIconTexture(),
GetTemplateYarnScriptPath());
}
///
/// Creates a new C# script asset containing a template Dialogue Presenter in
/// the current folder, and begins interactively renaming it.
///
[MenuItem("Assets/Create/Yarn Spinner/Dialogue Presenter Script", false, 111)]
[MenuItem("Assets/Create/Scripting/Yarn Spinner/Dialogue Presenter Script", false, 101)]
public static void CreateDialoguePresenterScript()
{
// This method call is undocumented, but public. It's defined
// in ProjectWindowUtil, and used by other parts of the editor
// to create other kinds of assets (scripts, textures, etc).
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
default,
ScriptableObject.CreateInstance(),
"NewDialoguePresenter.cs",
null,
GetTemplateDialoguePresenterPath());
}
///
/// Writes a Yarn Project to .
///
/// The path at which to write the file.
/// The Yarn Project to write to disk.
public static Object CreateYarnProject(string path, Compiler.Project project)
{
var text = project.GetJson();
File.WriteAllText(path, text);
AssetDatabase.ImportAsset(path);
return AssetDatabase.LoadAssetAtPath