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/save Scene2")]
static void Exportresource ()
{
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.collectdependencies | Buildassetbundleoptions.completeassets);
Selection.objects = Selection;
}
}
[MenuItem ("Custom editor/make unity3d file to bytes file")]
static void Exportresourcenotrackss ()
{
object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
foreach (Object obj in selection)
{
String path1 = Assetdatabase.getassetpath (obj);
FileStream fs = new FileStream (path1, FileMode.Open, FileAccess.ReadWrite);
byte[] buff = new Byte[fs. Length];
Fs. Read (Buff, 0, (int) fs. Length);
string password = "LLH";
Packxor (buff, buff. Length, password);
Debug.Log ("filelength:" + buff. Length);
Fs. Close ();
String binpath = path1. Substring (0, path1. 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 sub-menus, the resources to be packaged into prefab, select the resources, then the menu custom Editor/save Scene2 Enter the name of 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
That is, after packaging Assetbundle encryption and repackaging Assetbundle(Can I encrypt the package directly?). Obviously not, Loadbundle will re-create the Assetbundle through the decrypted bytes when loading the resource, so the assetbundlemust be packaged first)
Load Packaged Resources
Using Unityengine;
Using System.Collections;
public class Loadnew:monobehaviour
{
public string filename = "My Resource";
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;
Gameobject obj = (gameobject) instantiate (Bundle.mainasset);
Obj. SetActive (TRUE);
}
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 = "LLH";
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 ()
{
}
}
Unity3d Resource Packaging encryption