// SPDX-FileCopyrightText: 2023 Unity Technologies and the glTFast authors
// SPDX-License-Identifier: Apache-2.0
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
namespace GLTFast
{
using Loading;
using Logging;
using Materials;
///
/// Base component for code-less loading of glTF files
///
public class GltfAsset : GltfAssetBase
{
///
/// URL to load the glTF from
/// Loading local file paths works by prefixing them with "file://"
///
public string Url
{
get => url;
set => url = value;
}
///
/// Automatically load at start
///
public bool LoadOnStartup
{
get => loadOnStartup;
set => loadOnStartup = value;
}
///
/// Scene to load (-1 loads glTFs default scene)
///
protected int SceneId => sceneId;
///
/// If true, the first animation clip starts playing right after instantiation.
///
public bool PlayAutomatically => playAutomatically;
///
/// If true, url is treated as relative StreamingAssets path
///
public bool StreamingAsset
{
get => streamingAsset;
set => streamingAsset = value;
}
///
public InstantiationSettings InstantiationSettings
{
get => instantiationSettings;
set => instantiationSettings = value;
}
[SerializeField]
[Tooltip("URL to load the glTF from. Loading local file paths works by prefixing them with \"file://\"")]
string url;
[SerializeField]
[Tooltip("Automatically load at start.")]
bool loadOnStartup = true;
[SerializeField]
[Tooltip("Override scene to load (-1 loads glTFs default scene)")]
int sceneId = -1;
[SerializeField]
[Tooltip("If true, the first animation clip starts playing right after instantiation")]
bool playAutomatically = true;
[SerializeField]
[Tooltip("If checked, url is treated as relative StreamingAssets path.")]
bool streamingAsset;
[SerializeField]
InstantiationSettings instantiationSettings;
///
/// Latest scene's instance.
///
public GameObjectSceneInstance SceneInstance { get; protected set; }
///
/// Final URL, considering all options (like )
///
// ReSharper disable once MemberCanBePrivate.Global
public string FullUrl => streamingAsset
? Path.Combine(Application.streamingAssetsPath, url)
: url;
///
/// Called at initialization phase
///
protected virtual async void Start()
{
if (loadOnStartup && !string.IsNullOrEmpty(url))
{
// Automatic load on startup
await Load(FullUrl);
}
}
///
public override async Task Load(
string gltfUrl,
IDownloadProvider downloadProvider = null,
IDeferAgent deferAgent = null,
IMaterialGenerator materialGenerator = null,
ICodeLogger logger = null
)
{
logger = logger ?? new ConsoleLogger();
var success = await base.Load(gltfUrl, downloadProvider, deferAgent, materialGenerator, logger);
if (success)
{
if (deferAgent != null) await deferAgent.BreakPoint();
// Auto-Instantiate
if (sceneId >= 0)
{
success = await InstantiateScene(sceneId, logger);
}
else
{
success = await Instantiate(logger);
}
}
return success;
}
///
protected override IInstantiator GetDefaultInstantiator(ICodeLogger logger)
{
return new GameObjectInstantiator(Importer, transform, logger, instantiationSettings);
}
///
protected override void PostInstantiation(IInstantiator instantiator, bool success)
{
SceneInstance = (instantiator as GameObjectInstantiator)?.SceneInstance;
#if UNITY_ANIMATION
if (playAutomatically)
SceneInstance?.LegacyAnimation?.Play();
#endif
base.PostInstantiation(instantiator, success);
}
///
public override void ClearScenes()
{
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
#if UNITY_ANIMATION
if (SceneInstance?.LegacyAnimation != null)
{
Destroy(SceneInstance.LegacyAnimation);
}
#endif
SceneInstance = null;
}
}
}