Files

77 lines
3.8 KiB
C#

using System;
using System.IO;
using HotUpdate.Gameplay;
using Newtonsoft.Json;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using YooAsset.Editor;
public static class BuildMainMenuMusic
{
public const string PrefabPath = "Assets/Bundles/Sound/Prefab/MainMenuMusic.prefab";
const string ClipPath = "Assets/Bundles/Sound/BGM/MeadowBreeze_MainMenu_Loop.ogg";
public static string Build()
{
if (EditorApplication.isPlaying) throw new Exception("Build music in Edit Mode");
var clip = AssetDatabase.LoadAssetAtPath<AudioClip>(ClipPath);
if (clip == null) throw new Exception("Selected BGM missing");
if (!AssetDatabase.IsValidFolder("Assets/Bundles/Sound/Prefab"))
AssetDatabase.CreateFolder("Assets/Bundles/Sound", "Prefab");
var original = SceneManager.GetActiveScene();
var temporary = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
try
{
SceneManager.SetActiveScene(temporary);
var root = new GameObject(MainMenuMusic.PrefabName);
var source = root.AddComponent<AudioSource>();
source.clip = clip;
source.playOnAwake = false;
source.loop = true;
source.spatialBlend = 0;
source.volume = 1;
var player = root.AddComponent<MainMenuMusic>();
var serialized = new SerializedObject(player);
serialized.FindProperty("_source").objectReferenceValue = source;
serialized.ApplyModifiedPropertiesWithoutUndo();
PrefabUtility.SaveAsPrefabAsset(root, PrefabPath);
}
finally
{
SceneManager.SetActiveScene(original);
EditorSceneManager.CloseScene(temporary, true);
}
var package = BundleCollectorSettingData.Setting.Packages.Find(p => p.PackageName == "Art");
var group = package?.Groups.Find(g => g.GroupName == "Sound");
if (group == null) throw new Exception("Existing Art/Sound group missing");
const string folder = "Assets/Bundles/Sound/Prefab";
if (!group.Collectors.Exists(c => c.CollectPath == folder))
BundleCollectorSettingData.CreateCollector(group, new BundleCollector {
CollectPath = folder, CollectorGUID = AssetDatabase.AssetPathToGUID(folder),
CollectorType = ECollectorType.MainAssetCollector,
AddressRuleName = nameof(AddressByFileName), PackRuleName = nameof(PackSeparately),
FilterRuleName = nameof(CollectAll), AssetTags = "art"
});
package.CheckConfigError();
BundleCollectorSettingData.SaveFile();
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
var audio = prefab.GetComponent<AudioSource>();
if (prefab.GetComponent<MainMenuMusic>()?.Source != audio || audio.clip != clip ||
!audio.loop || audio.playOnAwake || audio.spatialBlend != 0 || audio.volume != 1)
throw new Exception("Music prefab configuration invalid");
string report = JsonConvert.SerializeObject(new {
selected = ClipPath, prefab = PrefabPath, address = MainMenuMusic.PrefabName,
package = "Art", group = "Sound", loop = audio.loop, volume = audio.volume,
seconds = clip.length, clip.channels, clip.frequency,
independentMusicToggle = true, runtimePlaybackWired = true,
bundleBuilt = false, verifiedUtc = DateTime.UtcNow.ToString("o")
}, Formatting.Indented);
string repo = Directory.GetParent(Application.dataPath).Parent.FullName;
File.WriteAllText(Path.Combine(repo, "Doc/Design/Sound/MainMenuSoft/playback_assets.json"), report);
return report;
}
}