• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ChenMo2 · Feb 27, 2013 at 03:10 AM · assetbundle

A question about parameter BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets when called BuildPipeline.BuildAssetBundle

I want to build some material asset bundles and some texture bundles in our project.

If I use BuildPipeline.BuildAssetBundle function with BuildAssetBundleOptions.CollectDependencies parameter to build a material bundle,the material will not attach a texture, so that it is very small. And if I use BuildPipeline.BuildAssetBundle function with BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets parameter to do that, it will attach a texture, so that it is much bigger.

 static public class BundleAsset
 {
     //  原始资源路径
     static private string materialPathName = @"Assets/Materials";               //  材质
     static private string texturePathName = @"Assets/Textures";                 //  贴图
 
     //  打包资源路径
     static private string materialBundlePathName;           //  材质
     static private string textureBundlePathName;            //  贴图
 
     //  包文件后缀
     private const string suffix = ".unity3d";
 
     static private bool hasInitialized = false;
 
     static private BuildTarget targetPlatform;
 
     //  初始化
     static private void Initialize()
     {
         string[] subRootPathes = Application.dataPath.Split('/');
         string rubbishPart = @"/" + subRootPathes[subRootPathes.Length - 1];
         string root = Application.dataPath.Replace(rubbishPart, "");
 
         materialBundlePathName = root + @"/Bundles/Materials";
         textureBundlePathName = root + @"/Bundles/Textures";
 
         if (!Directory.Exists(materialBundlePathName))
             Directory.CreateDirectory(materialBundlePathName);
 
         if (!Directory.Exists(textureBundlePathName))
             Directory.CreateDirectory(textureBundlePathName);
 
         //  判断目标平台
         if (Application.platform == RuntimePlatform.WindowsEditor)
             targetPlatform = BuildTarget.StandaloneWindows;
         else
             targetPlatform = BuildTarget.StandaloneWindows;
 
         hasInitialized = true;
     }
 
     //  获取文件名
     static private string GetFileName(string fullFileName)
     {
         string[] subFileNames = fullFileName.Split('/');
         int index = subFileNames.Length - 1;
         string fileName = @"Assets/" + subFileNames[index];
 
         return fileName;
     }
 
     //  是否是meta文件
     static private bool IsMetaFile(string fileName)
     {
         string[] subFileNames = fileName.Split('.');
 
         return subFileNames[subFileNames.Length - 1] == "meta";
     }
 
     //  打包所有prefab 物件 特效 风景
     [MenuItem("BundleAsset/Material And Texture [Regenerate All]", false, 1)]
     static public void BundlePrefab()
     {
         if (!hasInitialized)
             Initialize();
 
         BuildPipeline.PushAssetDependencies();
         {
             //  打包的顺序一定要这样,原因未知.
             //  贴图,材质
             BundleTexture();
 
             BundleMaterial();
         }
         BuildPipeline.PopAssetDependencies();
     }
 
     //  打包材质球
     static private void BundleMaterial()
     {
         string[] fileNames = Directory.GetFiles(materialPathName);
 
         for (int i = 0; i < fileNames.Length; ++i)
         {
             string fileName = GetFileName(fileNames[i]);
 
             if (IsMetaFile(fileName))
                 continue;
 
             Object obj = AssetDatabase.LoadMainAssetAtPath(fileName);
 
             BuildPipeline.BuildAssetBundle(obj, null, materialBundlePathName + @"/" + obj.name + suffix,
                 //  As you see
                 BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.DeterministicAssetBundle, targetPlatform);
         }
     }
 
     //  打包贴图
     static private void BundleTexture()
     {
         string[] fileNames = Directory.GetFiles(texturePathName);
 
         for (int i = 0; i < fileNames.Length; ++i)
         {
             string fileName = GetFileName(fileNames[i]);
 
             if (IsMetaFile(fileName))
                 continue;
 
             Object obj = AssetDatabase.LoadMainAssetAtPath(fileName);
 
             BuildPipeline.BuildAssetBundle(obj, null, textureBundlePathName + @"/" + obj.name + suffix,
                 BuildAssetBundleOptions.DeterministicAssetBundle, targetPlatform);
         }
     }
 }

Thank you in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to import the object from server to unity 2 Answers

problem creating assetBundle : size is way too small 1 Answer

LoadFromCacheOrDownload CRC bug? 1 Answer

Problem with AssetBundle unloading and mainAsset 1 Answer

Load 3D model from external drive in runtime without using Assetbundle ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges