Example #1
0
        /// <summary>
        /// 同步加载
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="setSceneObj">是否设置Parent为SceneTrs</param>
        /// <param name="bClear">跳场景是否清除</param>
        /// /// <param name="createCall">创建时回调函数</param>
        /// <returns></returns>
        public GameObject InstantiateObject(string path, bool setSceneParent = false, bool bClear = true)
        {
            uint        crc        = CRC32.GetCRC32(path);
            TResouceObj resouceObj = GetObjectFromPool(crc);

            if (resouceObj == null)
            {
                resouceObj       = mResouceObjPool.Allocate();
                resouceObj.Crc   = crc;
                resouceObj.Clear = bClear;

                //ResouceManager提供加载方法
                resouceObj = ResourcesManager.Instance.LoadResouce(path, resouceObj);


                if (resouceObj.ResItem.Obj != null)
                {
                    resouceObj.CloneObj = GameObject.Instantiate(resouceObj.ResItem.Obj) as GameObject;
                    resouceObj.OffData  = resouceObj.CloneObj.GetComponent <OfflineData>();
                    resouceObj.OffData.ResetProp();
                }
            }
            if (setSceneParent)
            {
                resouceObj.CloneObj.transform.SetParent(SceneTrs, false);
            }
            int tempID = resouceObj.CloneObj.GetInstanceID();

            if (!mResouceObjDic.ContainsKey(tempID))
            {
                mResouceObjDic.Add(tempID, resouceObj);
            }

            return(resouceObj.CloneObj);
        }
Example #2
0
        public void GetCRC32Test()
        {
            string file = "E:\\捕获2.PNG";
            uint   re   = CRC32.GetCRC32(file);

            Assert.AreEqual(0x89607A8F, re);
        }
Example #3
0
    /// <summary>
    /// 同步加载
    /// </summary>
    /// <param name="path"></param>
    /// <param name="bClear"></param>
    /// <returns></returns>
    public GameObject InstantiateObject(string path, bool setSceneObj = false, bool bClear = true)
    {
        uint        crc         = CRC32.GetCRC32(path);
        ResourceObj resourceObj = GetObjectFromPool(crc);

        if (resourceObj == null)
        {
            resourceObj          = m_ResourceObjClassPool.Spwan(true);
            resourceObj.m_Crc    = crc;
            resourceObj.m_bClear = bClear;
            resourceObj          = ResourceManager.Instance.LoadResource(path, resourceObj);
            if (!System.Object.ReferenceEquals(resourceObj.m_ResItem.m_Obj, null))
            {
                resourceObj.m_CloneObj    = GameObject.Instantiate(resourceObj.m_ResItem.m_Obj) as GameObject;
                resourceObj.m_OfflineData = resourceObj.m_CloneObj.GetComponent <OfflineData>();
            }
        }
        if (setSceneObj)
        {
            resourceObj.m_CloneObj.transform.SetParent(SceneTrs);
        }
        int tempID = resourceObj.m_CloneObj.GetInstanceID();

        if (!m_ResourceObjDic.ContainsKey(tempID))
        {
            m_ResourceObjDic.Add(tempID, resourceObj);
        }
        return(resourceObj.m_CloneObj);
    }
Example #4
0
    //同步资源加载----------------------------------------------------------------------------------

    /// <summary>
    /// 同步加载资源
    /// </summary>
    /// <param name="path"></param>
    public T LoadResource <T>(string path) where T : UnityEngine.Object
    {
        if (string.IsNullOrEmpty(path))
        {
            return(null);
        }
        uint         crc  = CRC32.GetCRC32(path);
        ResourceItem item = GetCacheResourceItem(crc);

        if (item != null)
        {
            return(item.m_Obj as T);
        }
        T obj = null;

#if UNITY_EDITOR
        if (!isLoadFromAssetBundle)
        {
            item = AssetBundleManager.Instance.GetResourceItem(crc);
            if (item != null && item.m_AssetBundle != null)
            {
                if (item.m_Obj != null)
                {
                    obj = (T)item.m_Obj;
                }
                else
                {
                    obj = item.m_AssetBundle.LoadAsset <T>(item.m_AssetName);
                }
            }
            else
            {
                if (item == null)
                {
                    item       = new ResourceItem();
                    item.m_Crc = crc;
                }
                obj = LoadAssetByEditor <T>(path);
            }
        }
#endif
        if (obj == null)
        {
            item = AssetBundleManager.Instance.LoadResourceAssetBundle(crc);
            if (item != null && item.m_AssetBundle != null)
            {
                if (item.m_Obj != null)
                {
                    obj = item.m_Obj as T;
                }
                else
                {
                    obj = item.m_AssetBundle.LoadAsset <T>(item.m_AssetName);
                }
            }
        }
        CacheResourceItem(path, ref item, crc, obj);

        return(obj);
    }
