Strength Package: Unity Pack Assetbundle (Finale)

Source: Internet
Author: User

→→ previously summary: Let the user select the file to be packaged ←←

Grand Finale: More options

Unity packaging Assetbundle from getting started to giving up the series is finally coming to a big finale. "Little brother said that really can't write O (╥﹏╥) o" ...

After the last tutorial, the whole package is almost complete, but we have not yet added a packaged button. These times we'll add a pack and Cancel button and open up more options.

Before we begin, let's look at the definition of the Buildassetbundles function ↓↓

  Buildassetbundles (targetplatform); Parameters

In this function, the last two parameters specify the compression method used in packaging and the target platform, we can expose these two options for the user to choose.

Buildassetbundleoptions and Buildtarget are enumerated types of variables, and we can provide a drop-down button that allows the user to choose freely. The Editorguilayout.enumpopup method happens to do this, requires an enumeration type parameter, and automatically turns all options of that enum type into a drop-down button, so first we need to define a field of two enum types ↓↓

  Buildassetbundleoptions.       buildtarget.  StandaloneWindows64; 

Then add the following code in the Ongui function ↓↓

  = (buildassetbundleoptions)editorguilayout.  Enumpopup (= (buildtarget)editorguilayout.  Enumpopup (targetplatform);       

Note that the function return value is type-cast, and the first parameter represents the text hint in front of the drop-down box.

In the second tutorial: The second package, we wrote two packaging functions that represent two different ways of packaging. One is to package the selected files separately, the other is to put the selected files into a package, we can also customize an enumeration type to represent the two different packaging methods, has provided a more flexible choice.

First define the enumeration type and the enumeration variable ↓↓

  Packagestyle {      build_all_in_one_folder,    build_all_in_an_assetbundle,  Packagestyle. packagename;    

Then add the following code to the Ongui function ↓↓

    Guilayout.Space (10);Guilayout.Beginvertical (Guilayout.height (50)); style = (packagestyle) editorguilayout. enumpopup ( "package Style:", style); if (style == packagestyle. build_all_in_an_assetbundle) { packagename =  Editorguilayout. textfield ( "package Name:",  PackageName); } guilayout. endvertical ();            

Here we also determine the packaging method, if all the files into a package, provide a text box for the user to enter the name of the package, because we do not know which file name as the package name appropriate.

Now only the last step is packaged, we offer two buttons, one called packing, one called Cancel. If the cancellation is clicked, the window is closed, and if the package is clicked, the corresponding function is called according to the user's choice of packaging, and the window is closed after completion. We continue to add code in the Ongui function ↓↓

    Guilayout.Beginhorizontal ();if (Guilayout.Button ("Cancel",Guilayout.Width (80)) { This.Close (); }if (Guilayout.Button ("Pack",Guilayout.Width (80)) { boolCanpackage=True if (PackagePath==Null||PackagePath.Length<=0||!Directory.Exists (PackagePath)) {  Canpackage=False  Editorapplication.Beep ();} List<String>Selectedassets=NewList<String> (); IntCount=0; foreach (VarGoInchAssetspath) {  if (Flags [Count])   Selectedassets.ADD (GO);   ++Count } if (Selectedassets==Null||Selectedassets.Count<=0) {  Canpackage=False  Editorapplication.Beep ();} if (Canpackage) {  Switch (Style) {  CasePackagestyle.Build_all_in_one_folder: {   Buildassetsinonefolder (PackagePath,Selectedassets.ToArray ());   Break }  CasePackagestyle.Build_all_in_an_assetbundle:  {   buildassetsinonebundle (packagepath,  Selectedassets. toarray ());    break;  }   defaul T: {break;}  }  this.close (); }} guilayout. Endhorizontal ();               

When you click the Pack button, we hit explain ↓↓

First, there is a bool flag that indicates whether it can be packaged. There are two cases where packaging is not possible:

    • The package path given by the user is empty or does not exist;

    • User has not selected any resource files

For both of these cases, we set the flag bit to false so that the packaged function is not run. Editorapplication.beep () can give the system a "ding" sound, prompting the user to make a mistake.

In addition, we also need to pick out the files that the user chooses. That is, you need to traverse the Assetspath list to add a file path to a new linked list with a value of true in the flags array. If the list is empty, the user does not select any files.

If the flag bit (canpackage) is true, it means that it can be packaged. First, you need to determine the user's choice of packaging methods, and then call different packaging functions. After the final package is finished, call the Close () method to close the window.

Note that the packaging function used here and the second tutorial in the two packaging functions are a little different, in order to adapt to the new parameters, we have made a little change, the core of the part has not changed, just the way the parameter passed, I believe it is difficult to pour you!

There it is. The entire packaging process is over, some details and UI layout of the things we do according to their preferences, thank you for your support!

The end of the sprinkle flower?? 's Finger licking (°▽°) Dentetsu?

Strength Package: Unity Pack Assetbundle (Finale)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.