unity 怎么往生成的cs工程里面添加assets

unity 怎么往生成的cs工程里面添加assets,第1张

创建AssetBundle
1创建一个空的Prefab,命名Cube,然后创建一个Cube,将其拉到刚创建好的Prefab
2新建一个脚本ExportAssetBundlescs(代码来自官方文档),保存在Asset/Editor目录下
[csharp] view plaincopyprint
//在Unity编辑器中添加菜单
[MenuItem("Assets/Build AssetBundle From Selection")]
static void ExportResourceRGB2()
{
// 打开保存面板,获得用户选择的路径
string path = EditorUtilitySaveFilePanel("Save Resource", "", "New Resource", "assetbundle");
if (pathLength != 0)
{
// 选择的要保存的对象
Object[] selection = SelectionGetFiltered(typeof(Object), SelectionModeDeepAssets);
//打包
BuildPipelineBuildAssetBundle(SelectionactiveObject, selection, path, BuildAssetBundleOptionsCollectDependencies | BuildAssetBundleOptionsCompleteAssets, BuildTargetStandaloneWindows);
}
}
这时我们将看到Asset下面出现Build AssetBundle From Selection和Build Scene
3选中预设Cube,运行Build AssetBundle From Selection。这时会d出一个保存框,将其命名为cubeunity3d(这里为了测试方便,放在c盘。实际项目中,我们是需要将他们放在web服务器,供所有客户端下载更新)
4新建一个场景scene1unity,上面放置几个模型,然后保存
5选中该场景,在之前的ExportAssetBundlescs脚本中添加打包场景的函数,运行Assets->Build Scene,保存为scene1unity3d(这里为了测试方便,也放在c盘)
[csharp] view plaincopyprint
[MenuItem("Assets/Save Scene")]
static void ExportScene()
{
// 打开保存面板,获得用户选择的路径
string path = EditorUtilitySaveFilePanel("Save Resource", "", "New Resource", "unity3d");
if (pathLength != 0)
{
// 选择的要保存的对象
Object[] selection = SelectionGetFiltered(typeof(Object), SelectionModeDeepAssets);
string[] scenes = {"Assets/scene1unity"};
//打包
BuildPipelineBuildPlayer(scenes,path,BuildTargetStandaloneWindows,BuildOptionsBuildAdditionalStreamedScenes);
}
}
注意事项
aAssetBundle的保存后缀名可以是assetbundle或者unity3d
bBuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer
加载AssetBundle
我们通过一个简单的代码来演示如何加载assetbundle,包括加载普通asset和场景。
[csharp] view plaincopyprint
using System;
using UnityEngine;
using SystemCollections;
public class Load: MonoBehaviour
{
private string BundleURL = "file:///C:/cubeassetbundle";
private string SceneURL = "file:///C:/scene1unity3d";
void Start()
{
//BundleURL = "file//"+ApplicationdataPath+"/cubeassetbundle";
DebugLog(BundleURL);
StartCoroutine(DownloadAssetAndScene());
}
IEnumerator DownloadAssetAndScene()
{
//下载assetbundle,加载Cube
using (>

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10744140.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-11
下一篇2023-05-11

发表评论

登录后才能评论

评论列表(0条)

    保存