Example #5
0
//    private void Load()
//    {
//        AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/attack");
//        GameObject go = Instantiate(ab.LoadAsset<GameObject>("attack"));
//        Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>("Assets/GameData/Prefabs/Attack.prefab"));
//    }

    private void LoadAseetBundle()
    {
        AssetBundle            assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/loadconfig");
        MemoryStream           ms          = new MemoryStream(assetBundle.LoadAsset <TextAsset>("AssetBundleLoadConfig").bytes);
        BinaryFormatter        bf          = new BinaryFormatter();
        AssetBundleLoadProfile loadProfile = (AssetBundleLoadProfile)bf.Deserialize(ms);

        ms.Close();

        string path   = "Assets/GameData/Prefabs/Attack.prefab";
        uint   crc    = CRC32.GetCRC32(path);
        ABBase abBase = null;

        for (int i = 0; i < loadProfile.ABList.Count; i++)
        {
            if (loadProfile.ABList[i].Crc == crc)
            {
                abBase = loadProfile.ABList[i];
            }
        }

        for (int i = 0; i < abBase.DependentBundles.Count; i++)
        {
            AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.DependentBundles[i]);
        }

        if (abBase != null)
        {
            AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.AssetBundleName);
            GameObject  go = Instantiate(ab.LoadAsset <GameObject>(abBase.AssetName));
        }
    }
Example #6
0
    /// <summary>
    /// 加载AB资源
    /// </summary>
    private AssetBundle LoadAssetBundle(string name)
    {
        AssetBundleItem item = null;
        uint            crc  = CRC32.GetCRC32(name);

        if (!abItemDict.TryGetValue(crc, out item))
        {
            AssetBundle assetBundle = null;
            string      fullPath    = ConstConfig.ASSETBUNDLEPATH + "/" + name;
            //if (!File.Exists(fullPath))
            //{
            //    Debug.LogError("Can't find Asset, path:" + fullPath);
            //    return assetBundle;
            //}
            assetBundle = AssetBundle.LoadFromFile(fullPath);
            if (assetBundle == null)
            {
                Debug.LogError("Load AssetBundle error :" + fullPath);
            }
            item = abItemClassPool.Spawn();
            item.m_AssetBundle = assetBundle;
            item.m_RefCount++;
            abItemDict.Add(crc, item);
        }
        else
        {
            item.m_RefCount++;
        }

        return(item.m_AssetBundle);
    }
Example #7
0
    void TestLoadAB()
    {
        AssetBundle asset = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/AssetBundleConfig");
        TextAsset   t     = asset.LoadAsset <TextAsset>("AssetBundleConfig");

        MemoryStream      stream = new MemoryStream(t.bytes);
        BinaryFormatter   bf     = new BinaryFormatter();
        AssetBundleConfig abc    = bf.Deserialize(stream) as AssetBundleConfig;

        stream.Close();
        string path   = "Assets/GameData/Prefabs/Attack.prefab";
        uint   crc    = CRC32.GetCRC32(path);
        ABBase abBase = null;

        for (int i = 0; i < abc.ABList.Count; i++)
        {
            if (abc.ABList[i].Crc == crc)
            {
                abBase = abc.ABList[i];
            }
        }

        for (int i = 0; i < abBase.ABDenpends.Count; i++)
        {
            AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABDenpends[i]);
        }

        AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName);
        GameObject  go          = Instantiate(assetBundle.LoadAsset <GameObject>(abBase.AssetName), Vector3.forward, Quaternion.identity);
    }
    void TestLoadAB()
    {
        AssetBundle       configAB          = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/assetbundleconfig");
        TextAsset         ta                = configAB.LoadAsset <TextAsset>("AssetBundleConfig");
        MemoryStream      ms                = new MemoryStream(ta.bytes);
        BinaryFormatter   bf                = new BinaryFormatter();
        AssetBundleConfig assetBundleConfig = (AssetBundleConfig)bf.Deserialize(ms);

        ms.Close();
        string path   = "Assets/GameData/Prefabs/Attack.prefab";
        uint   crc    = CRC32.GetCRC32(path);
        ABBase aBBase = null;

        for (int i = 0; i < assetBundleConfig.ABBaseList.Count; i++)
        {
            if (assetBundleConfig.ABBaseList[i].Crc == crc)
            {
                aBBase = assetBundleConfig.ABBaseList[i];
            }
        }
        //先加载预制体依赖项
        for (int i = 0; i < aBBase.ABDependceList.Count; i++)
        {
            AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + aBBase.ABDependceList[i]);
        }
        //再加载预制体
        AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + aBBase.ABName);
        GameObject  obj         = GameObject.Instantiate(assetBundle.LoadAsset <GameObject>(aBBase.ABName));
    }
