Unity3D六、unity3d assetbundle打包和从服务器加载下载资源之二利用assetbundle打包资源文件(解决材质球丢失等等问题)

①打开之前的工程文件endlessrunner03。

双击

②在Editor路径下创建C#文件ExportAssetBundles。

将这个文件重新命名为:ExportAssetBundles

C#文件ExportAssetBundles的具体内容为:

// C# Example

// Builds an asset bundle from the selected objects in the project view.

// Once compiled go to "Menu" -> "Assets" and select one of the choices

// to build the Asset Bundle



using UnityEngine;

using UnityEditor;
using System.IO;



public class ExportAssetBundles {
	
	[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
	
	static void ExportResource () {
		
		// Bring up save panel
		
		string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
		
		if (path.Length != 0) {
			
			// Build the resource file from the active selection.
			
			Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

			
			BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
			
			Selection.objects = selection;
			
		}
		
	}


	[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies  android")]
	
	static void ExportResourceAndroid () {
		
		// Bring up save panel
		
		string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
		
		if (path.Length != 0) {
			
			// Build the resource file from the active selection.
			
			Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
			
			
			BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets,BuildTarget.Android );
			
			Selection.objects = selection;
			
		}
		
	}









	
	[MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
	
	static void ExportResourceNoTrack () {
		
		// Bring up save panel
		
		string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
		
		if (path.Length != 0) {
			
			// Build the resource file from the active selection.
			
			BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
			
		}
		
	}

	[MenuItem("Assets/Build AssetBundle From Selection")]
	static void ExportResource02()
	{
		string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "assetbundle");
		if (path.Length != 0) 
		{
			Object[] selections = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
			BuildPipeline.BuildAssetBundle(Selection.activeObject, selections, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Android);
			AssetDatabase.Refresh();
		}
	}


	
}

我们只分析其中一个,其余的大同小异。

[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies  android")]

这句话的意思是在unity3d的菜单栏Assets增加菜单Build AssetBundle From Selection - Track dependencies  android

 

 

选中文件点击右键,也可以出现菜单Build AssetBundle From Selection - Track dependencies  android

 

    
    static void ExportResourceAndroid () {
        
        // Bring up save panel
        
        string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
这句话的意思是将打包的文件保存为New Resources.unity3d,即后缀为.unity3d

    

    
        if (path.Length != 0) {
            
            // Build the resource file from the active selection.
            
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            
            
            BuildPipeline.BuildAssetBundle(Selection.activeObject, selection,path,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets,BuildTarget.Android );

上面这句话是核心代码

因为我们这里要在Android平台运行,所以,后面加参数BuildTarget.Android

如果是iPhone平台,后面加参数BuildTarget.iPhone
如果是在64位Windows平台,后面加参数BuildTarget.StandaloneWindows64;

直接点BuildTarget.    点以后可以出现很多平台的参数。


            
            Selection.objects = selection;
            
        }
        
    }

 

然后保存场景,保存项目。

这个时候:

 

③更改GraphicsSettings参数。

 

 

需要将以上改成以下这样:

 

 

 

先将Size改成4,然后回车,否则无法加进元素(Element)。 

 

点击右边的小按钮,可以更改

 

同理将下面两个改好。

改成:

④更改重名的资源,以防后期使用bundle.Load,用名字查找,找不到第二个匹配的资源。名字一样,只返回第一个匹配的资源。后期不用改重名的资源,可以用另一种方法,直接bundle.LoadAll,然后objs[ index ]取出资源,可以通过index下标取出重名的资源。

 

 

⑤导出资源。

选中资源(不选中场景)

然后点击右键或者在菜单栏点击Assets

等待进度条完成

大约8.92M

可以把这个资源放到Tomcat服务器上。后面一节详细讲述。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值