1

大家好,在我导入项目后,Unity 中不断出现此错误。

这是代码:

public static void CameraFadeSwap(Texture2D texture){
    if(cameraFade){
        cameraFade.GetComponent<Image>().texture=texture; 
    }
}

错误是:

Assets\Scripts\Utils\iTween.cs(6016,37): error CS1061: 'Image' does not contain a definition for 'texture' and no accessible extension method 'texture' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

如何修复?请回复。我是 Unity 的新手。谢谢

4

1 回答 1

3

好吧,它没有(请参阅Image)。


但我认为你正在尝试的是分配一个新的Image.sprite

因此,您需要使用该纹理创建一个新的精灵Sprite.Create

public static void CameraFadeSwap(Texture2D texture)
{
    if(cameraFade)
    {
        var newSprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), Vector2.one * 0.5f);
        cameraFade.GetComponent<Image>().sprite = newSprite;
    }
}
于 2020-11-29T16:26:40.797 回答