U3D scenario resource Packaging

Source: Internet
Author: User

After one day, we finally packed up the U3D scenario, so that we don't have to modify too many programs and put the resources outside for modification. Benefits

But it was a very simple task for a day. Google said it was used as a scenario scene. The Unity file is packaged as unityd, and the result will never work (time is spent here, I believe it)

Later, I asked someone else, who said they were reading the document. I packed frefab. I just did it once. I went there.

The following is the package code.

Using unityengine; using system. collections; using unityeditor; public class assetpack: Editor {// package a single [menuitem ("Custom Editor/create assetbunldes main")] Static void createassetbunldesmain () {// obtain all the game objects selected in the project view [] selectedasset = selection. getfiltered (typeof (object), selectionmode. deepassets); // traverses all game objects foreach (Object OBJ in selectedasset) {// local test: We recommend that you put assetbundle in the streamingassets folder at last If not, create one, because the mobile platform can only read this path // streamingassets is a read-only path and cannot write/Server Download: you do not need to put it here, the client on the server uses the WWW class for download. String targetpath = application. datapath + "/streamingassets/" + obj. name + ". assetbundle "; if (buildpipeline. buildassetbundle (OBJ, null, targetpath, buildassetbundleoptions. collectdependencies) {debug. log (obj. name + "Resource Package successful");} else {debug. log (obj. name + "resource packaging failed") ;}// refresh the editor assetdatabase. refresh ();} [menuitem ("Custom Editor/create assetbunldes all")] Static void createassetbunldesall () {C Aching. cleancache (); string Path = application. datapath + "/streamingassets/All. assetbundle "; object [] selectedasset = selection. getfiltered (typeof (object), selectionmode. deepassets); foreach (Object OBJ in selectedasset) {debug. log ("create assetbunldes name:" + OBJ);} // pay attention to the second parameter here. If (buildpipeline. buildassetbundle (null, selectedasset, path, buildassetbundleoptions. collectdependencies )){ Assetdatabase. refresh ();} else {}} [menuitem ("Custom Editor/create scene")] Static void createsceneall () {// clear cache caching. cleancache (); string Path = application. datapath + "/escene. unity3d "; string [] levels = {" assets/myscene. unity "}; // packaging scenario buildpipeline. buildplayer (levels, path, buildtarget. standalonewindows, buildoptions. buildadditionalstreamedscenes); assetdatabase. refresh ();} [menuitem ("Custom Editor/save scene")] Static void exportscene () {// open the Save panel and obtain the path string Path = editorutility selected by the user. savefilepanel ("Save resource", "", "new resource", "unity3d"); If (path. length! = 0) {// select the object to be saved [] selection = selection. getfiltered (typeof (object), selectionmode. deepassets); string [] scenes = {"assets/myscene. unity "}; // package buildpipeline. buildplayer (scenes, path, buildtarget. standalonewindows, buildoptions. buildadditionalstreamedscenes);} assetdatabase. refresh ();} [menuitem ("Custom Editor/save scene2")] Static void exportresource () {// bring up save panel string Pa Th = editorutility. savefilepanel ("Save resource", "", "new resource", "unity3d"); If (path. length! = 0) {// build the resource file from the active selection. object [] selection = selection. getfiltered (typeof (object), selectionmode. deepassets); buildpipeline. buildassetbundle (selection. activeobject, selection, path, buildassetbundleoptions. collectdependencies | buildassetbundleoptions. completeassets); selection. objects = Selection ;}}}

The following is the loaded code.

using UnityEngine;using System.Collections;public class loadScene : MonoBehaviour{    // Use this for initialization    void Start ()    {        StartCoroutine(loadScenee());    }        // Update is called once per frame    void Update () {        }    IEnumerator loadScenee()    {        string path;        path = "file://" + Application.dataPath + "/StreamingAssets/eScene.unity3d";        Debug.Log(path);        WWW www = new WWW(path);        yield return www;        AssetBundle bundle = www.assetBundle;        //GameObject go = bundle.Load("gCube",typeof(GameObject)) as GameObject;        GameObject ObjScene = Instantiate(www.assetBundle.mainAsset) as GameObject;        bundle.Unload(false);           }}

Full memory

 

 

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.