Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

com.rest.blockadelabs 1.0.0-preview.1 #1

Merged
merged 18 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ _ReSharper.Caches
artifacts/
StreamingAssets/
StreamingAssets.meta

# ====================== #
# Project Specific Links #
# ====================== #
BlockadeLabs/Assets/Resources
BlockadeLabs/Assets/Resources.meta
BlockadeLabs/Assets/Plugins
BlockadeLabs/Assets/Plugins.meta
8 changes: 8 additions & 0 deletions BlockadeLabs/Assets/Demo.meta

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

20 changes: 20 additions & 0 deletions BlockadeLabs/Assets/Demo/BlockadeLabs.Demo.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "BlockadeLabs.Demo",
"rootNamespace": "BlockadeLabs.Demo",
"references": [
"GUID:0e64303a4898a4d4ba94306e88e6c025",
"GUID:a6609af893242c7438d701ddd4cce46a",
"GUID:6ab1da3c58a70364e92dc36aaec78f43",
"GUID:7958db66189566541a6363568aee1575",
"GUID:6055be8ebefd69e48b49212b09b47b2f"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions BlockadeLabs/Assets/Demo/BlockadeLabs.Demo.asmdef.meta

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

117 changes: 117 additions & 0 deletions BlockadeLabs/Assets/Demo/SkyboxDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using BlockadeLabs.Skyboxes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Utilities.Extensions;

namespace BlockadeLabs.Demo
{
public class SkyboxDemo : MonoBehaviour
{
[SerializeField]
private TMP_InputField promptInputField;

[SerializeField]
private TMP_Dropdown skyboxStyleDropdown;

[SerializeField]
private Button generateButton;

[SerializeField]
private Material skyboxMaterial;

private BlockadeLabsClient api;

private CancellationTokenSource lifetimeCancellationTokenSource;

private IReadOnlyList<SkyboxStyle> skyboxOptions;

private void OnValidate()
{
promptInputField.Validate();
skyboxStyleDropdown.Validate();
generateButton.Validate();
}

private void Awake()
{
OnValidate();
lifetimeCancellationTokenSource = new CancellationTokenSource();

try
{
api = new BlockadeLabsClient();
}
catch (Exception e)
{
Debug.LogError($"Failed to create {nameof(BlockadeLabsClient)}!\n{e}");
enabled = false;
return;
}

GetSkyboxStyles();
generateButton.onClick.AddListener(GenerateSkybox);
promptInputField.onSubmit.AddListener(GenerateSkybox);
promptInputField.onValueChanged.AddListener(ValidateInput);
}

private void ValidateInput(string input)
{
generateButton.interactable = !string.IsNullOrWhiteSpace(input);
}

private void GenerateSkybox() => GenerateSkybox(promptInputField.text);

private async void GenerateSkybox(string prompt)
{
if (string.IsNullOrWhiteSpace(prompt))
{
Debug.LogWarning("No prompt given!");
return;
}

try
{
generateButton.interactable = false;
promptInputField.interactable = false;
var request = new SkyboxRequest(prompt, skyboxStyleId: skyboxOptions[skyboxStyleDropdown.value].Id);
var skyboxInfo = await api.SkyboxEndpoint.GenerateSkyboxAsync(request, lifetimeCancellationTokenSource.Token).ConfigureAwait(true);
skyboxMaterial.mainTexture = skyboxInfo.MainTexture;
Debug.Log($"Successfully created skybox: {skyboxInfo.Id}");
}
catch (Exception e)
{
Debug.LogError(e);
}
finally
{
generateButton.interactable = true;
promptInputField.interactable = true;
}
}

private void OnDestroy()
{
lifetimeCancellationTokenSource.Cancel();
}

private async void GetSkyboxStyles()
{
try
{
skyboxOptions = await api.SkyboxEndpoint.GetSkyboxStylesAsync(lifetimeCancellationTokenSource.Token).ConfigureAwait(true);
var dropdownOptions = new List<TMP_Dropdown.OptionData>(skyboxOptions.Count);
dropdownOptions.AddRange(skyboxOptions.Select(skyboxStyle => new TMP_Dropdown.OptionData(skyboxStyle.Name)));
skyboxStyleDropdown.options = dropdownOptions;
}
catch (Exception e)
{
Debug.LogError(e);
}
}
}
}
11 changes: 11 additions & 0 deletions BlockadeLabs/Assets/Demo/SkyboxDemo.cs.meta

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

Loading