u3d加载加密和未加密

时间:2023-03-09 13:38:01
u3d加载加密和未加密
using UnityEngine;
using System.Collections; public class loadnew : MonoBehaviour
{
public bool IsCompressed =false;
public string filename;
private string BundleURL;
private string AssetName;
void Start()
{
if (!IsCompressed)
{
StartCoroutine(loadScenee());
}
else
{
StartCoroutine(LoadResource()); }
//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];
}
// shanghai
string password ="shanghai";
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()
{ }
}