79 lines
2.1 KiB
C#
79 lines
2.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using AnotherFileBrowser.Windows;
|
|
using BITKit;
|
|
using BITKit.IO;
|
|
using BITKit.UX;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.UIElements;
|
|
using Button = UnityEngine.UIElements.Button;
|
|
using Image = UnityEngine.UI.Image;
|
|
using Slider = UnityEngine.UIElements.Slider;
|
|
|
|
namespace BITFactory.Cutting.UX
|
|
{
|
|
public class UXReferenceImage : MonoBehaviour
|
|
{
|
|
[SerializeField] private Texture2D defaultTexture;
|
|
|
|
[SerializeField] private RawImage worldImage;
|
|
|
|
[UXBindPath("pick-image-button")] private Button pickImageButton;
|
|
[UXBindPath("reference-image")] private VisualElement referenceImage;
|
|
[UXBindPath("2d-opacity-slider")] private Slider opacitySlider;
|
|
[UXBindPath("3d-opacity-slider")] private Slider worldOpacitySlider;
|
|
|
|
private void Start()
|
|
{
|
|
UXUtils.Inject(this);
|
|
|
|
opacitySlider.RegisterValueChangedCallback(x => { referenceImage.SetOpacity(x.newValue); });
|
|
worldOpacitySlider.RegisterValueChangedCallback(x =>
|
|
{
|
|
worldImage.color = new Color(1, 1, 1, x.newValue);
|
|
});
|
|
|
|
referenceImage.SetOpacity(0);
|
|
worldImage.color = new Color(1, 1, 1, 0);
|
|
|
|
opacitySlider.SetValueWithoutNotify(0);
|
|
worldOpacitySlider.SetValueWithoutNotify(0);
|
|
|
|
if (defaultTexture)
|
|
{
|
|
worldImage.texture = defaultTexture;
|
|
referenceImage.style.backgroundImage = defaultTexture;
|
|
}
|
|
|
|
pickImageButton.clicked += PickImage;
|
|
}
|
|
|
|
private void PickImage()
|
|
{
|
|
new Thread(() =>
|
|
{
|
|
new FileBrowser().OpenFileBrowser(new BrowserProperties(),PickImage);
|
|
}).Start();
|
|
}
|
|
|
|
private async void PickImage(string path)
|
|
{
|
|
await UniTask.SwitchToMainThread();
|
|
if (destroyCancellationToken.IsCancellationRequested) return;
|
|
var texture = new Texture2D(2, 2);
|
|
if (texture.LoadImage(await System.IO.File.ReadAllBytesAsync(path)) is false)
|
|
{
|
|
Alert.Print("无法加载图片", "请检查选择的文件是否为图片");
|
|
return;
|
|
}
|
|
worldImage.texture = texture;
|
|
referenceImage.style.backgroundImage = texture;
|
|
}
|
|
}
|
|
}
|
|
|