Example #9
0
        void OnSetLoadFinish(string path, Object obj, object param1 = null, object param2 = null, object param3 = null)
        {
            uint       tempCrc = CRC32.GetCRC32(path);
            USpriteObj tempUSpriteObj;

            tempUSpriteObj       = USpriteClassPool.Spawn(true);
            tempUSpriteObj.m_Obj = obj as GameObject;
            tempUSpriteObj.m_Obj.transform.SetParent(USpriteRecyclePoolTrs);
            tempUSpriteObj.m_SpriteList = tempUSpriteObj.m_Obj.GetComponent <AssetList>();
            for (int i = 1, n = tempUSpriteObj.m_SpriteList.list.Count; i < n; i++)
            {
                Texture2D tempTexture = tempUSpriteObj.m_SpriteList.list[i] as Texture2D;
                tempUSpriteObj.m_SpriteDic.Add(tempTexture.name, tempUSpriteObj.m_SpriteList.list[i]);
            }
            USpriteAssetDic.Add(tempCrc, tempUSpriteObj);

            List <USpriteAsyncObj> tempUSpriteAsyncList;

            if (!USpriteAsyncDic.TryGetValue(tempCrc, out tempUSpriteAsyncList) && tempUSpriteAsyncList == null)
            {
                ZLogger.Error("USpriteMgr中,设置界面USprite出错,{0}", path);
                return;
            }
            foreach (USpriteAsyncObj tempUSpriteAsyncObj in tempUSpriteAsyncList)
            {
                for (int i = 0, n = tempUSpriteAsyncObj.m_ObjList.Count; i < n; i++)
                {
                    SetSprite(path, tempUSpriteAsyncObj.m_IconName, tempUSpriteAsyncObj.m_ObjList[i]);
                }
                tempUSpriteAsyncObj.Reset();
                USpriteAsyncObjClassPool.Recycle(tempUSpriteAsyncObj);
            }
            tempUSpriteAsyncList.Clear();
            USpriteAssetDic.Remove(tempCrc);
        }
Example #10
0
    public long AsyncInsObj(string path, Action <string, Object, object> finishAction, ResPriority priority, bool isSceneRoot, bool isSceneClear = true, object param = null)
    {
        var crc    = CRC32.GetCRC32(path);
        var resObj = GetCacheResObj(crc);

        if (resObj != null)
        {
            if (isSceneRoot)
            {
                resObj.CloneObj.transform.SetParent(sceneRoot, false);
            }
            if (finishAction != null)
            {
                finishAction(path, resObj.CloneObj, param);
            }
            return(resObj.Guid);
        }
        var guid = CreateGuid();

        resObj              = resObjPool.Spawn(true);
        resObj.Guid         = guid;
        resObj.CRC          = crc;
        resObj.IsSceneRoot  = isSceneRoot;
        resObj.IsSceneClear = isSceneClear;
        resObj.FinishAction = finishAction;
        resObj.param        = param;
        createingResObjs.Add(guid, resObj);
        ResSvc.Ins.AsyncLoadAsset(path, OnLoadAssetFinish, priority, false, resObj);
        return(guid);
    }
Example #11
0
    public GameObject InsObj(string path, bool isSceneRoot = false, bool isSceneClear = true)
    {
        var crc    = CRC32.GetCRC32(path);
        var resObj = GetCacheResObj(crc);

        if (resObj == null)
        {
            resObj              = resObjPool.Spawn(true);
            resObj.CRC          = crc;
            resObj.IsSceneClear = isSceneClear;
            var gameObj = ResSvc.Ins.LoadAsset <Object>(path);
            if (gameObj != null)
            {
                resObj.CloneObj = GameObject.Instantiate(gameObj) as GameObject;
            }
        }

        if (isSceneRoot)
        {
            resObj.CloneObj.transform.SetParent(sceneRoot, false);
        }

        var cloneInsID = resObj.CloneObj.GetInstanceID();

        resObj.CloneInsId = cloneInsID;
        if (!resObjDic.ContainsKey(cloneInsID))
        {
            resObjDic.Add(cloneInsID, resObj);
        }

        return(resObj.CloneObj);
    }
