Unity鼠标点击按钮打开本地文件

1.新建一个OpenUrl.cs脚本,并添加给Open按钮

using UnityEngine;
using System.IO;
using UnityEngine.UI;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class OpenUrl : MonoBehaviour
{
    private Button Open;
    // Use this for initialization
    void Start()
    {
        //鼠标点击按钮事件
        Open = GameObject.Find("Open").GetComponent<Button>();
        Open.onClick.AddListener(OnClick);
    }

    // Update is called once per frame
    void Update()
    {

    }
    
    //读取文件事件
    public static bool ShowInExplorer(string itemPath)
    {
        bool result = false;

#if !UNITY_WEBPLAYER
        itemPath = Path.GetFullPath(itemPath.Replace(@"/", @"\"));  
        if (File.Exists(itemPath))
        {
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
            
            Process.Start("explorer.exe", "/select," + itemPath);
#endif
            result = true;
        }
        else if (Directory.Exists(itemPath))
        {
            
            UnityEngine.Application.OpenURL(itemPath);
            result = true;
        }

#endif

        return result;
    }

    void OnClick()
    {
        ShowInExplorer("F:/2018");
    }
}

2.运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41211080/article/details/82860531