错误CS0115:“ PopupField <t> .ExecuteDefaultAction(EventBase)”:找不到合适的方法来覆盖

时间:2019-02-13 21:03:12

标签: c# visual-studio unity3d visual-c++

我的脚本有错误,Unity告诉我:

  

库\ PackageCache \ com.unity.package-manager-ui@1.9.11 \ Editor \ Sources \ UI \ Common \ PopupField.cs(118,33):错误CS0115:'PopupField.ExecuteDefaultAction(EventBase)':没有合适的方法可以覆盖

这是脚本:(我是一个有脚本的菜鸟)

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.UIElements;

namespace UnityEditor.PackageManager.UI
{
    internal class PopupField<T> : BaseTextElement, INotifyValueChanged<T>
    {
        private readonly List<T> m_PossibleValues;
        private Func<T, string> m_Callback;
        private EventCallback<ChangeEvent<T>> m_valueCallback;

        private T m_Value;
        public T value
        {
            get { return m_Value; }
            set
            {
                if (EqualityComparer<T>.Default.Equals(m_Value, value))
                    return;

                if (!m_PossibleValues.Contains(value))
                    throw new ArgumentException(string.Format("Value {0} is not present in the list of possible values", value));

                m_Value = value;
                m_Index = m_PossibleValues.IndexOf(m_Value);
                if (m_Callback != null)
                    text = m_Callback(m_Value);
                else
                    text = m_Value.ToString();

#if UNITY_2018_3_OR_NEWER
                MarkDirtyRepaint();
#else
                Dirty(ChangeType.Repaint);
#endif
            }
        }

        private int m_Index = -1;
        public int index
        {
            get { return m_Index; }
            set
            {
                if (value != m_Index)
                {
                    if (value >= m_PossibleValues.Count || value < 0)
                        throw new ArgumentException(string.Format("Index {0} is beyond the scope of possible value", value));
                    m_Index = value;
                    this.value = m_PossibleValues[m_Index];
                }
            }
        }

        /// <summary>
        /// Callback that will return the string to be set in the field's label.
        /// </summary>
        /// <param name="callback"></param>
        public void SetLabelCallback(Func<T, string> callback)
        {
            m_Callback = callback;
        }

        private PopupField(List<T> possibleValues)
        {
            if (possibleValues == null)
                throw new ArgumentNullException("possibleValues can't be null");

            m_PossibleValues = possibleValues;

            AddToClassList("popupField");
        }

        public PopupField(List<T> possibleValues, T defaultValue) :
            this(possibleValues)
        {
            if (defaultValue == null)
                throw new ArgumentNullException("defaultValue can't be null");

            if (!m_PossibleValues.Contains(defaultValue))
                throw new ArgumentException(string.Format("Default value {0} is not present in the list of possible values", defaultValue));

            // note: idx will be set when setting value
            value = defaultValue;
        }

        public PopupField(List<T> possibleValues, int defaultIndex) :
            this(possibleValues)
        {
            if (defaultIndex >= m_PossibleValues.Count || defaultIndex < 0)
                throw new ArgumentException(string.Format("Default Index {0} is beyond the scope of possible value", value));

            // note: value will be set when setting idx
            index = defaultIndex;
        }

        public void SetValueAndNotify(T newValue)
        {
            if (!EqualityComparer<T>.Default.Equals(newValue, value))
            {
                using (ChangeEvent<T> evt = ChangeEvent<T>.GetPooled(value, newValue))
                {
                    value = newValue;
                    if (m_valueCallback != null)
                        m_valueCallback(evt);
                }
            }
        }

        public void OnValueChanged(EventCallback<ChangeEvent<T>> callback)
        {
            m_valueCallback = callback;
            RegisterCallback(callback);
        }

        protected override void ExecuteDefaultAction(EventBase evt)
        {
            base.ExecuteDefaultAction(evt);

            if (evt.GetEventTypeId() == MouseDownEvent.TypeId())
                OnMouseDown();
        }

        private void OnMouseDown()
        {
            var menu = new GenericMenu();

            foreach (T item in m_PossibleValues)
            {
                bool isSelected = EqualityComparer<T>.Default.Equals(item, value);
                menu.AddItem(new GUIContent(item.ToString()), isSelected,
                    () => ChangeValueFromMenu(item));
            }

            var menuPosition = new Vector2(0.0f, layout.height);
            menuPosition = this.LocalToWorld(menuPosition);
            var menuRect = new Rect(menuPosition, Vector2.zero);
            menu.DropDown(menuRect);
        }

        private void ChangeValueFromMenu(T menuItem)
        {
            SetValueAndNotify(menuItem);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

请参见BaseTextElement

  

实验:此API是实验性的,将来可能会更改或删除。

显然是……这就是为什么它被称为Experimental


目前已经有一个PopupFied<T>,但再次出现

  

实验:此API是实验性的,将来可能会更改或删除。

答案 1 :(得分:0)