Example #12
0
    /// <summary>
    /// 加载单个 assetbundle,根据名字
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    private AssetBundle LoadAssetBundle(string name)
    {
        AssetBundleItem item = null;
        uint            crc  = CRC32.GetCRC32(name);

        if (!assetBundleItemDic.TryGetValue(crc, out item))
        {
            // 加载 ab
            AssetBundle assetBundle = null;
            string      fullPath    = ABLoadPath + name;
            assetBundle = AssetBundle.LoadFromFile(fullPath);
            if (assetBundle == null)
            {
                Debug.LogError(" Load AssetBundle Error:" + fullPath);
            }
            // 给 AssetBundleItem 赋值
            item             = assetBundleItemPool.Spawn(true);
            item.assetBundle = assetBundle;
            item.RefCount++;
            assetBundleItemDic.Add(crc, item);
        }
        else
        {
            item.RefCount++; // 已经加载过了,此次加载就不需要重复加载 ab,而是把引用计数 +1
        }
        return(item.assetBundle);
    }
    private void Start()
    {
        AssetBundle       abConfig  = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/abconfig");
        TextAsset         textAsset = abConfig.LoadAsset <TextAsset>("AssetBundleConfig");
        MemoryStream      ms        = new MemoryStream(textAsset.bytes);
        BinaryFormatter   bf        = new BinaryFormatter();
        AssetBundleConfig config    = (AssetBundleConfig)bf.Deserialize(ms);

        ms.Close();
        string path   = "Assets/GameData/Prefabs/Attack.prefab";
        uint   crc    = CRC32.GetCRC32(path);
        ABBase abBase = null;

        for (int i = 0; i < config.ABList.Count; i++)
        {
            if (config.ABList[i].Crc == crc)
            {
                abBase = config.ABList[i];
            }
        }
        for (int i = 0; i < abBase.ABDependce.Count; i++)   // 加载依赖项
        {
            AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABDependce[i]);
        }
        AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName); // 加载 ab
        GameObject  go = GameObject.Instantiate(ab.LoadAsset <GameObject>(abBase.AssetName));             // 实例化资源
    }
Example #14
0
        /// <summary>
        /// 异步加载
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="deaFinish">资源加载完成回调</param>
        /// <param name="priority">加载优先级</param>
        /// <param name="setSceneParent">是否设置Parent为SceneTrs</param>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <param name="param3"></param>
        /// <param name="bClear">跳场景是否清除</param>
        public long InstantiateObjectAsync(string path, OnAsyncObjFinish deaFinish, LoadResPriority priority, bool setSceneParent = false,
                                           object param1 = null, object param2 = null, object param3 = null, bool bClear = true)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(0);
            }
            uint        crc    = CRC32.GetCRC32(path);
            TResouceObj resObj = GetObjectFromPool(crc);

            if (resObj != null)
            {
                if (setSceneParent)
                {
                    resObj.CloneObj.transform.SetParent(SceneTrs, false);
                }
                deaFinish?.Invoke(path, resObj.CloneObj, param1, param2, param3);
                return(resObj.Guid);
            }
            long guid = ResourcesManager.Instance.CreatGuid();

            resObj                = mResouceObjPool.Allocate();
            resObj.Crc            = crc;
            resObj.SetSceneParent = setSceneParent;
            resObj.Clear          = bClear;
            resObj.DealFinish     = deaFinish;
            resObj.Param1         = param1;
            resObj.Param2         = param2;
            resObj.Param3         = param3;
            //调用ResouceManager的异步加载接口
            ResourcesManager.Instance.AsyncLoadResouce(path, resObj, OnLoadResouceObjFinish, priority);
            return(guid);
        }
Example #15
0
        public void SetTexture(string pathCan, GameObject objCan, bool clearScene = true)
        {
            string      tempPath = TEXTURE_PATH + pathCan;
            uint        tempCrc  = CRC32.GetCRC32(tempPath);
            UTextureObj tempUTextureObj;

            if (UTextureAssetDic.TryGetValue(tempCrc, out tempUTextureObj) && tempUTextureObj != null)
            {
                RawImage tempRawImage = objCan.GetComponent <RawImage>();
                tempRawImage.texture = tempUTextureObj.m_Obj;
            }
            else
            {
                List <GameObject> tempObjList;
                if (UTextureAsyncDic.TryGetValue(tempCrc, out tempObjList) && tempObjList != null)
                {
                    tempObjList.Add(objCan);
                }
                else
                {
                    tempObjList = new List <GameObject>();
                    tempObjList.Add(objCan);
                    UTextureAsyncDic.Add(tempCrc, tempObjList);
                    ResourcesMgr.Instance.AsyncLoadResource(tempPath, OnSetLoadFinish, LoadResPriority.RES_SLOW, false, clearScene);
                }
            }
        }
Example #16
0
    //创建CRC     格式: 路径+CRC
    static void CreateCRCList()
    {
        string        bundle_list_path = @"Assets\StreamingAssets\bundle_list.txt";
        DirectoryInfo di = new DirectoryInfo(@"Assets\StreamingAssets");

        FileInfo[]   fis = di.GetFiles("*.unity3d");
        FileStream   fs  = new FileStream(bundle_list_path, FileMode.Create);
        StreamWriter wr  = new StreamWriter(fs);

        try
        {
            foreach (FileInfo fi in fis)
            {
                byte[]     buff  = new byte[fi.Length];
                FileStream fsbuf = fi.OpenRead();
                fsbuf.Read(buff, 0, Convert.ToInt32(fsbuf.Length));
                fsbuf.Close();

                uint crc = CRC32.GetCRC32(buff);
                wr.WriteLine(fi.Name + "-" + crc);
            }
            wr.Close();
            fs.Close();
        }
        catch (Exception e)
        {
            Debug.Log(e);
            return;
        }
    }
Example #17
0
    /// <summary>
    /// 加载单个assetbundle根据名字
    /// </summary>
    /// <returns>AssetBundle</returns>
    private AssetBundle LoadAssetBundle(string name)
    {
        AssetBundleItem item = null;
        uint            crc  = CRC32.GetCRC32(name);

        if (!_assetBundleItemDic.TryGetValue(crc, out item))
        {
            AssetBundle assetBundle = null;
            string      fullPath    = Application.streamingAssetsPath + "/" + name;
            if (File.Exists(fullPath))
            {
                assetBundle = AssetBundle.LoadFromFile(fullPath);
            }

            if (assetBundle == null)
            {
                Debug.LogError("Load AssetBundle Error Path:" + fullPath);
            }

            item             = _assetBundleItemPool.Spawn(true);
            item.assetbundle = assetBundle;
            item.refCount++;
            _assetBundleItemDic.Add(crc, item);
        }
        else
        {
            item.refCount++;
        }
        return(item.assetbundle);
    }
Example #18
0
        void CacheSceneList(int sceneID, bool holdBoo)
        {
            string scenePath = SCENE_PATH + sceneID + ".unity";
            uint   crc       = CRC32.GetCRC32(scenePath);

            if (holdBoo)
            {
                if (!holdSceneList.Contains(crc))
                {
                    holdSceneList.Add(crc);
                }
            }
            else
            {
                if (!sceneList.Contains(crc))
                {
                    if (sceneList.Count >= MaxCacheCount)
                    {
                        uint tempCrc = sceneList.Dequeue();
                        ResourcesMgr.Instance.ReleaseResourceScene(tempCrc);
                        sceneList.Enqueue(crc);
                    }
                    else
                    {
                        sceneList.Enqueue(crc);
                    }
                }
            }
        }
Example #19
0
    void Test()
    {
        string    path = "Assets/AssetBundleConfig.bytes";
        TextAsset t    = AssetDatabase.LoadAssetAtPath <TextAsset>(path);

        MemoryStream ms = new MemoryStream(t.bytes);

        BinaryFormatter   bf    = new BinaryFormatter();
        AssetBundleConfig ab    = (AssetBundleConfig)bf.Deserialize(ms);
        string            path2 = "Assets/Prefabs/Models/Boss_Bruce/bruce.FBX";
        uint   crc    = CRC32.GetCRC32(path2);
        ABBase abBase = null;

        for (int i = 0; i < ab.ABList.Count; ++i)
        {
            if (ab.ABList[i].Crc == crc)
            {
                abBase = ab.ABList[i];
                continue;
            }
        }

        AssetBundle asset = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName);

        Debug.Log(asset);
        GameObject obj = GameObject.Instantiate(asset.LoadAsset <GameObject>(abBase.AssetName));
    }
