protected override IResourceLocation[] SetupLocations(KeyValuePair <string, string>[] assets)
        {
            Random.InitState(0);
            virtualBundleData     = new VirtualAssetBundleRuntimeData();
            sharedBundleLocations = new List <IResourceLocation>();
            bundleMap             = new Dictionary <string, VirtualAssetBundle>();
            for (int i = 0; i < kBundleCount; i++)
            {
                var bundleName = "shared" + i;
                var b          = new VirtualAssetBundle("shared" + i, i % 2 == 0, 0, "");
                virtualBundleData.AssetBundles.Add(b);
                bundleMap.Add(b.Name, b);
                sharedBundleLocations.Add(new ResourceLocationBase(bundleName, bundleName, typeof(AssetBundleProvider).FullName, typeof(IAssetBundleResource)));
            }

            IResourceLocation[] locs = new IResourceLocation[assets.Length];
            for (int i = 0; i < locs.Length; i++)
            {
                locs[i] = CreateLocationForAsset(assets[i].Key, assets[i].Value);
            }

            foreach (var b in virtualBundleData.AssetBundles)
            {
                b.SetSize(2048, 1024);
                b.OnAfterDeserialize();
            }
            m_ResourceManager.ResourceProviders.Insert(0, new VirtualAssetBundleProvider(virtualBundleData));
            m_ResourceManager.ResourceProviders.Insert(0, new VirtualBundledAssetProvider());
            return(locs);
        }
        private List <ResourceLocationBase> CreateBundleSet(VirtualAssetBundleRuntimeData data, int count, bool local, int dataSize, int headerSize)
        {
            List <ResourceLocationBase> locations = new List <ResourceLocationBase>();

            for (int i = 0; i < count; i++)
            {
                locations.Add(AddNewBundle(data, "bundle" + i.ToString(), local, dataSize, headerSize));
            }
            return(locations);
        }
        private ResourceLocationBase AddNewBundle(VirtualAssetBundleRuntimeData data, string bundleName, bool local, int dataSize, int headerSize)
        {
            VirtualAssetBundle bundle = new VirtualAssetBundle(bundleName, local, 0, "");

            bundle.SetSize(dataSize, headerSize);
            data.AssetBundles.Add(bundle);
            ResourceLocationBase bundleLocation = new ResourceLocationBase(bundleName, bundleName, typeof(AssetBundleProvider).FullName, typeof(IAssetBundleResource));

            bundle.SetSize(dataSize, headerSize);
            return(bundleLocation);
        }
        public void WhenMultipleBundlesLoading_BandwidthIsAmortizedAcrossAllBundles([Values(true, false)] bool localBundles)
        {
            const int   kBundleCount = 4;
            const int   kLocalBW     = 800;
            const int   kRemoteBW    = 2000;
            const int   kHeaderSize  = 2000;
            const int   kDataSize    = 4000;
            const float kTimeSlize   = 0.5f;

            int kUpdatesForRemoteDownload = (int)Math.Ceiling((((kDataSize * kBundleCount) / kRemoteBW) / kTimeSlize));
            int kUpdatesLocalLoad         = (int)Math.Ceiling((((kHeaderSize * kBundleCount) / kLocalBW) / kTimeSlize));

            ResourceManager rm = new ResourceManager();

            rm.CallbackHooksEnabled = false;

            VirtualAssetBundleRuntimeData data      = new VirtualAssetBundleRuntimeData(kLocalBW, kRemoteBW);
            List <ResourceLocationBase>   locations = CreateBundleSet(data, kBundleCount, localBundles, kDataSize, kHeaderSize);
            VirtualAssetBundleProvider    provider  = new VirtualAssetBundleProvider(data);

            rm.ResourceProviders.Add(provider);
            var ops = new List <AsyncOperationHandle <VirtualAssetBundle> >();

            foreach (IResourceLocation loc in locations)
            {
                ops.Add(rm.ProvideResource <VirtualAssetBundle>(loc));
            }


            int totalUpdatesNeeded = kUpdatesLocalLoad + (localBundles ? 0 : kUpdatesForRemoteDownload);

            for (int i = 0; i < totalUpdatesNeeded; i++)
            {
                foreach (AsyncOperationHandle <VirtualAssetBundle> op in ops)
                {
                    Assert.IsFalse(op.IsDone);
                    Assert.Less(op.PercentComplete, 1.0f);
                }
                provider.Update(kTimeSlize);
            }

            foreach (var op in ops)
            {
                Assert.IsTrue(op.IsDone);
                Assert.AreEqual(1.0f, op.PercentComplete);
                Assert.AreEqual(AsyncOperationStatus.Succeeded, op.Status);
                op.Release();
            }

            Assert.Zero(rm.OperationCacheCount);
            rm.Dispose();
        }
    protected override void CreateLocations(List<IResourceLocation> locations)
    {
        ResourceManager.InstanceProvider = new InstanceProvider();
        ResourceManager.SceneProvider = new SceneProvider();
        var virtualBundleData = new VirtualAssetBundleRuntimeData();

        var sharedBundles = new List<VirtualAssetBundle>();
        var sharedBundleLocations = new List<IResourceLocation>();
        for (int i = 0; i < 10; i++)
        {
            var bundleName = "shared" + i;
            sharedBundles.Add(new VirtualAssetBundle("shared" + i, i % 2 == 0));
            sharedBundleLocations.Add(new ResourceLocationBase(bundleName, bundleName, typeof(AssetBundleProvider).FullName));
        }
        virtualBundleData.AssetBundles.AddRange(sharedBundles);

        for (int i = 0; i < 5; i++)
        {
            var isLocal = i % 2 == 0;
            var b = new VirtualAssetBundle("bundle" + i, isLocal);
            var bundleLocation = new ResourceLocationBase(b.Name, b.Name, typeof(AssetBundleProvider).FullName);
            for (int a = 0; a < 10; a++)
            {
                var name = b.Name + "_asset" + a;
                var path = RootFolder + "/" + name + ".prefab";
                GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                go.name = name;

        #if UNITY_2018_3_OR_NEWER
                PrefabUtility.SaveAsPrefabAsset(go, path);
        #else
                PrefabUtility.CreatePrefab(path, go);
        #endif
                Object.DestroyImmediate(go, false);

                var asset = new VirtualAssetBundleEntry(path, Random.Range(1024, 1024 * 1024));
                b.Assets.Add(asset);
                locations.Add(new ResourceLocationBase(name, path, typeof(BundledAssetProvider).FullName, bundleLocation, sharedBundleLocations[Random.Range(0, sharedBundleLocations.Count)], sharedBundleLocations[Random.Range(0, sharedBundleLocations.Count)]));
            }
            b.OnAfterDeserialize();
            virtualBundleData.AssetBundles.Add(b);
        }

        var abManager = new GameObject("AssetBundleSimulator", typeof(VirtualAssetBundleManager)).GetComponent<VirtualAssetBundleManager>();
        abManager.Initialize(virtualBundleData, s => s);
        ResourceManager.ResourceProviders.Insert(0, new CachedProvider(new VirtualAssetBundleProvider(abManager, typeof(AssetBundleProvider).FullName)));
        ResourceManager.ResourceProviders.Insert(0, new CachedProvider(new VirtualBundledAssetProvider()));
    }
        public IEnumerator WhenLoadingUnknownBundle_OperationFailsWithMessage()
        {
            ResourceManager rm = new ResourceManager();

            rm.CallbackHooksEnabled = false;

            VirtualAssetBundleRuntimeData data     = new VirtualAssetBundleRuntimeData();
            VirtualAssetBundleProvider    provider = new VirtualAssetBundleProvider(data);

            rm.ResourceProviders.Add(provider);
            ResourceLocationBase unknownLocation = new ResourceLocationBase("unknown", "unknown", typeof(AssetBundleProvider).FullName, typeof(IAssetBundleResource));
            var op = rm.ProvideResource <VirtualAssetBundle>(unknownLocation);

            // wait for delayed action manager.
            // TODO: refactor delayed action manager so we can pump it instead of waiting a frame
            yield return(null);

            Assert.AreEqual(AsyncOperationStatus.Failed, op.Status);
            StringAssert.Contains("Unable to unload virtual bundle", op.OperationException.Message);
            op.Release();

            Assert.Zero(rm.OperationCacheCount);
            rm.Dispose();
        }
