Unity3d Custom Resource Package usage

Source: Internet
Author: User

1. Create a resource package:

Public class sript_01: monobehaviour

{

[Menuitem ("Custom Resource Package/create resource package")]

Static void execcreateassetbunldes ()

{

// Set the root path for saving the resource package

String targetdir = application. datapath + "/assetbundles ";

Object [] selectedasset = selection. getfiltered (typeof (object), selectionmode. deepassets );

// Create a directory to save the resource package

If (! Eirectory. exists (targetdir ))

{

Directory. createdirectory (targetdir );

}

For (INT I = 0; I <selectedasset. length; I ++)

{

// Generate the full path of the Resource Package

String filepath = targetdir + "/" + selectedasset [I]. Name + ". unity3d ";

// Determine whether the resource package file exists based on the path

If (file. exists (filepath ))

{

File. Delete (filepath );

}

// Generate a new resource package file

If (buildpipeline. buildassetbundle (selectedasset [I], null, filepath, buildassetbundleoptions. collectdependencies ))

{

Debug. Log ("Resource Package generated successfully ");

Assetdatabase. Refresh ();

}

Else

Debug. Log ("resource package file generation failed ");

}

}

}

}

 Note:This is an editor class. To use it you have to place your script in
Assets/EditorInside your project folder. Editor classes are in the unityeditor namespace so for C # scripts you need to add"Using unityeditor;"At the beginning of the script.

 

2. Download The resource package

Ienumerator loadbundlemat (string name)

{

Material MAT;

// Path of the resource on the local machine

WWW date = new WWW ("file: //" + application. datapath + "/assetbundles/" + name + "./unity3d ");

// Wait until the download is complete

Yield return date;

// Forcibly convert the downloaded resource into a material resource

MAT = (material) date. assetbundle. mainasset;

// Replace with the downloaded Resource

Renderer. Material = MAT;

// Release the resource object

Date. assetbundle. Unload (false );

}

 

Ienumerator loadbundleobject (string name)

{

WWW date = new WWW ("file: //" + application. datapath + "assetbundles/" + name + ". unity3d ");

// Wait until the download is complete and the game object is cloned,

Yield return instantiate (date. assetbundle. mainasset );

// Release the resource object

Date. assetbundle. Unload (false );

}

Note: After the resource package is downloaded, the resource file will remain in the assetbundle. mainasset object, and then use this object. An error occurs when downloading resource package objects consecutively. Therefore, you must manually release the object after downloading the resource.

 

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.