簡體   English   中英

Unity 獨立播放器 Windows 桌面平台 - 如何使用 Unity C# 從雙擊的文件中獲取路徑?

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

我在 Unity 中制作的 Windows 桌面應用程序可以讀取和播放 audio.wav,但無法打開它。 我想雙擊一個 .WAV 文件並播放它。 我已經完成了 myPlayWav(string path) 函數。 並分配給 .wav 文件擴展名以在雙擊鼠標時調用和運行我的應用程序。 但:

當我的應用程序打開並運行時,如何獲取音頻文件路徑? 我需要包括一些東西嗎? 因為沒有Unity功能。

using UnityEngine;

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

    //...

使用 Awake() 更好嗎?

皮斯編輯我的英語。

使用System.Environment.CommandLine
這將返回命令行的原始(未格式化)字符串。 然后使用string.Split(' ')將其分成單獨的參數。 在索引 0 處應該有您的應用程序。 在索引 1 要打開的文件,然后在進一步索引其他參數。

using System;

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

請注意,如果應用程序不是通過打開文件啟動的,則 args[1] 將為空。

為了防止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