您的位置:首页 > 移动开发 > Unity3D

【Unity】unity打包下载(参考)

2016-01-07 14:03 453 查看
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

/// <summary>
/// 编辑器 打包
/// </summary>
public class BuildAssetBundle
{

[MenuItem("build/1.create All Scene AssetBundleName")]
static void clearAssetBundleName()
{
string[] files = Directory.GetFiles(Application.dataPath, "*.unity", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
string currentFile = files[i].Replace("\\", "/");
int startIndex = currentFile.IndexOf("Assets");
string assetFile = currentFile.Substring(startIndex, currentFile.Length - startIndex);
string sceneName = Path.GetFileNameWithoutExtension(assetFile);
AssetImporter ai = AssetImporter.GetAtPath(assetFile);
ai.assetBundleName = sceneName;
ai.assetBundleVariant = Main.AB_END;
}
}

[MenuItem("build/2.Build All AssetBundle")]
static void buildAssetBundle()
{
string outPath = Application.streamingAssetsPath + "/AssetBundle";
if (!Directory.Exists(outPath))
{
Directory.CreateDirectory(outPath);
}
BuildPipeline.BuildAssetBundles(outPath);
}

[MenuItem("build/3.clear All AssetBundle")]
static void clearAssetBundle()
{
string outPath = Application.streamingAssetsPath + "/AssetBundle";
if (!Directory.Exists(outPath))
{
return;
}

deleteFileOrFolder(outPath);
AssetDatabase.Refresh();
}

static void deleteFileOrFolder(string fileOrFolder)
{
if (Directory.Exists(fileOrFolder))
{
string[] allFiles = Directory.GetFiles(fileOrFolder);
if (allFiles != null)
{
for (int i = 0; i < allFiles.Length; i++)
{
deleteFileOrFolder(allFiles[i]);
}
}

string[] allFolders = Directory.GetDirectories(fileOrFolder);
if (allFolders != null)
{
for (int i = 0; i < allFolders.Length; i++)
{
deleteFileOrFolder(allFolders[i]);
}
}

Directory.Delete(fileOrFolder);
}
else
{
if (File.Exists(fileOrFolder))
{
File.Delete(fileOrFolder);
}
}
}
}

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

/// <summary>
/// 下载
/// </summary>
public class Main : MonoBehaviour
{

public const string AB_END = "assetbundle";
// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{

}

void OnGUI()
{
if (GUILayout.Button("加载a场景"))
{
StartCoroutine(loadScene("a"));
}
}

IEnumerator loadScene(string sceneName)
{
string fileFullPath = "file://" + Application.streamingAssetsPath + "/AssetBundle/" + sceneName + "." + AB_END;
WWW www = new WWW(fileFullPath);
yield return www;
if (www.error == null)
{
AssetBundle abScene = www.assetBundle;
SceneManager.LoadScene(sceneName);
}
else
{
Debug.LogError("fileFullPath:" + fileFullPath + " error:" + www.error);
}
www.Dispose();
www = null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: