CreateAssetBundles

using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections.Generic;

public class CreateAssetBundles
{

    [MenuItem("Tools/CreateAssetBundles")]
    static void CreateAllAssetBundles()
    {
        //string assetBundleDir = Application.streamingAssetsPath + "/en/";
        string assetBundleDir = Application.streamingAssetsPath + "/AssetBundles/";

        BuildPipeline.BuildAssetBundles(assetBundleDir, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
        Encypt(assetBundleDir);
    }
    static void Encypt(string ASSETPATH)
    {
        string[] f = Directory.GetFiles(ASSETPATH);
        List<string> files = new List<string>();
        foreach (string file in f)
        {
            if (file.Split('.').Length == 2)
            {
                files.Add(file);
                //Debug.Log(file);
            }
        }
        foreach (string fileName in files)
        {
            if (!File.Exists(fileName))
            {
                Debug.Log(fileName + " not exist");
                continue;
            }
            byte[] stream = File.ReadAllBytes(fileName);
            FileUtility.Encypt(ref stream);
            File.WriteAllBytes(fileName, stream);
        }
    }

}

/*****************************************************************************
 * Module:加密解密                                                          *
 * Author:QXL                                                               *
 * Time:2020/04/30 16:25:03                                                 *
 *****************************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FileUtility
{
    const byte m_key = ---;
    /// <summary>
    /// 加密/解密
    /// </summary>
    /// <param name="targetData">加密、解密对象</param>
    public static void Encypt(ref byte[] targetData)
    {
        //加密,与key异或,解密的时候同样如此
        int dataLength = targetData.Length;
        for (int i = 0; i < dataLength; ++i)
        {
            targetData[i] = (byte)(targetData[i] ^ m_key);
        }
    }
}
 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.IO;
using System.Xml;

public class LoadAsset : MonoBehaviour
{
    string JSONPATH;
    string ASSETBUNDLEPATH;
    string SPRITEPATH;

 
    /// <summary>
    /// 所有AB包字典
    /// </summary>
    private  Dictionary<string, GameObject> AllABPackage;
    /// <summary>
    /// 所有UIAb字典
    /// </summary>
    private Dictionary<string, GameObject> UIABPackage;
    /// <summary>
    /// 其他Ab包字典
    /// </summary>
    private Dictionary<string, GameObject> OtherABPackage;


    //private Model model;
    void Start()
    {
        
        JSONPATH = Application.streamingAssetsPath + "/equipments.json";
        ASSETBUNDLEPATH = Application.streamingAssetsPath + "/AssetBundles/";
        SPRITEPATH = Application.streamingAssetsPath + "/Icon/";
        AllABPackage = new Dictionary<string, GameObject>();
        UIABPackage = new Dictionary<string, GameObject>();
        OtherABPackage = new Dictionary<string, GameObject>();
        StartCoroutine(LoadABPAckage());

    }
    IEnumerator LoadABPAckage()
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(ASSETBUNDLEPATH); 
        FileInfo[] f = directoryInfo.GetFiles("*.sw");
        List<string> files = new List<string>();
        foreach (FileInfo file in f)
        {
            string name = file.Name;
            if (name.Contains(".sw"))
            {
                files.Add(name);
            }
        }


        foreach (string fileName in files)
        {
            byte[] stream = File.ReadAllBytes(ASSETBUNDLEPATH + fileName);
            FileUtility.Encypt(ref stream);
            AssetBundleCreateRequest loadedAssetBundlerequest = AssetBundle.LoadFromMemoryAsync(stream);
            yield return loadedAssetBundlerequest;
            AssetBundle loadedAssetBundle = loadedAssetBundlerequest.assetBundle;


            string name = fileName.Split('.')[0].ToUpper();
            GameObject go= loadedAssetBundle.LoadAsset<GameObject>(name);
            if (go == null) Debug.LogError(name+"为空");
            AllABPackage.Add(name, go); 
            if(name.Contains("UI_"))
            {
                UIABPackage.Add(name, go);
            }
            else
            {
                OtherABPackage.Add(name, go);

            }
            loadedAssetBundle.Unload(false);
        }


        AssetPoolArchives.Instance.AllABPackage = AllABPackage;
        AssetPoolArchives.Instance.UIABPackage = UIABPackage;
        AssetPoolArchives.Instance.OtherABPackage = OtherABPackage;


        ///一机一档赋值
        Dictionary<string, Part> _partDict = ReadJsonFile();
        foreach (var p in _partDict)
        {
            if(AllABPackage.ContainsKey(p.Value.name))
            {
                p.Value.model_ab = AllABPackage[p.Value.name];
            }
            else
            {
                Debug.LogError(p.Value.ab_name+"AB包路径不存在");

            }
        }
        AssetPoolArchives.Instance.Parts = _partDict;


        DontDestroyOnLoad(gameObject);

        transform.GetChild(0).GetChild(3).gameObject.SetActive(false);
        transform.GetChild(1).gameObject.SetActive(true);

        ArchivesStateManager.Instance.SetLoadArchivesScene();
        ControlManager.Instance.SendMessage(null, "LoadFinish");

        Destroy(this);

    }

    /// <summary> 
    /// 读取一机一档配置 得到字典
    /// </summary>
    Dictionary<string, Part> ReadJsonFile()
    {
        Dictionary<string, Part> _partDict = new Dictionary<string, Part>();
        string json_text = File.ReadAllText(JSONPATH);
        List<Part> parts = JsonConvert.DeserializeObject<List<Part>>(json_text);
        //Debug.LogError(parts);
        foreach (Part p in parts)
        {
            if (!_partDict.ContainsKey(p.fakeName))
            {
                _partDict.Add(p.fakeName, p);
            }
        }

        return _partDict;
    }


    string fp = Application.streamingAssetsPath + "/Game.json";
    /// <summary>
    /// 写成配置表
    /// </summary>
    /// <param name="model"></param>
    public static void WriteIntoJson(string path,Model model)
    {
        File.WriteAllText(path, JsonConvert.SerializeObject(model));
    }
}
/*
    void OnlyInstantiate(Dictionary<string,Part> parts)
    {
        foreach(Part p in parts.Values)
        {
            p.model=Instantiate(p.model_ab);
            p.model.transform.SetParent(_units[p.unit]);
        }
        Destroy(_loadingImage);
    }
*/

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using Newtonsoft.Json;
using System.IO;
using System.Xml;

public class LoadAssets : MonoBehaviour
{
  string ASSETBUNDLEPATH;

