public static void ExpandTextTemplateInScripts() { var log = SelectionExtensions.GetSelectingScriptAssetPath() .Aggregate("", (_s, _c) => _s + _c + ";"); Debug.Log($"selecting scripts: {log}"); foreach (var assetPath in SelectionExtensions.GetSelectingScriptAssetPath()) { try { var textAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(assetPath); var(text, isSuccess) = ExpandTextTemplate(textAsset.text); if (isSuccess) { var filepath = EditorFileUtils.GetFullFilepath(assetPath); File.WriteAllText(filepath, text); AssetDatabase.ImportAsset(assetPath); } } catch (System.Exception) { Debug.LogWarning($"Failed to Expand TextTemplate in {assetPath}..."); } } }
/// <summary> /// /// </summary> /// <param name="keyword"></param> /// <param name="filepath"></param> /// <param name="expandText"></param> /// <returns></returns> bool SearchExpandText(string keyword, string filepath, out string expandText) { expandText = null; if (TraitsTemplates.ContainsKey(keyword)) {//case: use buildin template var template = TraitsTemplates[keyword]; expandText = RemoveIndents(template, 0, template.Length); return(true); } var fullFilepath = EditorFileUtils.GetFullFilepath(filepath); if (!File.Exists(fullFilepath)) { Logger.LogWarning(Logger.Priority.High, () => $"{LOG_PREFIX}Not exist filepath('{fullFilepath}')...", LOG_SELECTOR); return(false); } var text = File.ReadAllText(fullFilepath); // find traits define to match 'keyword' var defineTraitsEnumerable = REGEX_SearchDefineTraits.GetMatchEnumerable(text) .Where(_m => { return(_m.Groups[2].ToString() == keyword); }); var match = defineTraitsEnumerable.FirstOrDefault(); if (match == null) { Logger.LogWarning(Logger.Priority.High, () => $"{LOG_PREFIX}Not define Traits({keyword}) in filepath({filepath})...", LOG_SELECTOR); expandText = null; return(false); } //check to exist traits define endpoint var endMatch = REGEX_SearchDefineTraitsEnd.GetMatchEnumerable(text, match.Index + match.Length) .Where(_m => _m.Groups[2].ToString() == keyword) .FirstOrDefault(); if (endMatch == null) { Logger.LogWarning(Logger.Priority.High, () => $"{LOG_PREFIX}Not exist Traits({keyword}) terminal in filepath({filepath})...", LOG_SELECTOR); expandText = null; return(false); } // check to duplicate define if (defineTraitsEnumerable.Count() >= 2) { Logger.LogWarning(Logger.Priority.High, () => { return($"{LOG_PREFIX}Traits({keyword}) define Duplicate..."); }, LOG_SELECTOR); expandText = null; return(false); } //remove indents in traits's define expandText = RemoveIndents(text, text.GoNext('\n', match.Index + match.Length, 1), endMatch.Index); return(true); }
Texture2D GetTexture(string filepath) { if (_usedFilepath == filepath) { return(_tex); } if (EditorFileUtils.IsExistAsset(filepath)) {//Assetの時 var tex = AssetDatabase.LoadAssetAtPath <Texture2D>(filepath); if (tex == null) { return(null); } _tex = tex; _usedFilepath = filepath; return(_tex); } else {//Assetではない場合 if (!File.Exists(filepath)) { _tex = null; return(null); } if (_tex != null) { return(_tex); } var tex = new Texture2D(4, 4); var bytes = File.ReadAllBytes(filepath); if (tex.LoadImage(bytes)) { _tex = tex; _usedFilepath = filepath; } return(_tex); } }