admin 管理员组

文章数量: 887006

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 initializationvoid Start(){//鼠标点击按钮事件Open = GameObject.Find("Open").GetComponent<Button>();Open.onClick.AddListener(OnClick);}// Update is called once per framevoid Update(){}//读取文件事件public static bool ShowInExplorer(string itemPath){bool result = false;#if !UNITY_WEBPLAYERitemPath = Path.GetFullPath(itemPath.Replace(@"/", @"\"));  if (File.Exists(itemPath)){
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WINProcess.Start("explorer.exe", "/select," + itemPath);
#endifresult = true;}else if (Directory.Exists(itemPath)){UnityEngine.Application.OpenURL(itemPath);result = true;}#endifreturn result;}void OnClick(){ShowInExplorer("F:/2018");}
}
2.运行结果

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