Hi,
I’ve been trying to understand how to use attributes (unityscript) and the thing is I couldn’t find any info about them.
I want to “mark” some of my variables in different classes and all those “marked” variables to be added in a List. of another class to do stuff with them later on.
I though that attributes is what I should be using for such a thing(?)
Thanks in advance
I don’t see a lot of documentation describing attributes in UnityScript. However, I think UnityScript attributes might just be a proxy implementation of the Attribute classes of the Mono framework, which is an implementation of the .NET framework. Your best bet would be to stick with C# if you want to write custom attributes.
Attributes weren’t designed to do exactly what you want to do, but you might be able to finagle it. In order to discover which fields in a given assembly have a given attribute, you would have to basically do a brute-force search of all fields in all types in the assembly. But as long as the assembly isn’t totally massive, then it shouldn’t be much of a problem.
Microsoft’s Attribute documentation includes a good tutorial and plenty of information to get you started. Writing custom attributes is easy.
Your “Mark” attribute, since you are using it as a tag, it doesn’t need any properties, would literally be two lines of code. Something like this…
[System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false)]
public class Mark : System.Attribute { }
Thanks for the reply Brian.
I’ve seen that msdn tutorial but… particularly I dont understand when an attribute’s constructor gets execute. Or can we get the target or even the class instance that the target resides within the constructor?
Again, I might have completely misundertood the reason of attributes existance and looking at the wrong directions about it as you’ve pointed out
Thanks!