Unity3D -- 编辑器模式打开unity导出的应用程序

有时候我们想把一些外部命令集成到unity中,比如点击unity的一个按钮就可以打开导出的项目,或者执行一些项目更新等。
很简单,直接上代码:

#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.Threading;
using System.IO;
using UnityEditor;

public class CommandForUnity
{
    public static void LaunchAppllication ()
    {
        #if UNITY_EDITOR_OSX
        // windows上面的.exe相对路径
        string path = Application.dataPath + "/../Relase/Yummy.app";
        UnityEngine.Debug.Log("path:" + path);
        processCommand ("open", path);
        #else
        // windows上面的.exe相对路径
        string path = Application.dataPath + "/../Relase/Yummy.exe";
        Application.OpenURL(path);
        #endif
    }

    private static void processCommand (string command, string argument)
    {
        ProcessStartInfo start = new ProcessStartInfo (command);
        start.Arguments = argument;
        start.CreateNoWindow = false;
        start.ErrorDialog = true;
        start.UseShellExecute = true;

        if (start.UseShellExecute) {
            start.RedirectStandardOutput = false;
            start.RedirectStandardError = false;
            start.RedirectStandardInput = false;
        } else {
            start.RedirectStandardOutput = true;
            start.RedirectStandardError = true;
            start.RedirectStandardInput = true;
            start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
            start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
        }

        Process p = Process.Start (start);

        p.WaitForExit ();
        p.Close ();
    }
}
#endif

以上实现的功能是处于编辑器模式下,绘制一个按钮,比如“试玩”按钮,点击后直接打开我们的游戏,很简单的吧。拿去直接用吧,更改一下路径就可以了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值