2

カスタム UI クラスを作成するコードがいくつかあります。これは次の方法で行われます。

public class EasyUIData
{
    protected static Canvas EasyCanvasOptions;
    protected static Vector2 EasyCanvasDimensions;
}

public class UIBaseProperties : EasyUIData
{
    protected GameObject UIElement;
    protected RectTransform Anchor;
    protected Vector2 Loc;
    protected int? SizeX, SizeY;

    public UIBaseProperties(Vector2 loc, int? sizeX = null, int? sizeY = null)
    {
        UIElement = new GameObject();
        this.Loc = loc;
        this.SizeX = sizeX;
        this.SizeY = sizeY;
    }
}

public class RawImage : UIBaseProperties
{
    private RawImage UIImageComponent;
    private Texture2D Img;

    public RawImage(Texture2D img, Vector2 loc, int? sizeX = null, int? sizeY = null) : base(loc, sizeX, sizeY)
    {
        UIImageComponent = UIElement.AddComponent(typeof(RawImage)) as RawImage; // this generates the error.
    }
}

RawImageしかし、コンポーネントを追加したい行で、次のエラーが発生します。

参照変換、ボックス化変換、ボックス化解除変換、ラッピング変換、または null 型変換を介して、型 'UnityEngine.Component' を 'Easy.UI.RawImage' に変換できません

以前にこの手法を使用したことがあり、うまく機能したため、理由はわかりません。
何か不明な点がある場合は、明確にすることができるようにお知らせください。

4

1 に答える 1