Unity3d resource packing and encryption arrangement

Source: Internet
Author: User

Resource packaging script, placed under the Assets\editor folder

Using unityengine;using system.collections;using unityeditor;using system.io;public class assetpack:editor{/*[  MenuItem ("Custom editor/build assetbundle from Selection-track dependencies")]static void ExportResource2 () {//Bring    Up save panel string path = 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.C ollectdependencies |        Buildassetbundleoptions.completeassets);    Selection.objects = Selection; }} * *//*[menuitem ("Custom editor/build assetbundle complited to bytes")]static void ExportResource5 () {//Bring up S    Ave Panel String path = 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.C ollectdependencies |        Buildassetbundleoptions.completeassets, buildtarget.standalonewindows);        Selection.objects = Selection;        FileStream fs = new FileStream (path, FileMode.Open, FileAccess.ReadWrite); byte[] buff = new Byte[fs.        Length]; Fs. Read (Buff, 0, (int) fs.        Length);        string password = "Shanghaichaolan"; Packxor (Buff,buff.        Length,password); Debug.Log ("filelength:" + buff.        Length); Fs.        Close ();        File.delete (path); String binpath = path. Substring (0, path. LastIndexOf ('. '))        + ". Bytes";        FileStream cfs = new FileStream (binpath,filemode.create); Cfs. Write (buff, 0, buff.        Length); Debug.Log ("filelength:" + buff. Length);        Buff = null; Cfs.     Close (); }} */[menuitem ("Custom editor/save Scene2")]static void Exportresource () {//Bring up Save panel string path = Ed    Itorutility.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; }}[menuitem ("Custom editor/make unity3d file to bytes file")]static void Exportresourcenotrackss () {String path = Edito    Rutility.savefilepanel ("Save Resource", "", "New Resource", "Unity3d"); if (path.        Length! = 0) {FileStream fs = new FileStream (path, FileMode.Open, FileAccess.ReadWrite); byte[] buff = new Byte[fs. LEngth]; Fs. Read (Buff, 0, (int) fs.        Length);        string password = "Shanghaichaolan"; Packxor (buff, buff.        Length, password); Debug.Log ("filelength:" + buff.        Length); Fs.        Close ();        File.delete (path); String binpath = path. Substring (0, path. LastIndexOf ('. '))        + ". Bytes";        FileStream cfs = new FileStream (BinPath, FileMode.Create); Cfs. Write (buff, 0, buff.        Length); Debug.Log ("filelength:" + buff.        Length);        Buff = null; Cfs.    Close ();    }}static void Packxor (byte[] _data, int _len, string _pstr) {int length = _len;    int strcount = 0; for (int i = 0; i < length; ++i) {if (Strcount >= _pstr.        Length) strcount = 0;    _data[i] ^= (byte) _pstr[strcount++]; }}}

The menu will appear two, the resources to be packaged into a prefab, select the resources, and then the menu custom Editor/save Scene2 Enter the name

The newly generated file, then select the newly generated file, click the menu custom editor/make Unity3d file to bytes file input name

Another file is generated, then click on the file, the menu named Editor/save Scene2, so that the package encryption is OK

Using unityengine;using System.collections;public class Loadnew:monobehaviour {public string filename;    private string Bundleurl;    private string Assetname;        void Start () {//startcoroutine (Loadscenee ());    Startcoroutine (LoadResource ());        } IEnumerator Loadscenee () {string path;        Path = "file://" + Application.datapath + "/" + filename + ". 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);        } IEnumerator LoadResource () {bundleurl = "file://" + Application.datapath + "/" + filename + ". Unity3d";        Debug.Log ("Path:" + Bundleurl);        www m_download = new www (bundleurl);        Yield return m_download;      if (m_download.error! = null)  {//Debug.logerror (M_DOWNLOAD.ERROR);            Debug.logerror ("Warning errow:" + "Newscene");        Yield break;        } Textasset txt = m_Download.assetBundle.Load (filename, typeof (Textasset)) as Textasset;        byte[] data = txt.bytes;        Byte[] Decrypteddata = decryption (data);        Debug.Log ("Decrypteddata Length:" + decrypteddata.length);    Startcoroutine (Loadbundle (decrypteddata)); } IEnumerator Loadbundle (byte[] decrypteddata) {assetbundlecreaterequest ACR = assetbundle.createfrommemory (        Decrypteddata);        Yield return ACR;        Assetbundle bundle = Acr.assetbundle;    Instantiate (Bundle.mainasset); } byte[] Decryption (byte[] data) {byte[] tmp = new byte[data.        Length]; for (int i = 0; i < data. Length;        i++) {Tmp[i] = Data[i];        }//Shanghaichaolan string password = "Shanghaichaolan"; Packxor (tmp,tmp.        Length,password); REturn tmp;        } static void Packxor (byte[] _data, int _len, string _pstr) {int length = _len;        int strcount = 0; for (int i = 0; i < length; ++i) {if (Strcount >= _pstr.            Length) strcount = 0;        _data[i] ^= (byte) _pstr[strcount++]; }} void Update () {}}

Load encrypted and unencrypted resources

Unity3d resource packing and encryption arrangement

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.