【发布时间】:2019-01-30 22:46:48
【问题描述】:
我有一些创建自定义 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 组件的行中,我收到以下错误:
无法通过引用转换、装箱转换、拆箱转换、包装转换或空类型转换将类型“UnityEngine.Component”转换为“Easy.UI.RawImage”
我不知道为什么,因为我以前使用过这种技术,效果很好。
如果有不清楚的地方请告诉我,以便我澄清。
【问题讨论】:
-
EasyUIData 继承了什么?我认为该对象没有理由能够正确转换为 UnityEngine.Component
-
您要使用哪种 RawImage? Unity 已经知道名称为 RawImage 的组件。而且您已经建立了自己的同名课程。也许您的班级与 UnityEngine.UI.RawImage 之间存在混淆。
-
@Eliasar EasyUIData 类不继承任何东西,它只是基础。为了清楚起见,我会更新我的问题
-
@GameGenerator 我正在制作一个自定义 UI 框架,它建立在统一的 UI 元素之上。所以我试图在这个自定义 UI 类中使用 Component RawImage。
-
@FutureCake Unity 可能不知道要使用什么原始图像。你想要 UnityEngine.UI.RawImage 但 Unity 理解 Easy.UI.RawImage 这似乎不是组件。
标签: c# unity3d type-conversion components