public static void CreateAssetBundlesFromSelection(UnityEngine.Object[] objects, string bundleParent) { if (objects == null || objects.Length == 0) { return; } float progress = 0.0f; foreach (UnityEngine.Object obj in objects) { string assetPath = AssetDatabase.GetAssetPath(obj); if (string.IsNullOrEmpty(assetPath)) { continue; } if (EditorUtility.DisplayCancelableProgressBar("Create AssetBundles from selection", string.Format("Adding {0} to bundle manager", assetPath), ++progress / objects.Length)) { EditorUtility.ClearProgressBar(); return; } string assetName = obj.name; if (BundleManager.GetBundleData(assetName) == null) { BundleManager.CreateNewBundle(assetName, bundleParent, false); } if (BundleManager.CanAddPathToBundle(assetPath, assetName)) { BundleManager.AddPathToBundle(assetPath, assetName); } } EditorUtility.ClearProgressBar(); }
private static void CreateBundle(string bunlePath, string parentName) { Object[] assetObjects = Resources.LoadAll(bunlePath); foreach (var asset in assetObjects) { string bundleName = asset.name; if (BundleManager.GetBundleData(bundleName) != null) { continue; } bool created = BundleManager.CreateNewBundle(bundleName, parentName, false); if (!created) { UnityEngine.Debug.LogError("Can't create bundle tree bundle : " + bundleName); } string bundleAssetDataPath = AssetDatabase.GetAssetPath(asset); if (BundleManager.CanAddPathToBundle(bundleAssetDataPath, bundleName)) { BundleManager.AddPathToBundle(bundleAssetDataPath, bundleName); } } }
public static void RefreshBundlesForUI() { const string STANDLONE_BUNDLE_NAME = "UIStandalone"; //Check Error var standaloneBundle = BundleManager.GetBundleData(STANDLONE_BUNDLE_NAME); if (standaloneBundle == null) { Debug.LogError("Cannot find parent bundle:" + STANDLONE_BUNDLE_NAME); return; } //standalone UI Bundles var groups = new Dictionary <string, string>(); foreach (var filePath in Directory.GetFiles("Assets/_Prefab/Edit/UI/Windwos", ".prefab", SearchOption.TopDirectoryOnly)) { var groupName = "UI/" + Path.GetFileNameWithoutExtension(filePath); groups.Add(groupName, filePath); } foreach (var filePath in Directory.GetFiles("Assets/_Prefab/Edit/UI/NewUI", ".prefab", SearchOption.TopDirectoryOnly)) { var groupName = "UI/" + Path.GetFileNameWithoutExtension(filePath); groups.Add(groupName, filePath); } var oldGroups = BundleManager.GetBundleData(STANDLONE_BUNDLE_NAME).GetChildren().ToArray(); foreach (var oldGroup in oldGroups) { if (!groups.ContainsKey(oldGroup)) { BundleManager.RemoveBundle(oldGroup); } } foreach (var pair in groups) { var groupName = pair.Key; var bundle = BundleManager.GetBundleData(groupName); if (bundle == null) { BundleManager.CreateNewBundle(groupName, STANDLONE_BUNDLE_NAME, BundleType.Normal); bundle = BundleManager.GetBundleData(groupName); BundleManager.AddPathToBundle(pair.Value, groupName); } else { var path = pair.Value; var guid = AssetDatabase.AssetPathToGUID(path); if (bundle.includeGUIDs[0] != guid) { BundleManager.RemoveAssetFromBundle(bundle.includeGUIDs[0], groupName); BundleManager.AddPathToBundle(path, groupName); } } } }
private void _CreateCombatBundleForCharacter() { var parentName = "Combat"; var srcBundle = m_Selections[0]; var destBundleName = srcBundle.Replace("Character/model_", "Combat/combat_"); var parentBundle = BundleManager.GetBundleData(parentName); if (parentBundle == null) { Debug.LogError("Cannot find parent bundle:" + parentName); return; } if (BundleManager.GetBundleData(destBundleName) != null) { Debug.LogWarning("Didn't creat[" + destBundleName + "].Its already in the bundle list."); return; } if (!BundleManager.CreateNewBundle(destBundleName, parentName, BundleType.Normal)) { Debug.LogError("Create failed,unexpeted."); return; } //Add effects- 通过名字特征来搜索符合的特效 var keyword = srcBundle.Replace("Character/model_", ""); var guids = AssetDatabase.FindAssets(keyword, new[] { "Asset/_Prefab/Edit.Effect" }); foreach (var guid in guids) { var assetPath = AssetDatabase.GUIDToAssetPath(guid); if (!Directory.Exists(assetPath)) { BundleManager.AddPathToBundle(assetPath, destBundleName); } } //Add Sounds - 声音的命名方式和特效不一样,使用特殊处理 var soundKeyword = keyword.Remove(keyword.IndexOf("_"), 1); var soundGuids = AssetDatabase.FindAssets(soundKeyword, new[] { "Asset/_Prefab/Edit/Sound" }); foreach (var soundGuid in soundGuids) { var assetPath = AssetDatabase.GUIDToAssetPath(soundGuid); if (!Directory.Exists(assetPath)) { BundleManager.AddPathToBundle(assetPath, destBundleName); } } }
private void CreateBundleFromAssetWithFolder(Object asset, string parent = "") { var path = AssetDatabase.GetAssetOrScenePath(asset); if (!path.StartsWith("Assets")) { return; } string defBundleName = System.IO.Path.GetFileNameWithoutExtension(path); string currentBundleName = defBundleName; //int index = 0; //while (BundleManager.GetBundleData(currentBundleName)!=null) //{ // currentBundleName = defBundleName + (++index); //} currentBundleName = parent + "/" + currentBundleName; if (BundleManager.GetBundleData(currentBundleName) != null) { Debug.Log("Skip [" + currentBundleName + "].Its already int the bundle list"); return; } if (string.IsNullOrEmpty(parent) && m_Selections.Count == 1) { parent = m_Selections[0]; } var bundleType = path.EndsWith(".unity") ? BundleType.Scene : BundleType.Normal; bool created = BundleManager.CreateNewBundle(currentBundleName, parent, bundleType); if (created) { if (BundleManager.CanAddPathToBundle(path, currentBundleName)) { BundleManager.AddPathToBundle(path, currentBundleName); } if (IsFold(parent)) { SetFold(parent, false); } m_Selections.Clear(); m_Selections.Add(currentBundleName); } }
void OnRecieve(GUIDragHandler.DragDatas recieverData, GUIDragHandler.DragDatas dragData) { if (dragData.customDragData == null && dragData.dragPaths.Length != 0) { foreach (string dragPath in dragData.dragPaths) { if (dragPath == null) { continue; } if (BundleManager.CanAddPathToBundle(dragPath, (string)recieverData.customDragData)) { BundleManager.AddPathToBundle(dragPath, (string)recieverData.customDragData); } } } else { BundleManager.SetParent((string)dragData.customDragData, (string)recieverData.customDragData); } }
//you can use this to make prefabs & assetbundles, or just prefabs public static void CreateAssetBundlesFromScene(bool do_assetbundling = true) { //BundleData[] bundles = BundleManager.bundles.Select(); string[] names = BundleManager.bundles.Select(bundle => bundle.name).ToArray(); string active_level = EditorApplication.currentScene; active_level = System.IO.Path.GetFileName(active_level); string[] levelsplit = active_level.Split('.'); active_level = levelsplit[0]; //if scene doesnt have an assetbundle folder bool level_has_assetbundle_folder = false; for (int i = 0; i < names.Length; i++) { if (string.Equals(names[i], active_level)) { level_has_assetbundle_folder = true; break; } } //create a scene assetbundle folder if (!level_has_assetbundle_folder) { CreateFolderNode("+" + active_level); } //get all the gameobjects that should be assetbundles in the scene GameObject[] obj = (GameObject[])GameObject.FindSceneObjectsOfType(typeof(GameObject)); List <GameObject> assetbundle_list = new List <GameObject>(); for (int i = 0; i < obj.Length; i++) { if (obj[i].GetComponent <AssetBundleHelper>() != null) { assetbundle_list.Add(obj[i]); } } //does the prefab exits? for (int i = 0; i < assetbundle_list.Count; i++) { GameObject assetbundle_obj = assetbundle_list[i]; string prefab_path = "Assets/_GameAssets/Res/Environment/" + active_level + "/Prefabs/" + assetbundle_list[i].name + ".prefab"; if (!Directory.Exists("Assets/_GameAssets/Res/Environment/" + active_level + "/Prefabs/")) { AssetDatabase.CreateFolder("Assets/_GameAssets/Res/Environment/" + active_level, "Prefabs"); } //needs a prefab //if(!AssetDatabase.Contains(assetbundle_list[i])) { //create a prefab! Transform parent = assetbundle_obj.transform.parent; Vector3 local_pos = assetbundle_obj.transform.localPosition; Vector3 local_scale = assetbundle_obj.transform.localScale; Quaternion local_rotation = assetbundle_obj.transform.localRotation; GameObject new_prefab = PrefabUtility.CreatePrefab(prefab_path, assetbundle_obj); GameObject.DestroyImmediate(assetbundle_obj); GameObject new_obj = (GameObject)EditorUtility.InstantiatePrefab(new_prefab); new_obj.transform.localPosition = local_pos; new_obj.transform.localScale = local_scale; new_obj.transform.localRotation = local_rotation; new_obj.transform.parent = parent; assetbundle_obj = new_obj; } if (do_assetbundling) { if (BundleManager.GetBundleData(assetbundle_obj.name) == null) { BundleManager.CreateNewBundle(assetbundle_obj.name, "+" + active_level, false); } //add the new prefab to the assetbundle if (BundleManager.CanAddPathToBundle(prefab_path, assetbundle_obj.name)) { BundleManager.AddPathToBundle(prefab_path, assetbundle_obj.name); } } } }