Suppose I have a list of IEnumerator
s:
public List<IEnumerator> routines;
Suppose I have this enum:
public enum Shape { CIRCLE, TRIANGLE, SQUARE }
I have a lot of IEnumerator
routines that receive a Shape
as an argument:
public IEnumerator Move(Shape shape, float distance){ /* code */ }
public IEnumerator Jump(Shape shape, float height){ /* code */ }
public IEnumerator Fire(Shape shape, float power){ /* code */ }
My list routines
will contain a sequence of instances of these functions (that will be called with StartCoroutine
later):
routines.Add(Move(Shape.SQUARE, 1));
routines.Add(Jump(Shape.CIRCLE, 1));
routines.Add(Fire(Shape.TRIANGLE, 1));
//...
Given I've already filled routines
with all those functions, is there a way for me to iterate through routines
and find out the Shape
argument of each function call? In this case, it would be SQUARE, CIRCLE, TRIANGLE, etc.
If your enumerator implementation is 'Shape' aware, then you can cast items from your routines
and get a shape.
public interface IShapedEnumerator : IEnumerator {
Shape EnumeratorShape {get; }
}
pblic class ShapedIteratorProxy : IShapedEnumerator {
private readonly IEnumerator _baseEnumerator;
private readonly Shape _shape;
public Shape EnumeratorShape => _shape;
public ShapedIteratorProxy (Shape shape, IEnumerator baseEnumerator) {
_baseEnumerator = baseEnumerator;
_shape = shape;
}
public ShapedIteratorProxy (Shape shape, float arg, Func<Shape, float, IEnumerator> operation)
: this(shape, operation(shape, arg))
{}
// implementation of IEnumerator that proxy all calls to _baseEnumerator.
// ...
}
routines.Add(new ShapedIteratorProxy(Shape.SQUARE, Move(Shape.SQUARE, 1)));
routines.Add(new ShapedIteratorProxy(Shape.CIRCLE, 1, Jump);
routines.Add(new ShapedIteratorProxy(Shape.TRIANGLE, 1, Fire);
var shapes = routines.OfType<IShapedEnumerator>()
.Select(s=>s.EnumeratorShape)
.ToArray();
Please note, that if routines
contain items that do not implement IShapedEnumerator
, they will be just skipped.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.