AssetDatabase是一个能获取工程资源的API,它提供一些方法比如:查找、加载、创建、删除和修改。Unity需要了解工程文件夹里的所有改变,假如想要获取或修改资源文件,就使用 AssetDatabase的API而不是文件IO流。
AssetDatabase接口仅在编辑器中可用,在构建的播放器中没有任何功能。与所有其他编辑器类一样,它只对放置在编辑器文件夹中的脚本可用(如果还没有这个文件夹,只需在项目的主资产文件夹中创建一个名为“editor”的文件夹)。
Movie importing requires Quicktime to beinstalled.
在Unity中使用MovieTexture播放视频会碰到movieimporting requires quicktime的错误,解决方法如下:
1、关闭Unity安装QuickTime播放器,打开Unity
2、重新启动计算机
3、重启Unity
4、使用管理员权限运行Unity
5、进入Unity3d中加载的视频文件,如果是白色的文件,右边属性页中有个勾,勾调后Unity会重新载入视频文件
AssetBundle打包:
[MenuItem("Assets/BuildAssetBundles")]
staticvoid BuildAllAssetBundles()
{
string assetBundleDirectory ="Assets/AssetBundles";
if (!Directory.Exists(assetBundleDirectory))
{
Directory.CreateDirectory(assetBundleDirectory);
}
BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows);
}
类必须继承自Editor,必须放在Editor文件夹下。选择要打包的文件资源,Inspector左下角AssetBundle中new一个新名字,右边None表示默认用AssetBundle的名字,也可以自己New一个。之后Assets->Build AssetBundles会有进度条,会自动生成文件夹AssetBundles,打包后的文件就在下面,有时打包后没反应,重启Unity就能出现
每个输出的AssetBundle文件都会出现在AssetBundle菜单中。另外,每个AssetBundle文件都会有一个关联文件.manifest。这个
manifest文件是文本类型,可以使用文本编辑器打开。这个文件提供了CRC和asset依赖的相关信息。
除了创建这两个文件,还有另外两个文件会被创建:另外一个AssetBundle和另外一个manifest文件。这两个文件只要任何
AssetBundle被创建的时候都会被创建。这两个文件是为了放置AssetBundles的文件夹创建,所以你如果一直把AssetBundle放在同
一个文件夹,只会得到两个额外的文件。额外的文件不仅会包含其他的manifest的信息,还会包含AssetBundle之间的引用关系。
publicstring url;
publicstring assetname;
IEnumerator Start()
{
using(WWW www=newWWW(url))
{
yieldreturn www;
if(www.error!=null)
{
Debug.LogError("网络错误");
}
else
{
AssetBundle bundle = www.assetBundle;
Object obj = bundle.LoadAsset(assetname);
Instantiate(obj);
bundle.Unload(false);
}
}
}
使用AssetBundle加载出来的资源,url为file://D:/unity/workspace/hearthstoneTest1/Assets/AssetBundles/test8
本地加载要加file://
test8为打包的assetBundle名,一般打包的AssetBundle用.assetbundle或.unity结尾,注意
assetname是assetBundle里预制体的名字
using 语句允许程序员指定使用资源的对象应当何时释放资源。using 语句中使用的对象必须实现IDisposable 接口。此接口提供了 Dispose 方法,该方法将释放此对象的资源。
①可以在using 语句之前声明对象。
Font font2 = new Font("Arial", 10.0f);
using(font2)
{
// use font2
}
②可以在 using 语句之中声明对象。
using(Font font2 = new Font("Arial", 10.0f))
{
// use font2
}
③可以有多个对象与 using 语句一起使用,但是必须在 using 语句内部声明这些对象。
using (Font font3=newFont("Arial",10.0f), font4=new Font("Arial",10.0f))
{
// Use font3 and font4.
}
使用规则
①using只能用于实现了IDisposable接口的类型,禁止为不支持IDisposable接口的类型使用using语句,否则会出现编译错误;
②using语句适用于清理单个非托管资源的情况,而多个非托管对象的清理最好以try-finnaly来实现,因为嵌套的using语句可能存在隐藏的Bug。内层using块引发异常时,将不能释放外层using块的对象资源;
③using语句支持初始化多个变量,但前提是这些变量的类型必须相同,例如:
using(Pen p1 = newPen(Brushes.Black), p2 = new Pen(Brushes.Blue))
{
//
}
④针对初始化多个不同类型的变量时,可以都声明为IDisposable类型,例如:
using (IDisposable font= new Font("Verdana", 12), pen = new Pen(Brushes.Black))
{
float size = (font as Font).Size;
Brush brush = (pen as Pen).Brush;
}
using实质
在程序编译阶段,编译器会自动将using语句生成为try-finally语句,并在finally块中调用对象的Dispose方法,来清理资源。所以,using语句等效于try-finally语句,例如:
using (Font f2 = newFont("Arial", 10, FontStyle.Bold))
{
font2.F();
}
被编译器翻译为:
Font f2 = new Font("Arial",10, FontStyle.Bold);
try
{
font2.F();
}
finally
{
if (f2 !=null) ((IDisposable)f2).Dispose();
}
IEnumerator Start()
{
WWW www =newWWW(url);
yieldreturn www;
if (www.error !=null)
{
Debug.LogError("网络错误");
}
else
{
AssetBundle bundle = www.assetBundle;
Object obj = bundle.LoadAsset(assetname);
Instantiate(obj);
bundle.Unload(false);
}
www.Dispose();
}
--不使用using时用Dispose()释放资源