【问题标题】:Method is marked as an override but no suitable method found to override方法被标记为覆盖,但找不到合适的方法来覆盖
【发布时间】:2018-02-11 17:22:32
【问题描述】:

我在尝试进入游戏模式时遇到了统一代码问题 它说

Assets/game/Scripts/Effects/Gradient.cs(14,26): error CS0115: `Gradient.ModifyVertices(System.Collections.Generic.List<UnityEngine.UIVertex>)' is marked as an override but no suitable method found to override

Gradient.cs 看起来像这样

using System.Collections.Generic;
using UnityEngine.UI;

[AddComponentMenu( "UI/Effects/Gradient" )]
public class Gradient : BaseMeshEffect
{
    [SerializeField]
    private Color32 topColor = Color.white;
    [SerializeField]
    private Color32 bottomColor = Color.black;

    public override void ModifyVertices( List<UIVertex> vertexList )
    {
        if( !IsActive() )
        {
            return;
        }

        int count = vertexList.Count;
        float bottomY = vertexList[0].position.y;
        float topY = vertexList[0].position.y;

        for( int i = 1; i < count; i++ )
        {
            float y = vertexList[i].position.y;
            if( y > topY )
            {
                topY = y;
            }
            else if( y < bottomY )
            {
                bottomY = y;
            }
        }

        float uiElementHeight = topY - bottomY;

        for( int i = 0; i < count; i++ )
        {
            UIVertex uiVertex = vertexList[i];
            uiVertex.color = Color32.Lerp( bottomColor, topColor, ( uiVertex.position.y - bottomY ) / uiElementHeight );
            vertexList[i] = uiVertex;
        }
    }
}

谁能帮我解决这个问题,我是团结的初学者,一个多月以来我一直在寻找解决方案。 PS:我正在使用统一 2017.3

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    你的代码有两个问题:

    1.错误的类派生

    如果你用重载List&lt;UIVertex&gt; vertices 参数覆盖ModifyVertices 函数,你应该让你的脚本驱动来自BaseVertexEffect BaseMeshEffect

    应该起作用了:

    public class Gradient : BaseVertexEffect 
    {
    
        public override void ModifyVertices( List<UIVertex> vertexList )
        {
            ....
        }
    }
    

    但它不会因为您使用的是 Unity 2017.3。它应该适用于旧版本的 Unity。请参阅 #2 了解更多信息和操作。

    2.不推荐使用的脚本和函数

    #1 中的修复方法会起作用,但您使用的是 g Unity 2017.3。

    BaseVertexEffect 已被弃用很长时间,现在在 Unity 2017.3.0p3 中已成为 removed。您不能再从BaseVertexEffect 脚本派生,也不能使用void ModifyVertices(List&lt;UIVertex&gt; verts),因为它已被删除。添加了一个新的重载来替换它。

    派生自的新类是BaseMeshEffect。要覆盖的函数是void ModifyMesh(Mesh mesh)。虽然,它看起来已被 void ModifyMesh(VertexHelper vh) 替换,所以如果您遇到任何已弃用的错误,请删除 ModifyMesh(Mesh mesh)

    您的新代码应如下所示:

    public class Gradient: BaseMeshEffect
    {
        public override void ModifyMesh(VertexHelper vh)
        {
    
        }
    
        public override void ModifyMesh(Mesh mesh)
        {
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      类 UI.BaseMeshEffect (https://docs.unity3d.com/ScriptReference/UI.BaseMeshEffect.html) 没有可以覆盖的函数 ModifyVertices。

      覆盖仅适用于基类具有且标记为可覆盖(虚拟或抽象)的函数。在您的情况下,覆盖没有意义。您所能做的就是将函数添加到您的派生类中。

      【讨论】:

      • 你能解释一下我该怎么做吗?
      • @hatimboubakri: 怎么样?压倒一切?这实际上是不可能的。将函数添加到派生类?只需在派生类中编写函数即可,无需特殊关键字。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多