  /// <summary>
  /// 所有AB包字典
  /// </summary>
  private Dictionary<string, GameObject> AllABPackage;


  //private Model model;
  void Start()
  {
    ASSETBUNDLEPATH = Application.streamingAssetsPath + "/AssetBundles/";
    AllABPackage = new Dictionary<string, GameObject>();
    StartCoroutine(LoadABPAckage());

  }
  IEnumerator LoadABPAckage()
  {
 DirectoryInfo directoryInfo = new DirectoryInfo(ASSETBUNDLEPATH);
    FileInfo[] f = directoryInfo.GetFiles("*.sw");
    List<string> files = new List<string>();
    foreach (FileInfo file in f)
    {
      string name = file.Name;
      if (name.Contains(".sw"))
      {
        files.Add(name);
      }
    }


    foreach (string fileName in files)
    {
      byte[] stream = File.ReadAllBytes(ASSETBUNDLEPATH + fileName);
      FileUtility.Encypt(ref stream);
      AssetBundleCreateRequest loadedAssetBundlerequest = AssetBundle.LoadFromMemoryAsync(stream);
      yield return loadedAssetBundlerequest;
      AssetBundle loadedAssetBundle = loadedAssetBundlerequest.assetBundle;


      string name = fileName.Split('.')[0].ToUpper();
       Debug.Log(name);
      GameObject go= loadedAssetBundle.LoadAsset<GameObject>(name);
      if (go == null) Debug.LogError(name+"为空");
            Instantiate(go);
      AllABPackage.Add(name, go);
      loadedAssetBundle.Unload(false);
    }
   }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值