public static void SetReplaceListFromConfigValue(List <ReplaceInfo> replaces, string prefix) { foreach (var replace in replaces) { replace.ReplaceWord = TemplateUtility.GetConfigValue(prefix + replace.Key); } }
public static void RemoveChainWords(ICollection <string> words, object[] objects) { if (objects == null) { return; } var chains = new List <IProcessChain>(); foreach (var obj in objects) { var chain = TemplateUtility.ConvertProcessChianInstanceFromObject(obj); if (chain != null) { chains.Add(chain); } } foreach (var chain in chains) { foreach (var word in chain.GetReplaceWords()) { words.Remove(word); // ToArray = 遅延実行だとエラーになるため var regex = new Regex(string.Format(ProcessDictionary.ConvertWordPattern, word, @"\d+"), RegexOptions.IgnoreCase); foreach (var matchWord in words.Where(w => regex.IsMatch(w)).ToArray()) { words.Remove(matchWord); } } } }
public void Process(ProcessMetadata metadata, ProcessDictionary result) { var typeNameList = new List <string[]>(); var elementNameList = new List <string[]>(); var nameList = new HashSet <string>(); var targetNameList = new List <string>(); foreach (var target in _targets) { if (target == null) { continue; } GetNames(target.CloneTree().Children(), nameList); var tab = new string(' ', 8); typeNameList.Add(new[] { target.name, string.Join(",\n" + tab, nameList.Select(StringBuilderExtension.ConvertEnumName).ToArray()) }); elementNameList.Add(new[] { target.name, string.Join(",\n" + tab, nameList.Select(n => "\"" + n + "\"").ToArray()) }); nameList.Clear(); targetNameList.Add(target.name); } result.Add(ReplaceWords[0], TemplateUtility.GetFilePathFromFileName(ScriptName + ".cs") ?? "Assets"); result.Add(ReplaceWords[1], ScriptName); result.Add(ReplaceWords[2], typeNameList); result.Add(ReplaceWords[3], elementNameList); result.Add(ReplaceWords[4], targetNameList); }
public void Process(ProcessMetadata metadata, ProcessDictionary result) { var createPath = TemplateUtility.GetFilePathFromFileName(AssetsMenuItemScriptName + ".cs") ?? "Assets/TemplateEditorTool/Editor"; result.Add(ReplaceWords[(int)ReplaceWordType.Settings], BuildMenuItemList <TemplateSetting>()); result.Add(ReplaceWords[(int)ReplaceWordType.GroupSettings], BuildMenuItemList <TemplateGroupSetting>()); result.Add(ReplaceWords[(int)ReplaceWordType.CreatePath], createPath); result.Add(ReplaceWords[(int)ReplaceWordType.ScriptName], AssetsMenuItemScriptName); }
public static void ExecuteChain(TemplateSettingStatus status, ProcessDictionary result) { var metadata = new ProcessMetadata(status.TargetTemplateSetting); var property = status.GetProperty(TemplateSettingStatus.Property.Chain); for (int i = 0; i < property.arraySize; ++i) { TemplateUtility.ExecuteProcessChain(property.GetArrayElementAtIndex(i).objectReferenceValue, metadata, result); } }
public static void CreateScript(TemplateSettingStatus status, List <ReplaceInfo> replaces, ProcessDictionary result = null, bool isRefresh = true) { if (result == null) { result = new ProcessDictionary(); } if (replaces != null) { foreach (var replace in replaces) { if (string.IsNullOrEmpty(replace.ReplaceWord)) { continue; } result[replace.Key] = replace.ReplaceWord; } } ExecuteChain(status, result); // 生成ディレクトリが指定されていなければアクティブなパスへ作成 var createDirectory = status.GetProperty(TemplateSettingStatus.Property.Path).stringValue; var createPath = Path.Combine( string.IsNullOrEmpty(createDirectory) == false ? createDirectory : TemplateUtility.GetActiveFolder(), status.GetProperty(TemplateSettingStatus.Property.ScriptName).stringValue ); var code = status.GetProperty(TemplateSettingStatus.Property.Code).stringValue; var path = Replace(createPath, result); TemplateUtility.CreateScript( path, Replace(code, result), (TemplateUtility.OverwriteType)status.GetProperty(TemplateSettingStatus.Property.Overwrite).enumValueIndex ); if (isRefresh) { AssetDatabase.ImportAsset(path); TemplateUtility.RefreshEditor(); } // プレハブ生成登録 var prefabObject = status.GetProperty(TemplateSettingStatus.Property.DuplicatePrefab).objectReferenceValue as GameObject; var targetObject = status.GetProperty(TemplateSettingStatus.Property.AttachTarget).objectReferenceValue as GameObject; if (prefabObject != null && targetObject != null) { TemplatePrefabCreator.AddTempCreatePrefabSetting(status.TargetTemplateSetting, path); } }
public static void DrawHeader(TemplateSettingStatus status) { EditorGUI.BeginChangeCheck(); { // setting create path EditorGUILayout.BeginVertical(EditorGUIHelper.GetScopeStyle()); { var property = status.GetProperty(TemplateSettingStatus.Property.Path); EditorGUILayout.PropertyField(property, new GUIContent("Create Path")); var paths = EditorGUIHelper.DrawDragAndDropArea(); if (paths != null && paths.Length > 0) { // Index 0 のパスを使用する property.stringValue = TemplateUtility.GetDirectoryPath(paths[0]); } var createPath = status.TargetTemplateSetting.Path; if (string.IsNullOrEmpty(createPath)) { EditorGUILayout.HelpBox("If empty, the script will be created in active folder", MessageType.Info); } EditorGUILayout.HelpBox("Example: Assets/Folder", MessageType.Info); } EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(EditorGUIHelper.GetScopeStyle()); { EditorGUILayout.PropertyField(status.GetProperty(TemplateSettingStatus.Property.ScriptName), new GUIContent("Script Name")); if (string.IsNullOrEmpty(status.TargetTemplateSetting.ScriptName)) { EditorGUILayout.HelpBox("Example: Example.cs", MessageType.Info); } else if (Regex.IsMatch(status.TargetTemplateSetting.ScriptName, @"\..+$", RegexOptions.Compiled) == false) { EditorGUILayout.HelpBox("Extension required", MessageType.Warning); } } EditorGUILayout.EndVertical(); } if (EditorGUI.EndChangeCheck()) { status.IsUpdateText = true; Undo.IncrementCurrentGroup(); } }
private void OnGUI() { if (_targetListProperty == null) { return; } _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos); { _serializedObject.Update(); EditorGUILayout.PropertyField(_targetListProperty, new GUIContent("Target VisualTreeAssets"), true); _serializedObject.ApplyModifiedProperties(); } EditorGUILayout.EndScrollView(); if (GUILayout.Button("Create")) { TemplateUtility.ExecuteSetting(VisualTreeNameTableGuid); } }
private List <string[]> BuildMenuItemList <T>() where T : UnityEngine.Object { var list = new List <string[]>(); foreach (var guid in TemplateUtility.FindAssetGuids(typeof(T))) { var path = AssetDatabase.GUIDToAssetPath(guid); var setting = AssetDatabase.LoadAssetAtPath <T>(path) as IAssetsMenuItem; if (setting == null || setting.IsAssetsMenuItem == false) { continue; } var name = Path.GetFileNameWithoutExtension(path); var array = new string[2]; array[0] = name; array[1] = guid; list.Add(array); } return(list); }
public static void DrawChain(TemplateSettingStatus status) { EditorGUILayout.BeginVertical(EditorGUIHelper.GetScopeStyle()); { status.ChainReorderableList.DoLayoutList(); var selectIndex = status.ChainReorderableList.index; if (selectIndex >= 0) { var select = status.ChainReorderableList.serializedProperty.GetArrayElementAtIndex(selectIndex); var chain = TemplateUtility.ConvertProcessChianInstanceFromObject(select.objectReferenceValue); if (chain != null) { var builder = new StringBuilder(); builder.AppendLine("[Used Variables]"); foreach (var word in chain.GetReplaceWords()) { builder.AppendLine(ReplaceProcessor.GetReplaceText(word)); } // TODO : Cache var style = new GUIStyle(GUI.skin.label) { wordWrap = true, }; var label = builder.ToString(); var content = new GUIContent(label); var rect = GUILayoutUtility.GetRect(content, style); EditorGUI.SelectableLabel(rect, label, style); EditorGUILayout.LabelField("[Description]\n" + chain.GetDescription(), style); } } else { EditorGUILayout.HelpBox("When you select item, description will be displayed", MessageType.Info, true); } } EditorGUILayout.EndVertical(); }
private void OnGUI() { _targetScriptableObject = EditorGUILayout.ObjectField(_targetScriptableObject, typeof(ScriptableObject), false) as ScriptableObject; if (_targetScriptableObject == null) { return; } if (GUILayout.Button("生成") == false) { return; } var empty = AssetDatabase.LoadAssetAtPath <ScriptableObject>(AssetDatabase.GUIDToAssetPath(EmptyScriptableObjectGuid)); var emptySerializeNames = GetSerializeNames(empty); var targetSerializeNames = GetSerializeNames(_targetScriptableObject); var targetUseSerializeNames = targetSerializeNames.Where(n => emptySerializeNames.Contains(n) == false).ToArray(); var type = _targetScriptableObject.GetType(); var targetName = type.Name; var path = Path.GetDirectoryName(AssetDatabase.GetAssetPath(_targetScriptableObject)); var customEditorName = targetName + "Editor"; var propertyGetterName = targetName + "PropertyGetter"; var result = new ProcessDictionary(); result.Add("TargetNamespace", "using " + type.Namespace + ";"); result.Add("CustomEditorCreatePath", TemplateUtility.GetFilePathFromFileName(customEditorName + ".cs") ?? path); result.Add("PropertyGetterCreatePath", TemplateUtility.GetFilePathFromFileName(propertyGetterName + ".cs") ?? path); result.Add("TargetScriptableObjectName", targetName); result.Add("CustomEditorName", customEditorName); result.Add("PropertyGetterName", propertyGetterName); result.Add("TypeNames", targetUseSerializeNames.Select(StringBuilderExtension.ConvertEnumName).ToArray()); result.Add("PropertyNames", targetUseSerializeNames); TemplateUtility.ExecuteGroupSetting(CustomEditorCreateGroupSettingGuid, result); }
public static void DrawReplace(List <ReplaceInfo> replaces, string savePrefix) { EditorGUILayout.BeginVertical(EditorGUIHelper.GetScopeStyle()); if (replaces.Count == 0) { EditorGUILayout.HelpBox("{<Foo>} in 'Create Path' and 'Script Name' and 'Code' will be replace\nNote: Variables used in 'Pre Process' are not listed here.", MessageType.Info); } else { foreach (var replace in replaces) { var str = EditorGUILayout.TextField(ReplaceProcessor.GetReplaceText(replace.Key), replace.ReplaceWord); if (str == replace.ReplaceWord) { continue; } // 置き換え文字をUnityへキャッシュ replace.ReplaceWord = str; TemplateUtility.SetConfigValue(savePrefix + replace.Key, replace.ReplaceWord); } } EditorGUILayout.EndVertical(); }
public void Process(ProcessMetadata metadata, ProcessDictionary result) { // ファイルパス取得 var removeWord = "Resources/"; var removeWordLength = removeWord.Length; // Assets内にあるResourcesフォルダの全てのパスを取得 var resourcesPaths = Directory.GetDirectories("Assets", "Resources", SearchOption.AllDirectories); // Resourcesフォルダ内のファイルパスを取得 // TODO : 最適化? var ignorePatterns = _ignorePathPatterns.Select(pattern => new Regex(pattern, RegexOptions.Compiled)); var filePaths = ResourcesLoaderUtility.GetFilePaths(resourcesPaths, new string[] { ".meta" }) .Where(path => _ignoreFileNames.Contains(Path.GetFileNameWithoutExtension(path)) == false) .Where(path => ignorePatterns.Any(pattern => pattern.IsMatch(path)) == false) .OrderBy(path => Path.GetFileNameWithoutExtension(path)); var filePathsList = new List <string[]>(); var fileNamesList = new List <string[]>(); var methodsList = new List <string[]>(); var enumValues = Enum.GetValues(typeof(ResourcesLoaderUtility.LoadType)).Cast <int>().ToArray(); foreach (var parameter in _parameters) { // 指定した拡張子のファイルパスを取得 var paths = ResourcesLoaderUtility.FindByExtension(filePaths, parameter.TargetExtensions).Select(path => { var startIndex = path.IndexOf(removeWord) + removeWordLength; var length = path.Length - startIndex - Path.GetExtension(path).Length; return(path.Substring(startIndex, length)); }); if (paths.Any() == false) { continue; } // スペースは8文字に var tab = new string(' ', 8); filePathsList.Add(new string[] { parameter.TypeName, string.Join(",\n" + tab, paths.Select(path => "\"" + path + "\"").ToArray()) }); // ファイルパスからファイル名を取得 // TODO : StringBuilderExtension.ConvertEnumName var fileNames = paths.Select(Path.GetFileNameWithoutExtension).Select(StringBuilderExtension.ConvertEnumName); fileNamesList.Add(new string[] { parameter.TypeName, string.Join(",\n" + tab, fileNames.ToArray()) }); var intValue = (int)parameter.EditLoadType; for (int i = 0; i < enumValues.Length; ++i) { if ((intValue & enumValues[i]) == 0) { continue; } methodsList.Add(new string[] { string.Format(ResourcesLoaderUtility.ReturnNamesFormat[i], parameter.TypeName), ResourcesLoaderUtility.LoadNames[i], parameter.TypeName }); } } // 生成パス設定 result.Add(ReplaceWords[(int)ReplaceWordType.CreatePath], TemplateUtility.GetFilePathFromFileName(ScriptName + ".cs") ?? "Assets"); // スクリプト名設定 result.Add(ReplaceWords[(int)ReplaceWordType.ScriptName], ScriptName); // Usings { var usingsList = new List <IUsings>(_parameters.Length + 1); usingsList.Add(this); usingsList.AddRange(_parameters); var usingNames = new HashSet <string>(); foreach (var usings in usingsList) { foreach (var names in usings.usings) { usingNames.Add(names); } } result.Add( ReplaceWords[(int)ReplaceWordType.Usings], usingNames.ToArray() ); } // Enums { result.Add( ReplaceWords[(int)ReplaceWordType.Enums], fileNamesList ); } // Paths { result.Add( ReplaceWords[(int)ReplaceWordType.Paths], filePathsList ); } // Methods { result.Add( ReplaceWords[(int)ReplaceWordType.Methods], methodsList ); } }
public static void Execute() { TemplateUtility.ExecuteSetting(ResourcesLoaderTemplateGuid); }
public static void Execute() { TemplateUtility.OpenEditorWindow(UnityCSharpTemplateGuid); }
private static void DuplicatePrefab(TemplateSetting[] settings) { if (settings == null || settings.Length == 0) { Debug.LogWarning("設定ファイルがありません"); return; } var components = new List <Component>(settings.Length); foreach (var setting in settings) { // Load Attach Script var scriptKye = string.Format(TempCreateScriptPathKeyFormat, setting.GetInstanceID()); var scriptPath = SessionState.GetString(scriptKye, null); var mono = AssetDatabase.LoadAssetAtPath <MonoScript>(scriptPath); SessionState.EraseString(scriptKye); if (mono == null) { Debug.LogErrorFormat("{0} : スクリプトファイルがありませんでした", scriptPath); return; } var scriptType = mono.GetClass(); if (scriptType == null) { Debug.LogErrorFormat("{0} : クラスを取得できませんでした。ファイル名とクラス名が違う可能性があります", mono.name); return; } components.Add(setting.AttachTarget.AddComponent(scriptType)); } // コピーパスは同じなはずのため、最初のを使用する var prefabPath = AssetDatabase.GetAssetPath(settings[0].DuplicatePrefab); var createPath = settings[0].PrefabPath; var prefabName = settings[0].PrefabName; if (string.IsNullOrEmpty(createPath)) { // 空白の場合はアクティブなパスへ生成 createPath = TemplateUtility.GetActiveFolder(); } if (string.IsNullOrEmpty(prefabName)) { // 空白の場合はコピー元のプレハブ名 prefabName = Path.GetFileName(prefabPath); } prefabName += Path.GetExtension(prefabName) == string.Empty ? ".prefab" : string.Empty; var createFullPath = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(createPath, prefabName)); AssetDatabase.CopyAsset(prefabPath, createFullPath); foreach (var component in components) { UnityEngine.Object.DestroyImmediate(component, true); } }
public static void CreateGroupSetting() { CreateScriptableObject <TemplateGroupSetting>(TemplateUtility.GetActiveFolder()); }
/// <summary> /// 生成を実行 /// </summary> public static void Execute() { TemplateUtility.ExecuteSetting(AssetsMenuItemSettingGuid); }