Exemplo n.º 7
0
        /// <inheritdoc />
        protected override string ProcessGroup(AddressableAssetGroup assetGroup, AddressableAssetsBuildContext aaContext)
        {
            var errorString = string.Empty;
            PlayerDataGroupSchema playerSchema = assetGroup.GetSchema <PlayerDataGroupSchema>();

            if (playerSchema != null)
            {
                if (CreateLocationsForPlayerData(playerSchema, assetGroup, aaContext.locations, aaContext.providerTypes))
                {
                    if (!m_CreatedProviderIds.ContainsKey(typeof(LegacyResourcesProvider).Name))
                    {
                        m_CreatedProviderIds.Add(typeof(LegacyResourcesProvider).Name, null);
                        m_ResourceProviderData.Add(ObjectInitializationData.CreateSerializedInitializationData(typeof(LegacyResourcesProvider)));
                    }
                }
                return(errorString);
            }

            var schema = assetGroup.GetSchema <BundledAssetGroupSchema>();

            if (schema == null)
            {
                return(errorString);
            }

            var bundledProviderId = schema.GetBundleCachedProviderId();
            var assetProviderId   = schema.GetAssetCachedProviderId();

            if (!m_CreatedProviderIds.ContainsKey(bundledProviderId))
            {
                //TODO: pull from schema instead of ProjectConfigData
                var virtualBundleRuntimeData = new VirtualAssetBundleRuntimeData(ProjectConfigData.localLoadSpeed, ProjectConfigData.remoteLoadSpeed);
                //save virtual runtime data to collect assets into virtual bundles
                m_CreatedProviderIds.Add(bundledProviderId, virtualBundleRuntimeData);
            }

            if (!m_CreatedProviderIds.ContainsKey(assetProviderId))
            {
                m_CreatedProviderIds.Add(assetProviderId, null);

                var assetProviderData = ObjectInitializationData.CreateSerializedInitializationData <VirtualBundledAssetProvider>(assetProviderId);
                m_ResourceProviderData.Add(assetProviderData);
            }


            var bundleInputDefs = new List <AssetBundleBuild>();

            BuildScriptPackedMode.PrepGroupBundlePacking(assetGroup, bundleInputDefs, schema.BundleMode);
            for (int i = 0; i < bundleInputDefs.Count; i++)
            {
                if (aaContext.bundleToAssetGroup.ContainsKey(bundleInputDefs[i].assetBundleName))
                {
                    var bid     = bundleInputDefs[i];
                    int count   = 1;
                    var newName = bid.assetBundleName;
                    while (aaContext.bundleToAssetGroup.ContainsKey(newName) && count < 1000)
                    {
                        newName = bid.assetBundleName.Replace(".bundle", string.Format("{0}.bundle", count++));
                    }
                    bundleInputDefs[i] = new AssetBundleBuild {
                        assetBundleName = newName, addressableNames = bid.addressableNames, assetBundleVariant = bid.assetBundleVariant, assetNames = bid.assetNames
                    };
                }

                aaContext.bundleToAssetGroup.Add(bundleInputDefs[i].assetBundleName, assetGroup.Guid);
            }

            m_AllBundleInputDefinitions.AddRange(bundleInputDefs);

            return(errorString);
        }