/// <summary> /// Assign a .yarn <see cref="TextAsset"/> file <paramref /// name="newSourceScript"/> to the <see /// cref="YarnProjectImporter"/> <paramref /// name="projectImporter"/>. /// </summary> /// <remarks>If <paramref name="projectImporter"/> is <see /// langword="null"/>, this script will be removed from its current /// project (if any) and not added to another.</remarks> /// <param name="newSourceScript">The script that should be /// assigned to the Yarn Project.</param> /// <param name="scriptImporter">The importer for <paramref /// name="newSourceScript"/>.</param> /// <param name="projectImporter">The importer for the project that /// newSourceScript should be made a part of, or null.</param> internal static void AssignScriptToProject(TextAsset newSourceScript, YarnImporter scriptImporter, YarnProjectImporter projectImporter) { if (scriptImporter.DestinationProject != null) { var existingProjectImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(scriptImporter.DestinationProject)) as YarnProjectImporter; existingProjectImporter.sourceScripts.Remove(newSourceScript); EditorUtility.SetDirty(existingProjectImporter); existingProjectImporter.SaveAndReimport(); } if (projectImporter != null) { projectImporter.sourceScripts.Add(newSourceScript); EditorUtility.SetDirty(projectImporter); // Reimport the program to make it generate its default string // table, if needed projectImporter.SaveAndReimport(); } }
/// <summary> /// Creates a new .yarnproject asset in the same directory as the /// Yarn script represented by serializedObject, and configures the /// script's importer to use the new Yarn Project. /// </summary> /// <param name="serializedObject">A serialized object that /// represents a <see cref="YarnImporter"/>.</param> /// <returns>The path to the created asset.</returns> internal static string CreateYarnProject(YarnImporter initialSourceAsset) { // Figure out where on disk this asset is var path = initialSourceAsset.assetPath; var directory = Path.GetDirectoryName(path); // Figure out a new, unique path for the localization we're // creating var databaseFileName = $"Project.yarnproject"; var destinationPath = Path.Combine(directory, databaseFileName); destinationPath = AssetDatabase.GenerateUniqueAssetPath(destinationPath); // Create the program YarnEditorUtility.CreateYarnAsset(destinationPath); AssetDatabase.ImportAsset(destinationPath); AssetDatabase.SaveAssets(); AssignScriptToProject(path, destinationPath); return(destinationPath); }