zoukankan      html  css  js  c++  java
  • 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()
        {
    
        }
    }
    
  • 相关阅读:
    Balance的数学思想构造辅助函数
    1663. Smallest String With A Given Numeric Value (M)
    1680. Concatenation of Consecutive Binary Numbers (M)
    1631. Path With Minimum Effort (M)
    1437. Check If All 1's Are at Least Length K Places Away (E)
    1329. Sort the Matrix Diagonally (M)
    1657. Determine if Two Strings Are Close (M)
    1673. Find the Most Competitive Subsequence (M)
    1641. Count Sorted Vowel Strings (M)
    1679. Max Number of K-Sum Pairs (M)
  • 原文地址:https://www.cnblogs.com/dragon2012/p/4112273.html
Copyright © 2011-2022 走看看