Skip to content

Commit

Permalink
Custom Background Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
NyxTheShield committed May 11, 2023
1 parent bd04472 commit 4ea9b28
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
88 changes: 88 additions & 0 deletions Assets/BackgroundManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

using UnityEngine;
using UnityEngine.Video;

#if UNITY_EDITOR
using UnityEditor;
#endif
public class BackgroundManager : MonoBehaviour {

public Camera mainCamera;
public RenderTexture bgTexture;

// Start is called before the first frame update
void Start() {
bgTexture = new RenderTexture(Screen.currentResolution.width, Screen.currentResolution.height, 16, RenderTextureFormat.ARGB32);
bgTexture.Create();
mainCamera.targetTexture = bgTexture;
}

private void OnDestroy() {
bgTexture.Release();

}

GameObject tromboneBackground;

#if UNITY_EDITOR
[ContextMenu("Export Background")]
public void ExportBackground() {
tromboneBackground = gameObject;
string path = EditorUtility.SaveFilePanel("Save Trombone Background", string.Empty, "bg",
"yarground");

BuildTargetGroup selectedBuildTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
BuildTarget activeBuildTarget = EditorUserBuildSettings.activeBuildTarget;

GameObject clonedTromboneBackground = null;

try {
if (!string.IsNullOrEmpty(path)) {
clonedTromboneBackground = Instantiate(tromboneBackground.gameObject);

string fileName = Path.GetFileName(path);
string folderPath = Path.GetDirectoryName(path);

// serialize tromboners (this one is not unity's fault, it's base game weirdness)
List<string> trombonePaths = new List<string>() { "Assets/_Background.prefab" };


PrefabUtility.SaveAsPrefabAsset(clonedTromboneBackground.gameObject, "Assets/_Background.prefab");
AssetBundleBuild assetBundleBuild = default;
assetBundleBuild.assetBundleName = fileName;
assetBundleBuild.assetNames = trombonePaths.ToArray();

BuildPipeline.BuildAssetBundles(Application.temporaryCachePath,
new AssetBundleBuild[] { assetBundleBuild }, BuildAssetBundleOptions.ForceRebuildAssetBundle,
EditorUserBuildSettings.activeBuildTarget);
EditorPrefs.SetString("currentBuildingAssetBundlePath", folderPath);
EditorUserBuildSettings.SwitchActiveBuildTarget(selectedBuildTargetGroup, activeBuildTarget);

foreach (var asset in trombonePaths) {
AssetDatabase.DeleteAsset(asset);
}

if (File.Exists(path)) File.Delete(path);

// Unity seems to save the file in lower case, which is a problem on Linux, as file systems are case sensitive there
File.Move(Path.Combine(Application.temporaryCachePath, fileName.ToLowerInvariant()), path);

AssetDatabase.Refresh();

EditorUtility.DisplayDialog("Export Successful!", "Export Successful!", "OK");

if (clonedTromboneBackground != null) DestroyImmediate(clonedTromboneBackground);
}
}
catch {
if (clonedTromboneBackground != null) DestroyImmediate(clonedTromboneBackground);
}

}

#endif
}
11 changes: 11 additions & 0 deletions Assets/BackgroundManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion Assets/Script/PlayMode/Play.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,24 @@ private void StartSong() {
i++;
}

SongStarted = true;
//Load Background
string backgroundPath = Path.Combine(song.Location, "bg.yarground");
if (File.Exists(backgroundPath)) {

var bundle = AssetBundle.LoadFromFile(backgroundPath);
var bg = bundle.LoadAsset<GameObject>("assets/_background.prefab");
Instantiate(bg);
Debug.Log("Loaded Custom Background!");
OnSongEnd += (SongEntry song) => { bundle.Unload(true);};
}
else {
Debug.LogWarning($"There is no background file in {backgroundPath}!");
}

SongStarted = true;



// Hide loading screen
GameUI.Instance.loadingContainer.SetActive(false);

Expand Down

0 comments on commit 4ea9b28

Please sign in to comment.