天天看點

在Unity中建立可遠端加載的.unity3d包

首發:http://blog.csdn.net/u010732148/article/details/8950552

--------------------------------------------------------------------------------------------------------

  在一個Unity項目中,釋出包本身不一定要包括所有的Asset(譯為資産或元件),其它的部分可以單獨釋出為.unity3d,再由程式從本地/遠端加載執行,這部分不在本文讨論範圍。雖然Unity并沒有直接提供.unity3d的導出功能,但可以通過其手冊了解到一些,并打開菜單項。

  翻看Unity關于AssetBundle的手冊,有相關的連結:

  • BuildPipeline.BuildAssetBundle
  • Building AssetBundles

【注意】導出.unity3d格式需要pro版本,非pro版本可以打開菜單項,但導出時會提示錯誤:

在Unity中建立可遠端加載的.unity3d包

  我們可以使用Untiy提供的現成的腳本打開兩個導出.unity3d的菜單項,也可以使用API根據自己的需求來寫。當項目變得越來越大時,手工導出AssetBundle會越來越吃力,這時可能就需要自己來開發導出功能,自動建立AssetBundle了。

打開菜單項

  在Unity中建立名為ExprotAssetBundles的C#腳本,放到Editor目錄下(必須是這個目錄,以便在編輯器中生效)。把下面的代碼複制到ExprotAssetBundles腳本中(可以在Building AssetBundles中找到這段代碼)

// 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;
    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 - 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);
            }
        }
    }
           

這時,在Assets菜單下可以看到兩個新的菜單項:

在Unity中建立可遠端加載的.unity3d包

1. Build AssetBundle From Selection - Track dependencies   這個選項會把目前對象打包到一個asset bundle中,并包含所有依賴。 2. Build AssetBundle From Selection - No dependency tracking   與前一個相反的選項,隻包含所選的asset   例:建立一個Cube,拖拽生成一個預置體。右鍵點選預置體,選擇 "Build AssetBundle From Selection - Track dependencies" ,這時可以看到.unity3d的儲存視窗。在項目中建立一個名為AssetBundles的目錄,并把選中的預置體存為Cube.unity3d,可以看到視窗顯示如下:

在Unity中建立可遠端加載的.unity3d包

現在,就可以把Cube.unity3d放到任意位置,或自己的伺服器上。

如何在建立元件包時修改屬性

  可以在調用 BuildPipeline.BuildAssetBundle以後使用 AssetDatabase.ImportAsset來強制導入元件,然後用 AssetPostprocessor.OnPreprocessTexture來設定需要的屬性。

  下面的示例來展示建構元件包時如何設定不同的紋理貼圖。

// Builds an asset bundle from the selected objects in the project view,
// and changes the texture format using an AssetPostprocessor.

using UnityEngine;
using UnityEditor;

public class ExportAssetBundles {

	// Store current texture format for the TextureProcessor.
	public static TextureImporterFormat textureFormat;

	[MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB2")]
	static void ExportResourceRGB2 () {
		textureFormat = TextureImporterFormat.PVRTC_RGB2;
		ExportResource();		
	}	

	[MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB4")]
	static void ExportResourceRGB4 () {
		textureFormat = TextureImporterFormat.PVRTC_RGB4;
		ExportResource();
	}

	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);

			foreach (object asset in selection) {
				string assetPath = AssetDatabase.GetAssetPath((UnityEngine.Object) asset);
				if (asset is Texture2D) {
					// Force reimport thru TextureProcessor.
					AssetDatabase.ImportAsset(assetPath);
				}
			}

			BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
			Selection.objects = selection;
		}
	}
}
           
// Changes the texture format when building the Asset Bundle.
using UnityEngine;
using UnityEditor;
public class TextureProcessor : AssetPostprocessor
{   
	void OnPreprocessTexture() {
		TextureImporter importer = assetImporter as TextureImporter;
		importer.textureFormat = ExportAssetBundles.textureFormat;
	}
}
           

也可以使用AssetDatabase.ImportAssetOptions.來控制如何導入元件。

【注】遠端加載.unity3d包,可以使用WWW類

繼續閱讀