簡體   English   中英

方法被標記為覆蓋,但沒有找到合適的方法來覆蓋

[英]Method is marked as an override but no suitable method found to override

我在嘗試進入播放模式時遇到了我的統一代碼問題,它說

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; } } }

任何人都可以幫我解決這個問題,我是 Unity 的初學者,我一直在尋找解決方案一個多月。 PS:我正在使用統一 2017.3

您的代碼有兩個問題:

1.錯誤的類推導

如果您使用重載List<UIVertex> 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 中刪除 您不能再從BaseVertexEffect腳本派生,並且不能使用void ModifyVertices(List<UIVertex> 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)
    {

    }
}

類 UI.BaseMeshEffect (https://docs.unity3d.com/ScriptReference/UI.BaseMeshEffect.html ) 沒有可以覆蓋的 ModifyVertices 函數。

覆蓋僅適用於基類具有且被標記為可覆蓋(虛擬或抽象)的函數。 在您的情況下,覆蓋毫無意義。 您所能做的就是將函數添加到您的派生類中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM