示例#1
0
    //private static Dictionary<string,AddressableAssetGroup> groupDict = new Dictionary<string, AddressableAssetGroup>();

    public static void CreatConfig()
    {
        AddressablesRules rules = new AddressablesRules();
        Dictionary <string, BuildAddressablesData> bundles = rules.GetBuilds();

        CreatAssetViewConst(bundles);
        AssetDatabase.ImportAsset("Assets/Bundles/AddressablesRules.txt", ImportAssetOptions.ForceUpdate);
        AssetDatabase.Refresh();
    }
示例#2
0
    public static void AddFileToAddressables()
    {
        AddressablesRules rules = new AddressablesRules();
        Dictionary <string, BuildAddressablesData> bundles = rules.GetBuilds();
        AddressableAssetSettings aaSettings = AddressableAssetSettingsDefaultObject.GetSettings(false);
        AddressableAssetGroup    group      = null;

        //清理错误group
        for (int i = aaSettings.groups.Count - 1; i >= 0; i--)
        {
            var g = aaSettings.groups[i];
            if (g == null || g.entries.Count == 0)
            {
                aaSettings.RemoveGroup(g);
                g = null;
                continue;
            }
            //
            // if (g != null)
            // {
            //     g.Name = $"{g.Name}_delete";
            // }
        }

        foreach (string key in bundles.Keys)
        {
            group = aaSettings.groups.Find(x => x.Name == bundles[key].GroupName);
            if (group == null)
            {
                group = aaSettings.CreateGroup(bundles[key].GroupName, false, false, false, null);
            }

            BundledAssetGroupSchema schema = group.GetSchema <BundledAssetGroupSchema>();
            if (schema == null)
            {
                schema = group.AddSchema <BundledAssetGroupSchema>();
            }

            ContentUpdateGroupSchema contentUpdateGroupSchema = group.GetSchema <ContentUpdateGroupSchema>();
            if (contentUpdateGroupSchema == null)
            {
                contentUpdateGroupSchema = group.AddSchema <ContentUpdateGroupSchema>();
            }

            schema.Compression = BundledAssetGroupSchema.BundleCompressionMode.LZ4;
            if (bundles[key].packageType == "PackSeparately")
            {
                schema.BundleMode = BundledAssetGroupSchema.BundlePackingMode.PackSeparately;
            }
            else if (bundles[key].packageType == "PackTogether")
            {
                schema.BundleMode = BundledAssetGroupSchema.BundlePackingMode.PackTogether;
            }
            else if (bundles[key].packageType == "PackTogetherByLabel")
            {
                schema.BundleMode = BundledAssetGroupSchema.BundlePackingMode.PackTogetherByLabel;
            }

            schema.IncludeInBuild      = true;
            schema.UseAssetBundleCache = true;
            schema.UseAssetBundleCrc   = false;
            schema.UseAssetBundleCrcForCachedBundles = false;

            if (bundles[key].ResType == "online")
            {
                schema.BuildPath.SetVariableByName(group.Settings, AddressableAssetSettings.kRemoteBuildPath);
                schema.LoadPath.SetVariableByName(group.Settings, AddressableAssetSettings.kRemoteLoadPath);
                schema.BundleNaming      = BundledAssetGroupSchema.BundleNamingStyle.AppendHash;
                schema.UseAssetBundleCrc = true;
                contentUpdateGroupSchema.StaticContent = false;
            }
            else
            {
                schema.BuildPath.SetVariableByName(group.Settings, AddressableAssetSettings.kLocalBuildPath);
                schema.LoadPath.SetVariableByName(group.Settings, AddressableAssetSettings.kLocalLoadPath);
                schema.BundleNaming = BundledAssetGroupSchema.BundleNamingStyle.NoHash;
                contentUpdateGroupSchema.StaticContent = true;
            }
        }

        foreach (string key in bundles.Keys)
        {
            int count    = 0;
            int MaxCount = bundles[key].entitys.Count;
            group = aaSettings.groups.Find(x => x.Name == bundles[key].GroupName);
            foreach (string entitysKey in bundles[key].entitys.Keys)
            {
                count++;
                if (count % 3 == 0)
                {
                    if (UnityEditor.EditorUtility.DisplayCancelableProgressBar(string.Format("Collecting... [{0}/{1}]", count, MaxCount), entitysKey,
                                                                               count * 1f / MaxCount))
                    {
                        break;
                    }
                }

                string guid = AssetDatabase.AssetPathToGUID(bundles[key].entitys[entitysKey].filestring);
                AddressableAssetEntry entity = aaSettings.CreateOrMoveEntry(guid, group);
                entity.SetAddress(entitysKey);
                for (int i = 0; i < bundles[key].Lable.Length; i++)
                {
                    if (bundles[key].Lable[i].Contains("#"))
                    {
                        if (!string.IsNullOrEmpty(bundles[key].entitys[entitysKey].folderlabel))
                        {
                            aaSettings.AddLabel(bundles[key].entitys[entitysKey].folderlabel);
                            entity.SetLabel(bundles[key].entitys[entitysKey].folderlabel, true);
                        }
                    }
                    else
                    {
                        aaSettings.AddLabel(bundles[key].Lable[i]);
                        entity.SetLabel(bundles[key].Lable[i], true);
                    }
                }
            }
        }
        // for (int i = aaSettings.groups.Count - 1; i >= 0; i--)
        // {
        //     var g = aaSettings.groups[i];
        //     if (g == null || g.entries.Count == 0)
        //     {
        //         aaSettings.RemoveGroup(g);
        //         g = null;
        //         continue;
        //     }
        //
        //     if (g != null)
        //     {
        //         if (g.Name.Contains("_delete"))
        //         {
        //             aaSettings.RemoveGroup(g);
        //             g = null;
        //         }
        //     }
        // }
        UnityEditor.EditorUtility.ClearProgressBar();
    }