AssetBundle小实验
来源:互联网 发布:微信浏览器js刷新页面 编辑:程序博客网 时间:2024/06/10 05:00
第一个场景:
从本地打包好后加载资源和场景资源方式:
首先要导出包,方法如下:
using UnityEngine;
using UnityEditor;
public class ExportAssetBundles
{
//在Unity编辑器中添加菜单
[MenuItem("Assets/Build AssetBundle From Selection")]
static void ExportResourceRGB2()
{
// 打开保存面板,获得用户选择的路径
string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle");
if (path.Length != 0)
{
// 选择的要保存的对象
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//打包
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);
}
}
[MenuItem("Assets/Save Scene")]
static void ExportScene()
{
// 打开保存面板,获得用户选择的路径
string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
if (path.Length != 0)
{
// 选择的要保存的对象
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
string[] scenes = {"Assets/scene1.unity"};
//打包
BuildPipeline.BuildPlayer(scenes,path,BuildTarget.StandaloneWindows,BuildOptions.BuildAdditionalStreamedScenes);
}
}
}
选择要导出的包,然后保存在选择的路径下,保存,然后用下面的脚本来加载
using System;
using UnityEngine;
using System.Collections;
public class Load: MonoBehaviour
{
private string BundleURL="file:///D:/jeanshaw/UnityProjects/BundleTest/cube.assetbundle";
private string SceneURL="file:///D:/jeanshaw/UnityProjects/BundleTest/scenetest.unity3d";
void Start()
{
//BundleURL = "file//"+Application.dataPath+"/cube.assetbundle";
Debug.Log(BundleURL);
StartCoroutine(DownloadAssetAndScene());
}
IEnumerator DownloadAssetAndScene()
{
//下载assetbundle,加载Cube
using (WWW asset = new WWW(BundleURL))
{
yield return asset;
AssetBundle bundle = asset.assetBundle;
Instantiate(bundle.Load("cube"));
bundle.Unload(false);
yield return new WaitForSeconds(3);
}
//下载场景,加载场景
using (WWW scene = new WWW(SceneURL))
{
yield return scene;
AssetBundle bundle = scene.assetBundle;
Application.LoadLevel("scene1");
}
}
}
第二个场景
将预置体和他们共用的图片分别打包,打包代码:
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class Pushandpop {
public static bool CheckPlatform(UnityEditor.BuildTarget target)
{
if (EditorUserBuildSettings.activeBuildTarget != target)
{
EditorUtility.DisplayDialog("Target is not mathch", "Current platform:" + EditorUserBuildSettings.activeBuildTarget + "\nTarget Platform:" + target, "OK");
return false;
}
return true;
}
[MenuItem("Test/BuildAssetBundle")]
static void Execute()
{
string SavePath = "D:\\";
BuildAssetBundleOptions buildOp = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets |
BuildAssetBundleOptions.DeterministicAssetBundle;
BuildPipeline.PushAssetDependencies();
Object sharedAsset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Tex1.jpg");
BuildPipeline.BuildAssetBundle(sharedAsset, null, SavePath + sharedAsset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
BuildPipeline.PushAssetDependencies();
Object p1Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/P1.prefab");
BuildPipeline.BuildAssetBundle(p1Asset, null, SavePath + p1Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PushAssetDependencies();
Object p2Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/P2.prefab");
BuildPipeline.BuildAssetBundle(p2Asset, null, SavePath + p2Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
EditorUtility.DisplayDialog("", "Completed", "OK");
AssetDatabase.Refresh();
}
}
然后分别加载出来,代码如下:
using UnityEngine;
using System.Collections;
public class BuildTest : MonoBehaviour {
void OnGUI()
{
if (GUI.Button(new Rect(0f, 0f, 100f, 20f), Caching.spaceOccupied.ToString()))
{
Caching.CleanCache();
}
if (GUI.Button(new Rect(0f, 30f, 100f, 20f), "Load Share Res"))
{
StartCoroutine(Load(@"file://D:\Tex1.assetbundle",1));
}
if (GUI.Button(new Rect(0f, 60f, 100f, 20f), "Load And Instantiate Preferb"))
{
StartCoroutine(LoadAndInstantiate(@"file://D:\P1.assetbundle", 1));
StartCoroutine(LoadAndInstantiate(@"file://D:\P2.assetbundle", 1));
}
}
IEnumerator Load(string url, int version)
{
WWW www = WWW.LoadFromCacheOrDownload(url, version);
yield return www;
}
IEnumerator LoadAndInstantiate(string url, int version)
{
WWW www = WWW.LoadFromCacheOrDownload(url, version);
yield return www;
if (!System.String.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
}
else
{
Object main = www.assetBundle.mainAsset;
GameObject.Instantiate(main);
}
}
}
第三个场景
从服务器上下载图片当作纹理图使用,首先要安装apache服务器,然后在目录文件下放一张图片,也可以直接本地下载,代码如下:
using UnityEngine;
using System.Collections;
using System.IO;
public class LoadTexture : MonoBehaviour {
public GameObject plane;
WWW www;
string filePath;
Texture2D test;
Texture2D newTexture;
void Start()
{
filePath = Application.dataPath + "/gg.jpg";
File.Delete(filePath);
if (System.IO.File.Exists(filePath))
{
Debug.Log("It's already exit.");
test = (Texture2D)Resources.Load("picture", typeof(Texture2D));
plane.renderer.material.mainTexture = test;
}
else
{
Debug.Log("Starting download the file.");
StartCoroutine(GetImage());
}
}
private bool isDone = false;
IEnumerator GetImage()
{
string url = "http://10.1.32.54:8080/liuyong.jpg";
www = new WWW(url);
yield return www;
newTexture = www.texture;
byte[] pngData = newTexture.EncodeToPNG();
File.WriteAllBytes(filePath, pngData);
isDone = true;
}
void OnGUI()
{
if (www != null && isDone)
{
plane.renderer.material.mainTexture = newTexture;
}
}
}
运行效果就不上传了,作为新手,还有很多要学习的地方,这个写下来当作笔记。
- AssetBundle小实验
- Unity5.0 AssetBundle 实验
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- assetbundle
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- AssetBundle
- 小实验
- 做了个导出AssetBundle的小插件
- 拆字小实验
- const的小实验
- 代码调优
- 用Java 8 Lambda表达式实现设计模式:命令模式
- Redis 的安装配置
- mac设置开机启动脚本或应用
- 常用的垃圾回收算法
- AssetBundle小实验
- 全栈工程师初级体验
- 斗地主 -(一) 如何开发斗地主
- JQuery Mobile学习笔记
- Activity四大组件之Service 方式一startService
- Markdown 语法
- C/C++程序编译步骤 如何生成可执行文件
- MySQL索引类型
- 【POJ1061】:青蛙的故事(未AC版)