Beispiel #1
0
        private static EventCommandEditor CreateEditor(Type commandType, CommandItem item,
                                                       VisualElement customContentContainer)
        {
            EventCommandEditor editor = null;

            if (commandType != null)
            {
                var editorType = EventCommandUtility.GetCommandEditorType(commandType);

                if (editorType != null)
                {
                    editor = Activator.CreateInstance(editorType, item, customContentContainer) as
                             EventCommandEditor;
                }
            }


            if (editor == null)
            {
                editor = new GenericEventCommandEditor(item, customContentContainer);
            }

            editor.OnCreated();
            return(editor);
        }
Beispiel #2
0
        private void Init(EventScriptAsset scriptAsset, UnityEngine.Object context)
        {
            selectedAssetGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(scriptAsset));

            target = scriptAsset;
            VisualTreeAsset uiAsset = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(visualTreeAssetPath);
            VisualElement   root    = uiAsset.CloneTree();

            GetSkins();

            rootVisualElement.styleSheets.Clear();
            rootVisualElement.styleSheets.Add(defaultCommonDarkStyleSheet);


            root.style.flexGrow = 1f;

            SerializedObject sObj;

            contextObject = context;
            if (context)
            {
                sObj = new SerializedObject(scriptAsset, context);
            }
            else
            {
                sObj = new SerializedObject(scriptAsset);
            }

            var commandsProperty = sObj.FindProperty("script.CommandList.commands");

            //メニューボタンのハンドラ
            root.Q <Button>("RefreshButton").clickable.clicked   += Refresh;
            root.Q <Button>("SaveAssetButton").clickable.clicked += SaveAsset;
            root.Q <Button>("SelectButton").clickable.clicked    += () => { EditorGUIUtility.PingObject(scriptAsset); };
            var contextObjectField = root.Q <ObjectField>("ContextObject");

            contextObjectField.objectType = typeof(EventRuntimeReferenceHost);
            contextObjectField.value      = context;
            root.Q <ObjectField>("ContextObject").RegisterCallback <ChangeEvent <UnityEngine.Object> >(evt =>
            {
                var newContext = evt.newValue;
                this.Init(target, newContext);
            });

            root.Q <Button>("FindReferenceHostButton").clickable.clicked += () => { Init(scriptAsset); };

            root.Q <Button>("CreateReferenceHostButton").clickable.clicked += () =>
            {
                var found = FindReferenceHostFromScene(scriptAsset);

                if (found)
                {
                    if (!EditorUtility.DisplayDialog("",
                                                     "現在のシーンにはすでにこのEventScriptAssetのEventRuntimeReferenceHostが存在します。重複によって実行時の自動参照が行われなくなるおそれがありますが、続行しますか?",
                                                     "はい", "キャンセル"))
                    {
                        return;
                    }
                }


                var referenceHost = new GameObject();
                referenceHost.name = scriptAsset.name + " RuntimeReference";
                var host = referenceHost.AddComponent <EventRuntimeReferenceHost>();
                host.TargetAsset = scriptAsset;
                Init(scriptAsset, host);
            };


            //コマンドリストの構築
            commandList = root.Q <VisualElement>("CommandList");
            commandList.Clear();

            domain = new CommandEditorDomain(commandsProperty, OnItemSelected);

            var rootCommandListView =
                domain.RootCommandListView; //new CommandListView(OnItemSelected, commandsProperty);

            rootCommandListView.style.paddingBottom = 100f;

            commandList.Add(rootCommandListView);

            //コマンド追加メニューの構成
            var addCommandList = root.Q <ScrollView>("AddCommandList");

            addCommandList.Clear();

            //-定義済みコマンドを列挙
            Dictionary <EventCommandAttribute, Type> definedCommands = new Dictionary <EventCommandAttribute, Type>();

            var commandTypes = EventCommandUtility.GetAllEventCommandTypes();

            foreach (var commandType in commandTypes)
            {
                object[] attributes = commandType.GetCustomAttributes(typeof(EventCommandAttribute), false);
                if (attributes.Length == 0)
                {
                    //EventCommand属性がついていないので、カテゴリなしとして分類
                    definedCommands.Add(new EventCommandAttribute("カテゴリなし", commandType.Name), commandType);
                    continue;
                }
                else
                {
                    foreach (var attribute in attributes)
                    {
                        var commandAttribute = attribute as EventCommandAttribute;
                        definedCommands.Add(commandAttribute, commandType);
                    }
                }
            }

            //-要素の構築

            Dictionary <string, Foldout> categories = new Dictionary <string, Foldout>();

            foreach (var definedCommand in definedCommands)
            {
                string category = definedCommand.Key.Category;
                if (!categories.ContainsKey(category))
                {
                    var foldout = new Foldout()
                    {
                        text = category
                    };
                    addCommandList.Add(foldout);
                    categories.Add(category, foldout);
                }

                var commandType = definedCommand.Value;


                var button = new Button()
                {
                    text = definedCommand.Key.DisplayName
                };
                button.clickable.clicked += () =>
                {
                    var command    = Activator.CreateInstance(commandType) as EventCommand;
                    var newCommand = domain.AddCommandAtSelected(command);
                    //選択
                    newCommand.Select();
                };
                categories[category].Add(button);

                var editorType = EventCommandUtility.GetCommandEditorType(commandType);

                if (editorType == null)
                {
                    continue;
                }

                var editorAttribute = editorType.GetCustomAttributes(typeof(CustomEventCommandEditorAttribute))
                                      .OfType <CustomEventCommandEditorAttribute>().FirstOrDefault();

                var path = editorAttribute.IconTexturePath;

                if (!string.IsNullOrEmpty(path))
                {
                    var iconTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(path);
                    var icon        = new Image {
                        image = iconTexture
                    };
                    icon.style.width  = new StyleLength(new Length(16f, LengthUnit.Pixel));
                    icon.style.height = new StyleLength(new Length(16f, LengthUnit.Pixel));
                    button.Add(icon);
                }
            }

            //キーボードショートカット
            rootVisualElement.RegisterCallback <KeyDownEvent>(evt =>
            {
                if (evt.keyCode == KeyCode.Delete)
                {
                    domain.RemoveCommandAtSelected();
                }
            });

            titleContent = new GUIContent("EventScriptEditor");
            rootVisualElement.Clear();
            rootVisualElement.Add(root);
        }