Files
sbox-public/game/addons/tools/Code/Assets/PreviewPrefab.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

55 lines
1.4 KiB
C#

namespace Editor.Assets;
[AssetPreview( "prefab" )]
class PreviewPrefab : AssetPreview
{
public override float PreviewWidgetCycleSpeed => 0.2f;
public override bool UsePixelEvaluatorForThumbs => true;
public PreviewPrefab( Asset asset ) : base( asset )
{
}
/// <summary>
/// Create the model or whatever needs to be viewed
/// </summary>
public override Task InitializeAsset()
{
using ( EditorUtility.DisableTextureStreaming() )
{
var pf = Asset.LoadResource<PrefabFile>();
if ( pf is null ) return Task.CompletedTask;
using ( Scene.Push() )
{
PrimaryObject = GameObject.Clone( pf );
PrimaryObject.WorldPosition = 0;
SceneCenter = PrimaryObject.GetBounds().Center;
SceneSize = PrimaryObject.GetBounds().Size;
TryAddDefaultLighting( Scene );
}
}
return Task.CompletedTask;
}
static void TryAddDefaultLighting( Scene scene )
{
if ( scene.Components.GetInDescendantsOrSelf<DirectionalLight>( true ).IsValid() ) return;
if ( scene.Components.GetInDescendantsOrSelf<SpotLight>( true ).IsValid() ) return;
if ( scene.Components.GetInDescendantsOrSelf<PointLight>( true ).IsValid() ) return;
var go = scene.CreateObject();
go.Name = "Directional Light";
go.WorldRotation = Rotation.From( 90, 0, 0 );
var light = go.Components.Create<DirectionalLight>();
light.LightColor = Color.White;
scene.SceneWorld.AmbientLightColor = "#557685";
}
}