Files
AR-Menu/Library/PackageCache/com.needle.engine-exporter@8c046140a1d9/Cloud/Editor/NeedleCloudAssetEditor.cs
2025-11-30 08:35:03 +02:00

129 lines
4.6 KiB
C#

using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using Needle.Cloud.Runtime;
using UnityEditor;
using UnityEngine;
namespace Needle.Cloud.Editor
{
[CustomEditor(typeof(NeedleCloudAsset))]
public class NeedleCloudAssetEditor : UnityEditor.Editor
{
[InitializeOnLoadMethod]
private static async void Init()
{
while (true)
{
await Task.Delay(3_000);
foreach (var inst in NeedleCloudAsset.instances)
{
if (inst.autoRefresh && inst.enabled)
{
inst.Load();
}
}
}
}
private SerializedProperty url;
private SerializedProperty password;
private void OnEnable()
{
url = serializedObject.FindProperty(nameof(NeedleCloudAsset.url));
password = serializedObject.FindProperty(nameof(NeedleCloudAsset.password));
var comp = target as NeedleCloudAsset;
if (comp)
{
comp.Load();
}
}
public override void OnInspectorGUI()
{
var comp = target as NeedleCloudAsset;
if (!comp)
{
return;
}
var hasUrl = !string.IsNullOrEmpty(this.url.stringValue) && this.url.stringValue.StartsWith("http");
{
EditorGUILayout.HelpBox($"[Early Access] Needle Cloud Remote Assets\nUse Needle Cloud hosted assets directly in your Unity and Needle Engine projects", MessageType.None);
}
using(var change = new EditorGUI.ChangeCheckScope())
{
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.PropertyField(url, new GUIContent("URL"));
using(new EditorGUI.DisabledScope(!hasUrl))
{
if (GUILayout.Button("View", GUILayout.Width(40)))
{
Application.OpenURL($"https://cloud.needle.tools/view?file={this.url.stringValue}");
}
}
}
if (!hasUrl)
{
EditorGUILayout.HelpBox("Enter a asset URL from Needle Cloud", MessageType.None);
}
else if(comp.isMissingPassword || !string.IsNullOrWhiteSpace(password.stringValue))
{
var modifierPressed = Event.current.modifiers.HasFlag(EventModifiers.Alt);
if (modifierPressed)
{
password.stringValue = EditorGUILayout.DelayedTextField(new GUIContent("Password"), password.stringValue);
}
else
{
// TODO: make this field delayed
var newPasswordValue = EditorGUILayout.PasswordField(new GUIContent("Password"), password.stringValue);
if (newPasswordValue != password.stringValue)
{
password.stringValue = newPasswordValue;
}
}
}
if (change.changed)
{
this.serializedObject.ApplyModifiedProperties();
}
}
if (comp)
{
if (comp.isMissingPassword)
{
EditorGUILayout.HelpBox("This asset is password protected. Please enter the password to access", MessageType.Error);
}
else
{
var localFileInfo = !string.IsNullOrWhiteSpace(comp.LocalPath) ? new FileInfo(comp.LocalPath) : null;
if (localFileInfo != null && localFileInfo.Exists && hasUrl)
{
var filename = localFileInfo.Name;
EditorGUILayout.LabelField("File Name", filename);
var sizeInMB = (localFileInfo.Length / 1024f / 1024f).ToString("N2", CultureInfo.InvariantCulture);
EditorGUILayout.LabelField("File Size", sizeInMB + " MB");
}
}
}
if (GUILayout.Button("Open Needle Cloud"))
{
Application.OpenURL("https://cloud.needle.tools");
}
}
}
}