Example #20
0
        public void LoadScene(int mapID)
        {
            switchLockBoo = true;
            EventMgr.Instance.TriggerEvent(SceneConst.SWITCH_SCENE_STAR);
            UIMgr.Instance.OpenView <LoadingView>("LoadingView");

            EventMgr.Instance.TriggerEvent <string, int>(SceneConst.SWITCH_SCENE_PROGRESS, "开始加载场景", 5);

            MapExcel tempMapData = DataMgr.Instance.tableMap.GetInfoById(mapID);

            switchMapID = mapID;

            string scenePath = SCENE_PATH + tempMapData.mapResId.ToString() + ".unity";
            uint   crc       = CRC32.GetCRC32(scenePath);

            if (holdSceneList.Contains(crc) || sceneList.Contains(crc))
            {
                switchSceneID = tempMapData.mapResId;
            }
            else
            {
                ResourcesMgr.Instance.LoadResourceScene(scenePath);
                switchSceneID = tempMapData.mapResId;
            }
            EventMgr.Instance.TriggerEvent <string, int>(SceneConst.SWITCH_SCENE_PROGRESS, "开始加载场景", 15);
            switchAsyncBoo = true;
        }
Example #21
0
    /// <summary>
    /// 异步加载实例化物体
    /// </summary>
    /// <param name="path">物体路径</param>
    /// <param name="finishCallBack">完成后的回调</param>
    /// <param name="loadPriority">加载优先级</param>
    /// <param name="isSceneParant">是否存放在默认场景父亲下(这里跳转场景不会清除)</param>
    /// <param name="isClear">该物体跳转场景是否清除</param>
    /// <param name="param1">回调参数1</param>
    /// <param name="param2">回调参数2</param>
    /// <param name="param3">回调参数3</param>
    public void InstantiateObjectAsync(string path, OnAsyncFinishCallBack finishCallBack, LoadPriority loadPriority, bool isSceneParant = false, bool isClear = true,
                                       object param1 = null, object param2 = null, object param3 = null)
    {
        if (string.IsNullOrEmpty(path))
        {
            return;
        }
        uint           crc    = CRC32.GetCRC32(path);
        ResourceObject resObj = GetObjectFromPool(crc);

        if (resObj != null)
        {
            resObj.m_Already = false;
            resObj.m_IsClear = isClear;
            m_AsyncResObjectDict.Add(resObj.m_GameObj.GetInstanceID(), resObj);
            if (isSceneParant)
            {
                resObj.m_GameObj.transform.SetParent(m_SceneParentNode, false);
            }
            finishCallBack?.Invoke(path, resObj.m_GameObj, param1, param2, param3);
            return;
        }
        resObj                  = m_ResObjPool.Spawn();
        resObj.m_IsClear        = isClear;
        resObj.m_FinishCallBack = finishCallBack;
        resObj.m_Crc            = crc;
        resObj.m_IsSceneParent  = isSceneParant;
        resObj.m_LoadPriority   = loadPriority;
        resObj.m_Param1         = param1;
        resObj.m_Param2         = param2;
        resObj.m_Param3         = param3;
        ResourceManager.Instance.AsyncLoadResObj(path, resObj, OnAysncResObjCallBack);
    }
Example #22
0
    AssetBundle LoadAssetBundle(string bundleName)
    {
        var             bundleCrc = CRC32.GetCRC32(bundleName);
        AssetBundleItem item      = null;

        if (cacheAssetBundleItemDic.TryGetValue(bundleCrc, out item))
        {
            item.RefCount += 1;
            return(item.Bundle);
        }

        AssetBundle assetBundle = null;
        var         fullPath    = PathDefine.ABLoadPath + bundleName;

        if (File.Exists(fullPath))
        {
            assetBundle = AssetBundle.LoadFromFile(fullPath);
        }
        if (assetBundle == null)
        {
            Debug.LogErrorFormat("load bundle error {0}", bundleName);
            return(null);
        }

        item        = assetBundleItemPool.Spawn(true);
        item.Bundle = assetBundle;
        item.RefCount++;
        cacheAssetBundleItemDic.Add(bundleCrc, item);
        return(assetBundle);
    }
