被打开端:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.IO;
using System;
public class OpenGameobject : MonoBehaviour {
WWW loadscene;
AsyncOperation ao;
public Slider jindutiao;//加载进度条
uint nowprocess = 0;
public static AssetBundle abcde;//本地全局bundle文件
string CommandLine;
string[] CommandLineArgs;
int num;
string[] arr;
public Text tip;
bool load = false;
//Playvideo pv;
private void Start()
{
Screen.SetResolution(1024,768,false);
CommandLine = Environment.CommandLine;
CommandLineArgs = Environment.GetCommandLineArgs();
if (CommandLineArgs.Length <10)
{
Debug.Log(CommandLineArgs.Length.ToString());
Debug.Log(CommandLineArgs[0]);
arr = CommandLineArgs[1].Split('=');
if (arr.Length >= 2)
{
num = int.Parse(arr[1]);
load = true;
}
tip.text = CommandLineArgs[1]+"***"+num;
// Application.Quit();
}
else
{
if (CommandLineArgs[1] == "")
{
}
else
{
Debug.Log("启动失败:" + CommandLineArgs[1] + "");
tip.text = "启动失败:" + CommandLineArgs[1] + "";
// Application.Quit();
}
}
}
void OnLoadScene(int index)
{
if (abcde != null)
{
abcde.Unload(false);
}
StartCoroutine(LoadScene(Application.dataPath, index.ToString()));
}
// Update is called once per frame
void Update () {
if (arr.Length >= 2&&load)
{
OnLoadScene(num);
load = false;
}
if (loadscene != null && jindutiao != null && !loadscene.isDone)
{
// jztell.text = "场景在加载中,请等候……";
jindutiao.value = loadscene.progress;
// jzbl.text = (int)(jindutiao.value * 100) + "%";
}
if (loadscene != null && loadscene.isDone && ao != null && !ao.isDone)
{
// //jindutiao.gameObject.SetActive(true);
// jztell.text = "场景正在初始化,请等待……";
uint toProcess;
if (ao.progress < 0.9f)
{
toProcess = (uint)(ao.progress * 100);
//jzbl.text = (int)(ao.progress * 100) + "%";
}
else
{
toProcess = 100;
}
if (nowprocess < toProcess)
{
nowprocess++;
}
jindutiao.value = nowprocess / 100f;
// jzbl.text = (int)(jindutiao.value * 100) + "%";
}
if (nowprocess == 100)
{
ao.allowSceneActivation = true;
jindutiao.gameObject.SetActive(false);
// jzbl.text = "";
// jztell.text = "";
nowprocess = 0;
ao = null;
}
}
IEnumerator DownloadScene(string url, string name2)
{
WWW ws = new WWW(url);
yield return ws;
if (ws.error != null)
{
Debug.Log(ws.error);
}
else
{
if (ws.isDone)
{
byte[] scenebyte = ws.bytes;
int length222 = scenebyte.Length;
CreatPCFile(Application.dataPath, name2, scenebyte, length222);
}
}
}
void CreatPCFile(string paths, string name1, byte[] info, int length)
{
Stream st;
FileInfo t = new FileInfo(paths + "/" + name1 + ".unity3d");
if (!t.Exists)
{
st = t.Create();
}
else
{
return;
}
st.Write(info, 0, length);
st.Close();
st.Dispose();
}
IEnumerator LoadScene(string pathp, string name3)
{
string s = null;
#if UNITY_STANDALONE_WIN||UNITY_EDITOR
s = "file:///" + pathp + "/" + name3 + ".unity3d";
#elif UNITY_IPHONE
s=pathp+"/"+name3+".unity3d";
#elif UNITY_ANDROID
s="jar:file:///"+pathp+"/"+name3+".unity3d";
#endif
FileInfo tt = new FileInfo(pathp + "/" + name3 + ".unity3d");
if (!tt.Exists)
{
Debug.Log("该文件不存在");
}
else
{
loadscene =WWW.LoadFromCacheOrDownload(s,18);
yield return loadscene;
if (loadscene.error != null)
{
Debug.Log(loadscene.error);
}
else
{
if (loadscene.isDone)
{
//jindutiao.value = 0;
abcde = loadscene.assetBundle;
StartCoroutine(LD(name3));
}
}
}
}
IEnumerator LD(string name6)
{
ao = SceneManager.LoadSceneAsync(name6);
ao.allowSceneActivation = false;
yield return ao;
}
}
打开端(测试时用到了6个场景,所以到最后一个场景后再打开的是第一个):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Diagnostics;
using UnityEngine.UI;
public class OpenDaiCan : MonoBehaviour {
int index = 0;
public Button btn;
Process process;
string arg = "sc=";
// Use this for initialization
void Start () {
btn.onClick.AddListener(OnclickEvent);
}
void OnclickEvent()
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "C:/Users/YF/Desktop/ARUseVr/test.exe";//文件名还是原名,不用带快捷方式四个字
info.WorkingDirectory = "C:/Users/YF/Desktop/ARUseVr";//运行文件夹
info.Arguments = arg+index;//赋值的参数
info.UseShellExecute = true;
UnityEngine.Debug.Log(info.Arguments);
UnityEngine.Debug.Log(index);
//process = Process.Start("C:/Users/YF/Desktop/ARUseVr/test.exe", arg + index);//这个可行
index++;
if(index>5)
{
index = 0;
}
process = Process.Start(info);
}
}