public static bool RemoveFromAtlas(
            AtlasGeneratorRule rule,
            string movedFromAssetPath,
            string assetPath)
        {
            var pathToAtlas = rule.ParseAtlasReplacement(movedFromAssetPath == null ? assetPath : movedFromAssetPath);

            pathToAtlas = rule.GetFullPathToAtlas(pathToAtlas);
            if (!TryGetAtlas(pathToAtlas, out var atlas))
            {
                Log(LogType.Warning, $"Failed to find atlas {rule.pathToAtlas} when removing {assetPath} from it");
                return(false);
            }
            atlas.RemoveSprite(assetPath);
            return(DeleteEmptyAtlas(atlas));
        }
        public static SpriteAtlas CreateOrUpdateAtlas(
            AtlasGeneratorSettings generatorSettings,
            AtlasGeneratorAtlasSettings atlasSettings,
            AtlasGeneratorRule rule,
            string assetPath)
        {
            // Set atlas
            SpriteAtlas atlas;
            var         pathToAtlas = rule.ParseAtlasReplacement(assetPath);

            pathToAtlas = rule.GetFullPathToAtlas(pathToAtlas);

            var newAtlas = false;

            if (string.IsNullOrWhiteSpace(pathToAtlas))
            {
                Log(LogType.Warning, $"Asset {assetPath} wasn't packed because its rule has no atlas path");
                return(null);
            }

            var appliedSettings = rule.applyCustomSettings ? rule.atlasSettings : atlasSettings.defaultAtlasSettings;

            if (!TryGetAtlas(pathToAtlas, out atlas))
            {
                atlas    = CreateAtlas(pathToAtlas, appliedSettings);
                newAtlas = true;
            }

            // Set atlas settings from template if necessary
            if (!newAtlas && ((rule.applyCustomSettings && rule.atlasSettingsApplicationMode == AtlasSettingsApplicationMode.AlwaysOverwriteAtlasSettings) ||
                              atlasSettings.atlasSettingseApplicationMode == AtlasSettingsApplicationMode.AlwaysOverwriteAtlasSettings))
            {
                atlas.ApplySettings(appliedSettings);
            }

            var packedAsset  = new[] { AssetDatabase.LoadAssetAtPath <Texture2D>(assetPath) };
            var packedAssets = atlas.GetPackables();

            if (!packedAssets.Contains(packedAsset[0]))
            {
                atlas.Add(packedAsset);
            }

            return(atlas);
        }
        public static bool TryGetMatchedRule(string assetPath, AtlasGeneratorSettings importSettings, out AtlasGeneratorRule rule)
        {
            if (importSettings.rules == null || importSettings.rules.Count == 0)
            {
                rule = null;
                return(false);
            }

            foreach (var r in importSettings.rules)
            {
                if (r.enabled == false)
                {
                    continue;
                }

                if (!r.Match(assetPath))
                {
                    continue;
                }

                rule = r;
                return(true);
            }

            rule = null;
            return(false);
        }