首页 > 解决方案 > Unity3d(Editor):使用 EditorUtility.OpenFilePanel() 打开多个文件

问题描述

我正在观看本教程并完成了它,现在我想扩展它。

我想统一使用 FileExplorer 打开多个图像,然后能够根据滑块的值显示图像,如下图所示:

图片

任何帮助表示赞赏。

标签: c#unity3dunity-editor

解决方案


这并没有回答问题,而是您在评论中提出的问题

你能给我一些代码吗“只需创建一个列表并在其中包含打开的图像,当滑块的值发生变化时,只需使用 int 将原始图像的精灵设置为列表中图像的精灵来获取列表中的图像。”?

[RequireComponent(typeof(RawImage))]
public class ImageSwitcher : MonoBehaviour
{
    private RawImage image;

    public Slider SliderComponent;

    // Get the textures somehow
    public List<Texture>() textures = new List<Texture>();

    private void Awake()
    {
        image = GetComponent<RawImage>();

        if(!SliderComponent)
        {
            Debug.LogError("No SliderComponent referenced!", this);
            return;
        }

        // Make the slider accept only whole numbers
        SliderComponent.wholeNumbers = true;

        SliderComponent.value = 0;

        SliderComponent.minValue = 0;

        // Index is 0 based so can maximal be list count -1
        SliderComponent.maxValue = textures.Count - 1;

        // Register a listener for onValueChanged
        // Remove the listener first to avoid multiple listeners added
        SliderComponent.onValueChanged.RemoveListener(OnSliderChanged);
        SliderComponent.onValueChanged.AddListener(OnSliderChanged);
    }

    private void OnDestroy ()
    {
        // Always clean up listeners when not needed anymore
        SliderComponent.onValueChanged.RemoveListener(OnSliderChanged);
    }

    // Use this to change the Texture list
    // and Max value of the slider afterwards
    public void UpdateSlider(List<Texture> textures)
    {
        // Update the texture list
        this.textures = textures;

        // Update the max value of the slider
        SliderComponent.maxValue = textures.Count - 1;

        // Unity might automatically clamp the slider value
        // after the maxValue was changed
        // But just to be sure we can do it as well
        SliderComponent.value = Mathf.Clamp(SliderComponent.value, 0, textures.Count - 1);
    }


    // Called when the slider value is changed
    private void OnSliderChanged()
    {
        // Get the value as int
        int index = Mathf.RoundToInt(SliderComponent.value);

        if(index < 0 || index > textures.Count - 1)
        {
            // Should actually be impossible but just in case log it
            Debug.Log("Slider produced impossible index: " + index, this);
            return;
        }

        // Get according texture from list
        var texture = textures[index];

        // Set texture
        image.texture = texture;
    }
}

但是,这并不能完全解决您的问题

Unity3d(Editor):使用 EditorUtility.OpenFilePanel() 打开多个文件

该线程中所述,这是不可能的,因为EditorUtility.OpenFilePanel仅返回一个文件路径为string.

所以简短的回答是:(目前)不可能

有一个为多项选择添加该功能的公开投票,因此您可能想在那里投票。

我的一个想法是尝试选择一个文件夹路径并从该文件夹加载所有纹理,但这只是一种解决方法,并不是您真正要求的。

但我希望其余的对你有所帮助:)


推荐阅读