Пример #1
0
 /// <summary>
 /// ルートListViewとしてセットアップします。
 /// </summary>
 /// <param name="onItemSelected">アイテム選択時のコールバック。</param>
 internal CommandListView(CommandEditorDomain domain, SerializedProperty commandArray)
 {
     Domain = domain;
     CommandArrayProperty = new PersistentSerializedProperty(commandArray);
     if (!CommandArrayProperty.IsArray)
     {
         throw new ArgumentException("commandArray must be an array.");
     }
     BuildList();
 }
Пример #2
0
        public CommandItem(CommandEditorDomain domain, CommandListView parentList,
                           PersistentSerializedProperty property,
                           Action <CommandItem, CommandListView, int> onMove) : base()
        {
            Domain          = domain;
            ParentList      = parentList;
            CommandProperty = property;

            this.binding = new CommandItemBinding(() =>
            {
                if (this != Domain.SelectedItem)
                {
                    return;
                }
                UpdateCommandDetail();
            });

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

            this.Add(root);

            this.AddManipulator(new CommandMovableManipulator(this));
            this.AddManipulator(new CommandMoveTargetManipulator(
                                    ParentList,
                                    part =>
            {
                switch (part)
                {
                case MovementPart.Top:
                    return(GetIndex());

                case MovementPart.Bottom:
                    return(GetIndex() + 1);

                default:
                    throw new ArgumentOutOfRangeException(nameof(part), part, null);
                }
            },
                                    () =>
            {
                this.style.borderBottomWidth = new StyleFloat(0f);
                this.style.borderTopWidth    = new StyleFloat(3f);
                this.style.borderTopColor    = new StyleColor(new Color(0.2f, 0.3411765f, 0.8509805f));
            },
                                    () =>
            {
                this.style.borderTopWidth    = new StyleFloat(0f);
                this.style.borderBottomWidth = new StyleFloat(3f);
                this.style.borderBottomColor = new StyleColor(new Color(0.2f, 0.3411765f, 0.8509805f));
            },
                                    () =>
            {
                this.style.borderTopWidth    = new StyleFloat(0f);
                this.style.borderBottomWidth = new StyleFloat(0f);
            }));

            this.RegisterCallback <MouseDownEvent>(evt =>
            {
                evt.StopPropagation();
                Select();
            });

            var enabledToggle = this.Q <Toggle>("CommandEnabled");

            enabledToggle.RegisterCallback <ChangeEvent <bool> >(evt =>
            {
                var prop              = CommandProperty.GetProperty();
                var propEnabled       = prop.FindPropertyRelative("enabled");
                propEnabled.boolValue = evt.newValue;
                propEnabled.serializedObject.ApplyModifiedProperties();
            });

            //string typeName = GetTypeName();

            var customContentContainer = this.Q <VisualElement>("CommandCustomDetailContent");

            customContentContainer.Clear();
            Editor = CreateEditor(GetCommandType(), this, customContentContainer);
            UpdateCommandDetail();
        }
Пример #3
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);
        }