Unity3d Tutorial: Assetbundle

Source: Internet
Author: User

This code is the final debugging feasible code, through the following production process and code to explain step-by-step.

Script:

Script_cube_trigger_terrain1.cs  
       
Script_cube_trigger_terrain3.cs  
       
Objects:

1, people resources, need to have charactorcontroller, otherwise can not do object trigger collision response.

2, two cube, in its Inspector panel, the Boxcollider under the Istrigger attribute, adjust the shape of the cube, as the loading terrain trigger point, I put them in the terrain junction, the player touches the terrain opposite loading.

3, bind the script on the two cube.

4, the various terrain blocks are made, the terrain on the object as a child node terrain objects. In Project view, create several prefab files, drag the terrain files into them, and then make the Assetbundle.

5, the production process found a major problem, the client model build out of the file or in the editor intuitive to see the results, and built into the web format after the results are somewhat different. Webplayer data may be based on a fixed stream source---the WWW object. The member or static member object object defined in the CS script cannot save the resource after the current WWW object has just been downloaded. The download and load parts of the code can not be written separately, or because the loss of the object will fail to load, or worse, in Web mode, is not reported anything wrong. So the explanation for this is that the two scripts correspond to the two cube, each of which holds a WWW object that records the resources it has downloaded. Be careful not to use the Assetbundlerequest object to save the asset in the WWW object, although the examples in the manual are as follows, but our problem is here to do the recording before the experience and information is sufficient.

Scripting code:

Using Unityengine;  
       
  Using System.Collections;  public class Script_cube_trigger_terrain1:monobehaviour {int Flag = 0;    Record whether the resource has been loaded and avoid loading the//assetbundlerequest ABR multiple times;    <----just said here, for the time being, don't use string debugstr = "";        Debug information static WWW wwwobj; A separate object in each scene, void Start () {if (Flag = 0) {//To determine platform information, if it is a web  
       
  The player loads from the network and, if it is local, loads it from the local resource directory. if (Application.platform = = Runtimeplatform.osxwebplayer | |  
       
  Application.platform = = Runtimeplatform.windowswebplayer) {//Instance WWW object, which is equivalent to downloading the resource process from the network address represented by the parameter. The Globalconfig.getconnectip () method can obtain the global IP address of the current machine (in this case, the server-side and the client on the same machine) Wwwobj = new WWW ("http://" +glob  
       
  Alconfig.getconnectip () + "/ab/terrain/terrain1.unity3d");  
       
  else {//because the player is playing a web-published game, so there is no resources locally, so this can be omitted, local debugging can be opened. Wwwobj = new WWW ("fiLe:///e:/123/terrain1.unity3d "); } void Update () {}//current object (in this case, those two cube) if Collier's Istrigg  
       
  The ER attribute is checked, and the function is triggered when another collier collides with the object. void Ontriggerenter (Collider other) {//This if-type hierarchy are just for simple debug, we can P
  UT debug message in each curly braces//View more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/    if (Flag = = 0) {if (wwwobj!=null) {print (wwwObj.isDone.ToString ()); Determine if the WWW object has already downloaded the if (wwwobj.assetbundle!=null) {if WwwObj.assetBundle.mainAsset!=nu ll) {//load resouce into the assetbundle of Wwwobject WwwObj.assetBundle.LoadAsync ("T  
       
  Errain1 ", typeof (Gameobject));  
       
  Create a terrain instance if (instantiate (WwwObj.assetBundle.mainAsset)!= null) Flag = 1;  
 }   }      
  }}///debug with GUI to debug, visualize, pretty handy void Ongui () { Gui.  
       
  Label (New Rect (0,0,200,200), debugstr); }   }

Author: csdn Blog Unity3d Learner

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.