From 93410270e85af72e5458e2f9aec798d992d76cce Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Sat, 15 Jul 2023 17:30:32 -0700 Subject: [PATCH] com.rest.blockadelabs 1.0.2 (#9) - enable skybox style dropdown selector via property drawer --- .../Editor/SkyboxStylePropertyDrawer.cs | 124 ++++++++++++++++++ .../Editor/SkyboxStylePropertyDrawer.cs.meta | 11 ++ .../Runtime/Skyboxes/SkyboxStyle.cs | 22 +++- .../com.rest.blockadelabs/package.json | 2 +- 4 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 BlockadeLabs/Packages/com.rest.blockadelabs/Editor/SkyboxStylePropertyDrawer.cs create mode 100644 BlockadeLabs/Packages/com.rest.blockadelabs/Editor/SkyboxStylePropertyDrawer.cs.meta diff --git a/BlockadeLabs/Packages/com.rest.blockadelabs/Editor/SkyboxStylePropertyDrawer.cs b/BlockadeLabs/Packages/com.rest.blockadelabs/Editor/SkyboxStylePropertyDrawer.cs new file mode 100644 index 0000000..931ec74 --- /dev/null +++ b/BlockadeLabs/Packages/com.rest.blockadelabs/Editor/SkyboxStylePropertyDrawer.cs @@ -0,0 +1,124 @@ +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using BlockadeLabs.Skyboxes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Authentication; +using UnityEditor; +using UnityEngine; + +namespace BlockadeLabs.Editor +{ + [CustomPropertyDrawer(typeof(SkyboxStyle))] + public class SkyboxStylePropertyDrawer : PropertyDrawer + { + private static BlockadeLabsClient blockadeLabsClient; + + private static BlockadeLabsClient BlockadeLabsClient => blockadeLabsClient ??= new BlockadeLabsClient(); + + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + try + { + if (!BlockadeLabsClient.HasValidAuthentication) + { + EditorGUI.LabelField(position, "Cannot fetch skybox styles"); + return; + } + } + catch (AuthenticationException) + { + EditorGUI.HelpBox(position, "Check blockade labs api key", MessageType.Error); + + return; + } + catch (Exception e) + { + Debug.LogError(e); + + return; + } + + var id = property.FindPropertyRelative("id"); + var name = property.FindPropertyRelative("name"); + + if (options.Length < 1) + { + FetchStyles(); + + if (string.IsNullOrWhiteSpace(name.stringValue)) + { + EditorGUI.HelpBox(position, "Fetching skybox styles...", MessageType.Info); + return; + } + + EditorGUI.LabelField(position, label, new GUIContent(name.stringValue, id.intValue.ToString())); + return; + } + + // dropdown + var index = -1; + dynamic currentVoiceOption = null; + + if (id.intValue > 0) + { + currentVoiceOption = styles?.FirstOrDefault(style => style.Id.ToString() == id.intValue.ToString()); + } + + if (currentVoiceOption != null) + { + for (var i = 0; i < options.Length; i++) + { + if (options[i].tooltip.Contains(currentVoiceOption.Id.ToString())) + { + index = i; + break; + } + } + } + + EditorGUI.BeginChangeCheck(); + index = EditorGUI.Popup(position, label, index, options); + + if (EditorGUI.EndChangeCheck()) + { + currentVoiceOption = styles?.FirstOrDefault(style => options[index].text.Contains(style.Name)); + id.intValue = currentVoiceOption!.Id; + name.stringValue = currentVoiceOption!.Name; + } + } + + private static bool isFetchingStyles; + + public static bool IsFetchingStyles => isFetchingStyles; + + private static IReadOnlyList styles = new List(); + + public static IReadOnlyList Styles => styles; + + private static GUIContent[] options = Array.Empty(); + + public static IReadOnlyList Options => options; + + public static async void FetchStyles() + { + if (isFetchingStyles) { return; } + isFetchingStyles = true; + + try + { + styles = await BlockadeLabsClient.SkyboxEndpoint.GetSkyboxStylesAsync(); + options = styles.OrderBy(style => style.Id).Select(style => new GUIContent(style.Name, style.Id.ToString())).ToArray(); + } + catch (Exception e) + { + Debug.LogError(e); + } + finally + { + isFetchingStyles = false; + } + } + } +} diff --git a/BlockadeLabs/Packages/com.rest.blockadelabs/Editor/SkyboxStylePropertyDrawer.cs.meta b/BlockadeLabs/Packages/com.rest.blockadelabs/Editor/SkyboxStylePropertyDrawer.cs.meta new file mode 100644 index 0000000..a30368a --- /dev/null +++ b/BlockadeLabs/Packages/com.rest.blockadelabs/Editor/SkyboxStylePropertyDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6e69be67fffe9a0459a91b14349d87ed +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {fileID: 2800000, guid: c0cdfd8ee0b46544aa66ab20cde3ddd1, type: 3} + userData: + assetBundleName: + assetBundleVariant: diff --git a/BlockadeLabs/Packages/com.rest.blockadelabs/Runtime/Skyboxes/SkyboxStyle.cs b/BlockadeLabs/Packages/com.rest.blockadelabs/Runtime/Skyboxes/SkyboxStyle.cs index 75b444c..c391711 100644 --- a/BlockadeLabs/Packages/com.rest.blockadelabs/Runtime/Skyboxes/SkyboxStyle.cs +++ b/BlockadeLabs/Packages/com.rest.blockadelabs/Runtime/Skyboxes/SkyboxStyle.cs @@ -1,9 +1,12 @@ // Licensed under the MIT License. See LICENSE in the project root for license information. using Newtonsoft.Json; +using System; +using UnityEngine; namespace BlockadeLabs.Skyboxes { + [Serializable] public sealed class SkyboxStyle { [JsonConstructor] @@ -13,22 +16,27 @@ public sealed class SkyboxStyle [JsonProperty("max-char")] string maxChar, [JsonProperty("negative-text-max-char")] int negativeTextMaxChar, [JsonProperty("image")] string image, - [JsonProperty("sort_order")] int sortOrder - ) + [JsonProperty("sort_order")] int sortOrder) { - Id = id; - Name = name; + this.id = id; + this.name = name; MaxChar = maxChar; NegativeTextMaxChar = negativeTextMaxChar; Image = image; SortOrder = sortOrder; } - [JsonProperty("id")] - public int Id { get; } + [SerializeField] + private string name; [JsonProperty("name")] - public string Name { get; } + public string Name => name; + + [SerializeField] + private int id; + + [JsonProperty("id")] + public int Id => id; [JsonProperty("max-char")] public string MaxChar { get; } diff --git a/BlockadeLabs/Packages/com.rest.blockadelabs/package.json b/BlockadeLabs/Packages/com.rest.blockadelabs/package.json index 906f00d..eaf49ab 100644 --- a/BlockadeLabs/Packages/com.rest.blockadelabs/package.json +++ b/BlockadeLabs/Packages/com.rest.blockadelabs/package.json @@ -3,7 +3,7 @@ "displayName": "BlockadeLabs", "description": "A Non-Official Blockade Labs Rest Client for Unity (UPM)", "keywords": [], - "version": "1.0.1", + "version": "1.0.2", "unity": "2021.3", "documentationUrl": "https://github.com/RageAgainstThePixel/com.rest.blockadelabs#documentation", "changelogUrl": "https://github.com/RageAgainstThePixel/com.rest.blockadelabs/releases",