コード例 #1
0
        IEnumerator DoLoadPrefab(BundledPrefab bundledPrefab)
        {
            yield return(StartCoroutine(DoLoadBundle(bundledPrefab.m_BundleID)));

            PrefabBundleManifest manifest = (PrefabBundleManifest)m_Bundles[bundledPrefab.m_BundleID].Bundle.mainAsset;

            if (manifest == null)
            {
                Debug.LogError("Found no manifest for bundle " + bundledPrefab.m_BundleID);

                yield break;
            }

            Instantiate(
                manifest.GetPrefab(bundledPrefab.m_PrefabID),
                bundledPrefab.transform.position,
                bundledPrefab.transform.rotation
                );

            // TODO: Copy over parenting, local transformation and game object data
        }
コード例 #2
0
        public static bool BuildBundle(string path)
        // TODO: Hold prefab paths in its own list so you only load the prefabs once
        {
            string manifestPath = path + "/" + "Manifest.asset";

            if (AssetDatabase.LoadMainAssetAtPath(manifestPath) != null)
            // Bail if manifest is found. Don't want to build a bundle twice.
            {
                Debug.LogError(string.Format(
                                   "Bundle at {0} already built. Found manifest.",
                                   path
                                   ));

                return(false);
            }

            // TODO: Bail if a non-prefab asset from this bundle is referenced from outside of it

            List <string> prefabs = new List <string> (ListPrefabs(path));
            // TODO: Remove all prefabs not referenced outside of the bundle

            List <BundledPrefab> bundledPrefabs = new List <BundledPrefab> ();
            PrefabBundleManifest manifest       = PrefabBundleManifest.Create();

            foreach (string prefab in prefabs)
            {
                bundledPrefabs.Add(manifest.Add(AssetDatabase.LoadMainAssetAtPath(prefab)));
            }

            AssetDatabase.CreateAsset(manifest, manifestPath);

            AssetDatabase.Refresh();

            List <string> allAssetPaths = ListAllAssets(path);

            BuildPipeline.BuildAssetBundle(
                AssetDatabase.LoadMainAssetAtPath(manifestPath),
                allAssetPaths.ConvertAll(
                    assetPath => AssetDatabase.LoadMainAssetAtPath(assetPath)
                    ).ToArray(),
                Application.dataPath + "/../" + manifest.ID + ".unity3d",
                BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies,
                EditorUserBuildSettings.activeBuildTarget
                );

            foreach (string prefab in prefabs)
            {
                AssetDatabase.CopyAsset(
                    prefab,
                    prefab.Substring(0, prefab.Length - "prefab".Length) + "original.prefab"
                    );
            }

            AssetDatabase.Refresh();

            for (int i = 0; i < prefabs.Count; i++)
            {
                PrefabUtility.ReplacePrefab(
                    bundledPrefabs[i].gameObject,
                    AssetDatabase.LoadMainAssetAtPath(prefabs[i]),
                    ReplacePrefabOptions.ReplaceNameBased
                    );

                Object.DestroyImmediate(bundledPrefabs[i].gameObject);
            }

            return(true);
        }