码迷,mamicode.com
首页 > 其他好文 > 详细

u3d加载加密和未加密

时间:2014-11-21 11:59:23      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   sp   for   on   div   art   log   

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()
    {

    }
}

 

u3d加载加密和未加密

标签:blog   io   ar   sp   for   on   div   art   log   

原文地址:http://www.cnblogs.com/dragon2012/p/4112273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!