Skip to content

Commit b3f0149

Browse files
committed
Update thumb generator.
1 parent 1767a1d commit b3f0149

1 file changed

Lines changed: 37 additions & 7 deletions

File tree

Project~/Assets/Runtime/ThumbGenerator.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Linq;
2222
using UnityEditor;
2323
using UnityEngine;
24+
using UnityEngine.SceneManagement;
2425
using VisualPinball.Unity.Editor;
2526

2627
namespace VisualPinball.Unity.Library
@@ -30,25 +31,32 @@ public class ThumbGenerator : MonoBehaviour
3031
[SerializeReference]
3132
public Editor.AssetLibrary AssetLibrary;
3233

34+
[SerializeReference]
35+
public GameObject DefaultEnvironment;
36+
3337
public bool IsProcessing { get; private set; }
3438
public int TotalProcessing { get; private set; }
3539
public int NumProcessed { get; private set; }
3640

41+
3742
private List<AssetMaterialCombination> _assets;
3843
private GameObject _currentGo;
3944
private ThumbGeneratorComponent _currentTbc;
4045
private AssetMaterialCombination _currentAmc;
4146
private Camera _camera;
42-
43-
private PlayfieldComponent _pf;
44-
private Vector3 _tableCenter;
47+
private readonly Dictionary<string, GameObject> _environmentObjects = new();
4548

4649
public void StartProcessing(bool newOnly = false, bool selectedOnly = false)
4750
{
4851
_camera = Camera.main;
49-
50-
_pf = GetComponentInChildren<PlayfieldComponent>();
51-
_tableCenter = new Vector3(_pf.Width / 2f, _pf.Height / 2f,0);
52+
53+
var bgParent = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(go => go.name == "_BackgroundObjects");
54+
_environmentObjects.Clear();
55+
if (bgParent != null) {
56+
foreach (var mr in bgParent.GetComponentsInChildren<MeshRenderer>(true)) {
57+
_environmentObjects[mr.name] = mr.gameObject;
58+
}
59+
}
5260

5361
var category = AssetLibrary.GetCategories().FirstOrDefault(c => c.Name.Contains("Flipper"));
5462
//var category = AssetLibrary.GetCategories().FirstOrDefault(c => c.Name.Contains("Flipper"));
@@ -99,25 +107,47 @@ public void StopProcessing()
99107

100108
private void Process(AssetMaterialCombination a)
101109
{
110+
// camera preset
102111
_currentAmc = a;
103112
if (a.Asset.ThumbCameraPreset != null) {
104113
a.Asset.ThumbCameraPreset.ApplyTo(_camera.transform);
105114
} else {
106115
AssetLibrary.DefaultThumbCameraPreset.ApplyTo(_camera.transform);
107116
}
108-
117+
118+
// background object
119+
if (a.Asset.EnvironmentGameObjectName != null && _environmentObjects.ContainsKey(a.Asset.EnvironmentGameObjectName)) {
120+
ToggleEnvironment(_environmentObjects[a.Asset.EnvironmentGameObjectName]);
121+
} else {
122+
ToggleEnvironment(DefaultEnvironment);
123+
}
124+
125+
// instantiate prefab
109126
_currentGo = PrefabUtility.InstantiatePrefab(a.Asset.Object) as GameObject;
110127

128+
// apply position and material
111129
a.ApplyObjectPos(_currentGo);
112130
a.ApplyMaterial(_currentGo);
113131

132+
// launch generation
114133
Debug.Log($"Processing {_currentGo!.name}");
115134
_currentTbc = _currentGo!.AddComponent<ThumbGeneratorComponent>();
116135
_currentTbc!.ThumbnailGuid = a.ThumbId;
117136
_currentTbc!.Prefab = a.Asset.Object;
118137
_currentTbc!.OnScreenshot += DoneProcessing;
119138
}
120139

140+
private void ToggleEnvironment(GameObject go)
141+
{
142+
if (go.activeInHierarchy) {
143+
return;
144+
}
145+
foreach (var bgo in _environmentObjects.Values) {
146+
bgo.SetActive(false);
147+
}
148+
go.SetActive(true);
149+
}
150+
121151
private void DoneProcessing(object sender, EventArgs e)
122152
{
123153
_currentTbc!.OnScreenshot -= DoneProcessing;

0 commit comments

Comments
 (0)