public string GetABNameByPath(string filePath) { if (!_pathABNameRelation.ContainsKey(filePath)) { _pathABNameRelation[filePath] = BundlePackRule.CalculateBundleNameByPath(filePath, _bundleRules); } return(_pathABNameRelation[filePath]); }
//allRules: 会遍历所有规则找到第一个匹配的规则,因此子目录的规则应该排在父目录规则前面 public static string CalculateBundleNameByPath(string path, List <BundlePackRule> allRules) { BundlePackRule matchRule = DefaultRule; foreach (BundlePackRule bundleRule in allRules) { if (path.StartsWith(bundleRule.directory)) { matchRule = bundleRule; break; } } //switch (matchRule.rule) switch (BundlePackRuleDefine.PackOneByOne) { case BundlePackRuleDefine.PackOneByOne: #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN return(path.Replace("\\", "/").Replace('/', Defines.ABNameSeparatorChar).ToLower()); #else return(path.Replace('/', Defines.ABNameSeparatorChar).ToLower()); #endif case BundlePackRuleDefine.PackToParentFolder: #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN return(Path.GetDirectoryName(path).Replace("\\", "/").Replace('/', Defines.ABNameSeparatorChar).ToLower()); #else return(Path.GetDirectoryName(path).Replace('/', Defines.ABNameSeparatorChar).ToLower()); #endif case BundlePackRuleDefine.PackToRootFolder: #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN return(matchRule.directory.Trim('/').Replace("\\", "/").Replace('/', Defines.ABNameSeparatorChar).ToLower()); #else return(matchRule.directory.Trim('/').Replace('/', Defines.ABNameSeparatorChar).ToLower()); #endif case BundlePackRuleDefine.PackToSelfFolderByUnderline: string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path); string[] sArray = fileNameWithoutExtension.Split('_'); #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN return(string.Join("_", Path.GetDirectoryName(path).Replace("\\", "/").Replace('/', Defines.ABNameSeparatorChar), sArray[0]).ToLower()); #else return(string.Join("_", Path.GetDirectoryName(path).Replace('/', Defines.ABNameSeparatorChar), sArray[0]).ToLower()); #endif case BundlePackRuleDefine.DoNotPack: return(null); } return(null); }
// AssetBoundleSourcePath/a/b.txt 应该传入参数 a.b 同 resource.load一样 ,不用加后缀 public static string CalABNameByPath(string path) { string _name = CalArtABNameByPath(path); if (!string.IsNullOrEmpty(_name)) { return(_name); } var parentPath = Path.GetFullPath(Defines.AssetBundleSourcePath); path = Path.GetFullPath(path); if (!path.StartsWith(parentPath)) { return(""); } var subPath = path.Substring(parentPath.Length + 1, path.Length - parentPath.Length - 1); subPath = Path.Combine(Path.GetDirectoryName(subPath), Path.GetFileNameWithoutExtension(subPath)); subPath = subPath.Replace('\\', '/'); path = subPath; var abName = BundlePackRule.CalculateBundleNameByPath(path, _allRules); if (string.IsNullOrEmpty(abName)) { return(""); //不返回null,因为未命名的asset其assetImporter 返为"" } else { var suffix = Defines.AssetBoundleSuffix; if (!string.IsNullOrEmpty(suffix)) { suffix = "." + suffix; } return(abName + suffix); } }
public void AddBundleRule(BundlePackRule rule) { _bundleRules.Add(rule); }
//Art目录只设置Effect/Tex public static string CalArtABNameByPath(string path) { var parentPath = Path.GetFullPath(Defines.AssetArtEffectSourcePath); path = Path.GetFullPath(path); if (!path.StartsWith(parentPath)) { return(""); } if (path.EndsWith(".mat") || path.EndsWith(".png")) { } else { return(""); } var appPath = Path.GetFullPath(Application.dataPath); string src_guid = AssetDatabase.AssetPathToGUID("Assets/" + path.Substring(appPath.Length + 1, path.Length - appPath.Length - 1)); var subPath = path.Substring(parentPath.Length + 1, path.Length - parentPath.Length - 1); subPath = Path.Combine(Path.GetDirectoryName(subPath), Path.GetFileNameWithoutExtension(subPath)); subPath = subPath.Replace('\\', '/'); var abName = BundlePackRule.CalculateBundleNameByPath(subPath, _allRules); if (string.IsNullOrEmpty(abName)) { return(""); //不返回null,因为未命名的asset其assetImporter 返为"" } else { if (!_artTexCollected) { var prefabFiles = Directory.GetFiles(Defines.AssetArtEffectPrefabSourcePath, "*.*", SearchOption.AllDirectories); foreach (var file in prefabFiles) { string[] dependenciesFiles = AssetDatabase.GetDependencies(file, true); foreach (var name in dependenciesFiles) { if (name.StartsWith(Defines.AssetArtEffectTexSourcePath) && (name.EndsWith(".mat") || name.EndsWith(".png"))) { string guid = AssetDatabase.AssetPathToGUID(name); if (!_artTex.ContainsKey(guid)) { _artTex[guid] = 1; } else { ++_artTex[guid]; } } } } _artTexCollected = true; } if (!_artTex.ContainsKey(src_guid) || _artTex[src_guid] <= 1) { return(""); } var suffix = Defines.AssetBoundleSuffix; if (!string.IsNullOrEmpty(suffix)) { suffix = "." + suffix; } return(abName + suffix); } }