I looked elsewhere on the internet, and if the answer is out there, it seems heavily obscured. For example, I want to make an indexer for Rect, the same way we have one for Vector4 or Color. Can it be done?
2 Answers
2Not a great solution, but I'll suggest it anyway since I can't think of anyway to create extension indexers nicely. You can create a custom class that functions just like Rect and allows implicit conversions to and from Rect. Then you can manipulate the data in your custom class and pass it to methods just like a Rect. The obvious down side is that you have to re-implement all the methods.
public struct PRect {
public PRect () { //some values = other values }
public PRect (Rect prototype) { //copy values over }
public float this[int index] {
get{ return value; }
set( values[index] = value; }
}
public static implicit operator Rect(PRect p) { return newRect; }
public static implicit operator PRect(Rect r) { return newPRect; }
}
Unfortunately this would be mostly a waste of time re-implementing all the methods, but depending on what your doing it might be useful. Fortunately `Rect` isn't a very large class.
Oh and not to mention the performance hit from casting.
You mean using [square brackets] to access members in a Rect? If so, couldn't you subclass it and add indexer functions ala http://msdn.microsoft.com/en-us/library/2549tw02.aspx, assigning each part individually (I would guess), like if index==0 return x, index==2 return width, etc.
You can't derive from a sealed type, and I don't want to anyway.
– JessyAny chance of using Reflection? Otherwise, vote for it in feedback?
– DaveAI've yet to use reflection, and I don't think it's something UT could implement. Or are you talking about Microsoft feedback? Do they have a site for that?
– JessyI've used reflection in Unity. I meant feedback.unity3d.com. I'm not sure you could do this, never tried with such a class, but wondering if it could be introspected with the reflection api's. Like a helper class.
– DaveA