Example #23
0
    //异步资源加载-----------------------------------------------------------------------------------

    /// <summary>
    /// 异步加载资源
    /// </summary>
    /// <param name="path">资源路径</param>
    /// <param name="finishCallBack">完成后回调</param>
    /// <param name="loadPriority">加载优先级</param>
    /// <param name="param1">回调参数1</param>
    /// <param name="param2">回调参数2</param>
    /// <param name="param3">回调参数3</param>
    public void AsyncLoadResource(string path, OnAsyncFinishCallBack finishCallBack, LoadPriority loadPriority, bool isSprite = false,
                                  object param1 = null, object param2 = null, object param3 = null)
    {
        uint         crc  = CRC32.GetCRC32(path);
        ResourceItem item = GetCacheResourceItem(crc);

        if (item != null)
        {
            finishCallBack?.Invoke(path, item.m_Obj, param1, param2, param3);
            return;
        }

        //判断当前要加载的资源是否已存在加载列表中,不存在,则加入加载列表
        AsyncLoadResParam param = null;

        if (!m_LoadingAssetDict.TryGetValue(crc, out param) || param == null)
        {
            param            = m_AsyncLoadResPool.Spawn();
            param.m_Crc      = crc;
            param.m_Path     = path;
            param.m_Priority = loadPriority;
            param.m_IsSprite = isSprite;
            m_LoadAsyncResList[(int)loadPriority].Add(param);
            m_LoadingAssetDict.Add(crc, param);
        }
        //回调列表里添加回调
        AsyncCallBack asyncCallBack = m_AsyncCallBackPool.Spawn();

        asyncCallBack.finishCallBack = finishCallBack;
        asyncCallBack.param1         = param1;
        asyncCallBack.param2         = param2;
        asyncCallBack.param3         = param3;
        param.callBackList.Add(asyncCallBack);
    }
Example #24
0
    public AssetBundle LoadAssetBundle(uint crc)
    {
        AssetBundleConfig config = null;

        if (!assetBundleConfigDic.TryGetValue(crc, out config))
        {
            return(null);
        }
        var bundleCrc = CRC32.GetCRC32(config.BundleName);
        var item      = GetCacheBundle(bundleCrc);

        if (item != null)
        {
            return(item.Bundle);
        }

        var bundle = LoadAssetBundle(config.BundleName);

        if (config.DependBundles != null && config.DependBundles.Count > 0)
        {
            for (int i = 0; i < config.DependBundles.Count; ++i)
            {
                LoadAssetBundle(config.DependBundles[i]);
            }
        }

        return(bundle);
    }
Example #25
0
    protected CMapList <ResourceItem> m_NoRefrenceAssetMapList = new CMapList <ResourceItem>(); //没有实例化,存在于内存中

    public T LoadResource <T> (string path) where T : UnityEngine.Object
    {
        if (string.IsNullOrEmpty(path))
        {
            return(null);
        }
        uint         crc  = CRC32.GetCRC32(path);
        ResourceItem item = GetCacheResourceItem(crc);

        if (item != null)
        {
            return(item.m_Obj as T);
        }
        //无缓存 则创建
        T obj = null;

#if UNITY_EDITOR
        if (!m_LoadFromAssetBundle)
        {
            obj  = LoadAssetByEditor <T>(path);
            item = AssetBundleManager.Instance.FindResourceItem(crc);
        }
#endif
        if (obj == null)
        {
            item = AssetBundleManager.Instance.LoadResourceAssetsBundle(crc);
            if (item != null && item.m_AssetBundle != null)
            {
                obj = item.m_AssetBundle.LoadAsset <T>(item.m_AssetName);
            }
        }
        return(obj);
    }
Example #26
0
    void TestLoadAB()
    {
        //TextAsset textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/GameData/Data/ABData/AssetbundleConfig.bytes");
        AssetBundle       configAB     = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/assetbundleconfig");
        TextAsset         textAsset    = configAB.LoadAsset <TextAsset>("AssetBundleConfig");
        MemoryStream      stream       = new MemoryStream(textAsset.bytes);
        BinaryFormatter   bf           = new BinaryFormatter();
        AssetBundleConfig testSerilize = (AssetBundleConfig)bf.Deserialize(stream);

        stream.Close();
        string path   = "Assets/GameData/Prefabs/Attack.prefab";
        uint   crc    = CRC32.GetCRC32(path);
        ABBase abBase = null;

        for (int i = 0; i < testSerilize.ABList.Count; i++)
        {
            if (testSerilize.ABList[i].Crc == crc)
            {
                abBase = testSerilize.ABList[i];
                break;
            }
        }
        // 加载依赖ab包
        for (int i = 0; i < abBase.ABDependce.Count; i++)
        {
            AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABDependce[i]);
        }

        AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName);
        GameObject  obj         = GameObject.Instantiate(assetBundle.LoadAsset <GameObject>(abBase.AssetName));
    }
Example #27
0
        public async Task <bool> WriteFile(IASync async, int fileID, byte[] data, int count, long offset, uint crc)
        {
            if (async.UserToken != null)
            {
                var user = async.Token <UserInfo>();

                if (CRC32.GetCRC32(data) != crc)
                {
                    return(false);
                }
                if (user.FilePushDictionary.ContainsKey(fileID))
                {
                    var Stream = user.FilePushDictionary[fileID];
                    Stream.Position = offset;

                    await Stream.WriteAsync(data, 0, count);

                    return(true);
                }

                return(false);
            }

            return(false);
        }
Example #28
0
        /// <summary>
        /// 加载单个AssetBundle根据名字
        /// </summary>
        /// <param name="name">AB 包名</param>
        /// <returns></returns>
        AssetBundle LoadAssetBundle(string name)
        {
            TAssetBundleItem item = null;
            uint             crc  = CRC32.GetCRC32(name);

            if (!mAssetBundleItemDic.TryGetValue(crc, out item))
            {
                AssetBundle assetBundel = null;

                string hotAbPath = HotPatchManager.Instance.ComputeABPath(name);
                string fullpath  = string.IsNullOrEmpty(hotAbPath) ? ABLoadPath + name : hotAbPath;
                byte[] bytes     = AES.AESFileByteDecrypt(fullpath, EdgeFrameworkConst.AESKEY);
                assetBundel = AssetBundle.LoadFromMemory(bytes);

                if (assetBundel == null)
                {
                    Debug.LogError("Load AssetBundle Error:" + fullpath);
                }

                item    = mAssetBundleItemPool.Allocate();
                item.AB = assetBundel;
                item.ReCount++;
                mAssetBundleItemDic.Add(crc, item);
            }
            else
            {
                item.ReCount++;
            }
            return(item.AB);
        }
Example #29
0
    /// <summary>
    /// 加载单个assetbundle根据名字
    /// </summary>
    /// <param name="abName"></param>
    /// <returns></returns>
    private AssetBundle LoadAssetBundle(string abName)
    {
        AssetBundleItem item = null;
        uint            crc  = CRC32.GetCRC32(abName);

        if (!m_AssetBundleItemDic.TryGetValue(crc, out item))
        {
            AssetBundle assetBundle = null;
            string      fullPath    = ABLoadPath + abName;
            assetBundle = AssetBundle.LoadFromFile(fullPath);
            if (assetBundle == null)
            {
                Debug.LogError(" Load AssetBundle Error : " + fullPath);
            }

            item             = m_AssetBundleItemPool.Spawn(true);
            item.assetBundle = assetBundle;
            item.RefCount++;
            m_AssetBundleItemDic.Add(crc, item);
        }
        else
        {
            item.RefCount++;
        }
        return(item.assetBundle);
    }
Example #30
0
    void TestLoad()
    {
        AssetBundle       assetBundleconfig = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/assetbundleconfig");
        TextAsset         textAsset         = assetBundleconfig.LoadAsset <TextAsset>("AssetBundleConfig");
        MemoryStream      stream            = new MemoryStream(textAsset.bytes);
        BinaryFormatter   bf           = new BinaryFormatter();
        AssetBundleConfig testserilize = (AssetBundleConfig)bf.Deserialize(stream);

        stream.Close();
        string path   = "Assets/Prefabs/Attack.prefab";
        string crc    = CRC32.GetCRC32(path).ToString();
        ABBase abbase = null;

        for (int i = 0; i < testserilize.ABList.Count; i++)
        {
            if (testserilize.ABList[i].Crc == crc)
            {
                abbase = testserilize.ABList[i];
            }
        }
        for (int i = 0; i < abbase.ABDependce.Count; i++)
        {
            AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abbase.ABDependce[i]);
        }
        AssetBundle assetbundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abbase.ABName);
        GameObject  obj         = GameObject.Instantiate(assetbundle.LoadAsset <GameObject>(abbase.AssetName));
    }