csharp 编辑器脚本,用于创建所选文件的资产包。复制到Unity3D环境并单击其要使用的菜单项。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 编辑器脚本,用于创建所选文件的资产包。复制到Unity3D环境并单击其要使用的菜单项。相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Elecelf.UnityUtils
{
class AssetBundles
{
protected static bool PackToAssetBundle(BuildTarget targetPlatform,
out UnityEngine.Object[] packedAssets,
BuildAssetBundleOptions option = BuildAssetBundleOptions.CollectDependencies,
string outputPath = "",
string outputName = "all.assetbundle")
{
if(outputPath == "")
{
outputPath = Application.dataPath + "/StreamingAssets/" + targetPlatform.ToString();
}
if (!Directory.Exists(outputPath))
Directory.CreateDirectory(outputPath);
UnityEngine.Object[] selectedAssets = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
packedAssets = selectedAssets;
if (BuildPipeline.BuildAssetBundle(null, selectedAssets, outputPath + "/" + outputName, option))
{
AssetDatabase.Refresh();
return true;
}
else
{
return false;
}
}
[MenuItem("Asset Bundle/Create AssetBundle")]
public static void PackAssetBundle()
{
UnityEngine.Object[] assets = {};
if (PackToAssetBundle(EditorUserBuildSettings.activeBuildTarget, out assets))
{
foreach(UnityEngine.Object asset in assets)
{
Debug.Log(asset.name + " has been packed.");
}
Debug.Log("Pack Completed.");
}
else
{
Debug.Log("Pack Failed.");
}
}
}
class AssetBundleWindow : EditorWindow
{
protected string path;
protected string fileName;
protected bool generatePlatformDir;
protected BuildAssetBundleOptions generateOption;
protected BuildTarget target;
[MenuItem("Asset Bundle/Create AssetBundle")]
public static void OpenWindow()
{
EditorWindow.GetWindow<AssetBundleWindow>();
}
internal void OnGUI()
{
String[] BuildAssetBundleOptionsStrings = { BuildAssetBundleOptions.CollectDependencies.ToString(),
BuildAssetBundleOptions.CompleteAssets.ToString(),
BuildAssetBundleOptions.DeterministicAssetBundle.ToString(),
BuildAssetBundleOptions.DisableWriteTypeTree.ToString(),
BuildAssetBundleOptions.UncompressedAssetBundle.ToString()};
String[] BuildTargetStrings = {BuildTarget.Android.ToString(),
BuildTarget.BB10.ToString(),
BuildTarget.FlashPlayer.ToString(),
BuildTarget.iPhone.ToString(),
BuildTarget.MetroPlayer.ToString(),
BuildTarget.NaCl.ToString(),
BuildTarget.PS3.ToString(),
BuildTarget.StandaloneGLESEmu.ToString(),
BuildTarget.StandaloneLinux.ToString(),
BuildTarget.StandaloneLinux64.ToString(),
BuildTarget.StandaloneLinuxUniversal.ToString(),
BuildTarget.StandaloneOSXIntel.ToString(),
BuildTarget.StandaloneOSXIntel64.ToString(),
BuildTarget.StandaloneOSXUniversal.ToString(),
BuildTarget.StandaloneWindows.ToString(),
BuildTarget.StandaloneWindows64.ToString(),
BuildTarget.Tizen.ToString(),
BuildTarget.WebPlayer.ToString(),
BuildTarget.WebPlayerStreamed.ToString(),
BuildTarget.Wii.ToString(),
BuildTarget.WP8Player.ToString(),
BuildTarget.XBOX360.ToString()
};
int currentTargetIndex = Array.IndexOf<string>(BuildTargetStrings, EditorUserBuildSettings.activeBuildTarget.ToString());
GUILayout.Label("File Information", EditorStyles.boldLabel);
path = EditorGUILayout.TextField("Path", Application.dataPath + "/StreamingAssets");
fileName = EditorGUILayout.TextField("File Name", "all.assetbundle");
generatePlatformDir = EditorGUILayout.Toggle("Generate Platform Directory", true);
GUILayout.Label("Options", EditorStyles.boldLabel);
string generateOptionContent = BuildAssetBundleOptionsStrings[EditorGUILayout.Popup("Option", 0, BuildAssetBundleOptionsStrings)];
string generateTargetContent = BuildTargetStrings[EditorGUILayout.Popup("Option", currentTargetIndex, BuildTargetStrings)];
generateOption = (BuildAssetBundleOptions)Enum.Parse(typeof(BuildAssetBundleOptions), generateOptionContent);
target = (BuildTarget)Enum.Parse(typeof(BuildTarget), generateTargetContent);
bool generate = GUILayout.Button("Generate");
if (generate)
CreateAssetBundle();
}
private void CreateAssetBundle()
{
UnityEngine.Object[] packedFiles;
if (generatePlatformDir)
path += "/" + target.ToString();
if (AssetBundles.PackToAssetBundle(target, out packedFiles, generateOption, path, fileName))
{
Debug.Log("Pack Completed.");
}
else
{
Debug.Log("Pack Failed.");
}
}
}
}
以上是关于csharp 编辑器脚本,用于创建所选文件的资产包。复制到Unity3D环境并单击其要使用的菜单项。的主要内容,如果未能解决你的问题,请参考以下文章
csharp Unity编辑器脚本,用于更好地编辑预制件。放入资产/编辑。
csharp 用于以所选元素阵列的词典顺序生成所有排列的扩展方法。