A common method of dynamically loading objects in Unity3d

Source: Internet
Author: User

1. Use Resources.load (); parameter is path, need to create Resources folder in assets folder, through path to find, instantiate and add to memory, through instantiate dynamic loading method to achieve the loading of object scene;

2. Use Assetbundle to package presets or scenarios to package all the resources associated with them, which is a good solution to resource dependency issues

To package a resource first:

Using Unityengine;
Using System.Collections;
Using Unityeditor;
Using System.IO;
public class Aesstbundletest:monobehaviour {

[MenuItem ("Custom bundle/create Bundel Main")]
public static void Creatbundlemain ()
{
Gets the path of the selected object
object[] os = selection.getfiltered (typeof (Object), selectionmode.deepassets);
BOOL Isexist = directory.exists (Application.datapath + "/streamingassets");
if (!isexist)
{
Directory.CreateDirectory (Application.datapath + "/streamingassets");
}
foreach (Object o in OS)
{
String sourcepath = Assetdatabase.getassetpath (o);

String TargetPath = Application.datapath + "/streamingassets/" + O.name + ". Assetbundle";
if (Buildpipeline.buildassetbundle (O, null, TargetPath, buildassetbundleoptions.collectdependencies))
{
Print ("Create bundle cuccess!");
}
Else
{
Print ("failure happen");
}
Assetdatabase.refresh ();
}
}
[MenuItem ("Custom bundle/create Bundle All")]
public static void Createbundleall ()
{
BOOL Isexist = directory.exists (Application.datapath + "/streamingassets");
if (!isexist)
{
Directory.CreateDirectory (Application.datapath + "/streamingassets");
}
object[] os = selection.getfiltered (typeof (Object), selectionmode.deepassets);
if (OS = = NULL | | os. Length = = 0)
{
Return
}
String TargetPath = Application.datapath + "/streamingassets/" + "All.assetbundle";
if (Buildpipeline.buildassetbundle (null, OS, TargetPath, buildassetbundleoptions.collectdependencies))
{
Print ("Create bundle all cuccess");
}
Else
{
Print ("failure happen");
}
Assetdatabase.refresh ();
}

}

Put the above code in the editor, you can see the custom menu item in the menu bar, select the preset that needs to be packaged, you can package and output the corresponding preset to streamassets.

Here is the load:

Using Unityengine;
Using System.Collections;

public class Loadbundletest:monobehaviour {
The path of streamingassets under different platforms is different, here need to pay attention to.
public static readonly String pathurl =
#if unity_android
"jar:file://" + Application.datapath + "!/assets/";
#elif Unity_iphone
Application.datapath + "/raw/";
#elif Unity_standalone_win | | Unity_editor
"file://" + Application.datapath + "/streamingassets/";
#else
String. Empty;
#endif


Update is called once per frame
void Update () {

}

void Ongui ()
{
if (Guilayout.button ("Load Bundle Main"))
{
String path_shpere = Pathurl + "Myspherepreb.assetbundle";
Startcoroutine (Loadbundlemain (path_shpere));

String path_cube = Pathurl + "Mycubepreb.assetbundle";
Startcoroutine (Loadbundlemain (Path_cube));
Print (Path_cube);
}

if (Guilayout.button ("Load Bundle All"))
{
Startcoroutine (Loadbundleall (Pathurl + "All.assetbundle"));
}
}


Private IEnumerator Loadbundlemain (string path)
{
www bundle = new www (path);
Yield return bundle;
Instantiate (Bundle.assetBundle.mainAsset);
Bundle.assetBundle.Unload (FALSE);
Yield return 1;
}

Private IEnumerator Loadbundleall (string path)
{
www bundle = new www (path);
Yield return bundle;
Instantiate (Bundle.assetBundle.Load ("Mycubepreb"));
Instantiate (Bundle.assetBundle.Load ("Myspherepreb"));
Yield return 1;
}
}

A common method of dynamically loading objects in Unity3d

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.