简体   繁体   English

Unity 独立播放器 Windows 桌面平台 - 如何使用 Unity C# 从双击的文件中获取路径?

[英]Unity Standalone Player Windows Desktop Platform- How to get path from file that is double click using Unity C#?

My Windows desktop application made in Unity can read and play audio.wav but can't open it.我在 Unity 中制作的 Windows 桌面应用程序可以读取和播放 audio.wav,但无法打开它。 I want to double click a .WAV file and play it.我想双击一个 .WAV 文件并播放它。 I have done myPlayWav(string path) function.我已经完成了 myPlayWav(string path) 函数。 And assigned to .wav file extension to call and run my application when is double click with the mouse.并分配给 .wav 文件扩展名以在双击鼠标时调用和运行我的应用程序。 But:但:

How I get the audio file path when my application is open and run?当我的应用程序打开并运行时,如何获取音频文件路径? Do I need to include something?我需要包括一些东西吗? because there is no Unity function.因为没有Unity功能。

using UnityEngine;

public class AtStart : MonoBehaviour 
{    
    void Start () 
    {
        string path;
        System.Run( path ?) ...
        StartCoroutine( myPlayWav ( path) );
    }

    //...

Is better to use Awake()?使用 Awake() 更好吗?

Pease edit my English.皮斯编辑我的英语。

Use System.Environment.CommandLine .使用System.Environment.CommandLine
This returns the raw (unformatted) string of the commandline.这将返回命令行的原始(未格式化)字符串。 Then use string.Split(' ') to divide it into its individual args.然后使用string.Split(' ')将其分成单独的参数。 At index 0 there should be your application.在索引 0 处应该有您的应用程序。 At index 1 the file you want to open and then at further indexes other arguments.在索引 1 要打开的文件,然后在进一步索引其他参数。

using System;

void Start ()
{
    string[] args = Environment.CommandLine.Split(' ');
    string path = args[1];
    StartCoroutine(myPlayWav(path));
}

Note that if the application is NOT started by opening a file, args[1] will be empty.请注意,如果应用程序不是通过打开文件启动的,则 args[1] 将为空。

To prevent an IndexOutOfRange exception you could do this:为了防止IndexOutOfRange异常,你可以这样做:

using System;

void Start ()
{
    string[] args = Environment.CommandLine.Split(' ');
    if (args.Length < 1)
    {
        Debug.Log("There is no file that is trying to be opened!");
        return;
    }
    string path = args[1];
    StartCoroutine(myPlayWav(path));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM