Esempio n. 1
0
        private void OnEnable()
        {
            _instanceId   = target.GetInstanceID().ToString();
            SettingStatus = new TemplateSettingStatus(serializedObject);

            _foldouts = new FoldoutInfo[]
            {
                new FoldoutInfo("Code", () => DrawCode(SettingStatus)),
                new FoldoutInfo("Replace Texts", () => DrawReplace(_replaceList, _instanceId)),
                new FoldoutInfo("Pre Process", () => DrawChain(SettingStatus)),
            };

            var property = SettingStatus.GetProperty(TemplateSettingStatus.Property.IsFoldouts);

            for (int i = 0; i < _foldouts.Length; ++i)
            {
                if (property.arraySize <= i)
                {
                    break;
                }

                _foldouts[i].IsFoldout = property.GetArrayElementAtIndex(i).boolValue;
            }

            _descriptionFoldout           = new FoldoutInfo("Description", DrawDescription);
            _descriptionFoldout.IsFoldout = string.IsNullOrEmpty(SettingStatus.GetProperty(TemplateSettingStatus.Property.Description).stringValue) == false;

            UpdateReplaceList(true);
        }
Esempio n. 2
0
        public static void DrawCode(TemplateSettingStatus status)
        {
            var minHeight = status.GetProperty(TemplateSettingStatus.Property.CodeAreaMinHeight);
            var maxHeight = status.GetProperty(TemplateSettingStatus.Property.CodeAreaMaxHeight);

            if (maxHeight.floatValue > 0f && maxHeight.floatValue < minHeight.floatValue)
            {
                maxHeight.floatValue = minHeight.floatValue;
            }

            EditorGUILayout.BeginHorizontal(EditorGUIHelper.GetScopeStyle());
            {
                EditorGUILayout.PropertyField(minHeight, new GUIContent("Min Height"));
                EditorGUILayout.PropertyField(maxHeight, new GUIContent("Max Height"));
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginVertical(EditorGUIHelper.GetScopeStyle());
            {
                var code       = status.GetProperty(TemplateSettingStatus.Property.Code).stringValue;
                var scrollPos  = status.GetProperty(TemplateSettingStatus.Property.ScrollPos);
                var scroll     = scrollPos.vector2Value;
                var editedCode = SyntaxHighlightUtility.DrawCSharpCode(ref scroll, code, 12, minHeight.floatValue, maxHeight.floatValue);
                scrollPos.vector2Value = scroll;
                if (editedCode != code)
                {
                    status.GetProperty(TemplateSettingStatus.Property.Code).stringValue = editedCode;
                    status.IsUpdateText = true;
                    Undo.IncrementCurrentGroup();
                }
            }
            EditorGUILayout.EndVertical();
        }
        protected string ReplaceCode(ProcessDictionary result)
        {
            object obj;

            result.TryGetValue(result.GetLastConvertReplaceWord(TemplateSetting.ResultKey), out obj);
            var setting = obj as TemplateSetting;

            if (setting == null)
            {
                Debug.LogErrorFormat("Not {0} object", TemplateSetting.ResultKey);
                return(null);
            }

            var status = new TemplateSettingStatus(new SerializedObject(setting));

            TemplateSettingEditor.ExecuteChain(status, result);
            var words = ReplaceProcessor.GetReplaceWords(
                status.GetProperty(TemplateSettingStatus.Property.Path).stringValue,
                status.GetProperty(TemplateSettingStatus.Property.ScriptName).stringValue,
                status.GetProperty(TemplateSettingStatus.Property.Code).stringValue
                );
            var replaces = TemplateSettingEditor.CreateReplaceList(new List <ReplaceInfo>(0), words.ToArray());

            foreach (var replace in replaces)
            {
                result.Add(replace.Key, replace.ReplaceWord);
            }

            return(TemplateSettingEditor.Replace(
                       status.GetProperty(TemplateSettingStatus.Property.Code).stringValue,
                       result
                       ));
        }
Esempio n. 4
0
        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();
            }
        }
Esempio n. 5
0
 public static void DrawOverwrite(TemplateSettingStatus status)
 {
     EditorGUILayout.BeginVertical(EditorGUIHelper.GetScopeStyle());
     {
         var property = status.GetProperty(TemplateSettingStatus.Property.Overwrite);
         EditorGUILayout.PropertyField(property, new GUIContent("Overwrite Type"));
     }
     EditorGUILayout.EndVertical();
 }
Esempio n. 6
0
        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);
            }
        }
Esempio n. 7
0
        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);
            }
        }
Esempio n. 8
0
        private void DrawIsAssetsMenuItem()
        {
            EditorGUILayout.BeginVertical(EditorGUIHelper.GetScopeStyle());
            {
                var cache = SettingStatus.TargetTemplateSetting.IsAssetsMenuItem;
                var isAssetMenuProperty = SettingStatus.GetProperty(TemplateSettingStatus.Property.AssetsMenuItem);
                EditorGUILayout.PropertyField(isAssetMenuProperty, new GUIContent("Add Asset Menu"));

                // 生成時に設定反映が間に合わないため
                SettingStatus.TargetTemplateSetting.IsAssetsMenuItem = isAssetMenuProperty.boolValue;

                if (cache != SettingStatus.TargetTemplateSetting.IsAssetsMenuItem)
                {
                    AssetsMenuItemProcessor.Execute();
                }

                EditorGUILayout.HelpBox("Add a menu item to \"Assets/Create/Template/~\"", MessageType.Info);
            }
            EditorGUILayout.EndVertical();
        }
Esempio n. 9
0
        public static void DrawPrefab(TemplateSettingStatus status)
        {
            EditorGUILayout.BeginVertical(EditorGUIHelper.GetScopeStyle());
            {
                var prefabProperty = status.GetProperty(TemplateSettingStatus.Property.DuplicatePrefab);
                var targetProperty = status.GetProperty(TemplateSettingStatus.Property.AttachTarget);

                var oldObj = prefabProperty.objectReferenceValue as GameObject;
                EditorGUILayout.PropertyField(prefabProperty, new GUIContent("Attach Prefab"), true);

                var obj = prefabProperty.objectReferenceValue as GameObject;
                if (obj == null || PrefabUtility.GetPrefabType(obj) != PrefabType.Prefab)
                {
                    targetProperty.objectReferenceValue = null;

                    EditorGUILayout.EndVertical();
                    return;
                }

                if (oldObj != obj)
                {
                    targetProperty.objectReferenceValue = prefabProperty.objectReferenceValue = PrefabUtility.FindRootGameObjectWithSameParentPrefab(obj);
                }

                EditorGUILayout.BeginHorizontal(EditorGUIHelper.GetScopeStyle());
                {
                    if (GUILayout.Button("Change Attach Target"))
                    {
                        PrefabTreeViewWindow.Open(obj, targetProperty.objectReferenceValue as GameObject, (targetObj) =>
                        {
                            status.TargetSerializedObject.Update();
                            status.GetProperty(TemplateSettingStatus.Property.AttachTarget).objectReferenceValue = targetObj;
                            status.TargetSerializedObject.ApplyModifiedProperties();
                        });
                    }

                    EditorGUILayout.LabelField(targetProperty.objectReferenceValue == null ? string.Empty : targetProperty.objectReferenceValue.name);
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginVertical(EditorGUIHelper.GetScopeStyle());
                {
                    var pathProperty = status.GetProperty(TemplateSettingStatus.Property.PrefabPath);
                    EditorGUILayout.PropertyField(pathProperty, new GUIContent("Create Prefab Path"), true);

                    var paths = EditorGUIHelper.DrawDragAndDropArea();
                    if (paths != null && paths.Length > 0)
                    {
                        pathProperty.stringValue = paths[0];
                    }

                    if (string.IsNullOrEmpty(pathProperty.stringValue))
                    {
                        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());
                {
                    var nameProperty = status.GetProperty(TemplateSettingStatus.Property.PrefabName);
                    EditorGUILayout.PropertyField(nameProperty, new GUIContent("Prefab Name"), true);

                    if (string.IsNullOrEmpty(nameProperty.stringValue))
                    {
                        EditorGUILayout.HelpBox("Example: ExamplePrefab", MessageType.Info);
                    }
                }
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndVertical();
        }