I'm wondering how to create a default class constructor. I do not want to waste resources, so I just want the constructor to return a pointer to an already existing instance of the class.
This is what I thought of. Obviously, it doesn't work, but I want to follow this code's logic.
public Sprite()
{
return Default.MissingSprite;
}
public Sprite(Texture2D texture, SpriteDrawMode drawMode)
{
if (drawMode != SpriteDrawMode.Sliced) throw new ArgumentException("...");
this.texture = texture;
this.drawMode = drawMode;
this.sliceFraction = Default.SpriteSliceFraction;
}
public Sprite(Texture2D texture, SpriteDrawMode drawMode, float sliceFraction)
{
this.texture = texture;
this.drawMode = drawMode;
this.sliceFraction = sliceFraction;
}
I know constructors are void, so I can't return in them.
I do NOT want to just assign the values of the default instance, as that would waste memory, since it would just create a duplicate of the default instance
//This is what I do NOT want
public Sprite()
{
this.texture = Default.MissingSprite.texture;
this.drawMode = Default.MissingSprite.drawMode;
this.sliceFraction = Default.MissingSprite.sliceFraction;
}
Is what I'm trying to achieve possible? Are there any design problems with my thought process?