Is there a way to pass instance values to a custom PropertyAttribute?
I have a custom property attribute that lets callers mark a field as read-only.
I'm able to set it up such that it accepts a boolean parameter as to whether or not to to actually make the field readonly, but what I want to do is pass in the value of one of another property as that argument.
Example:
bool CanEditCount = true;
[ReadOnly(!CanEditCount)]
int Count = 0;
This doesn't appear to work. The compiler gives me an error saying the parameter must be static.
Is there any way I can drive this readonly attribute at the instance level?
Answer by FortisVenaliter · May 20, 2016 at 06:50 PM
No, not really. PropertyAttributes are compile-time, so anything passed to them must be static or const. Here's the best I can recommend, but it's a bit hacky:
Make the argument a String. If that string is not null, it ought to be the name of a boolean variable. Then, you can use System.Reflection and typeof() to get the Type, and get the value of the boolean by name in your editor script. But, you'll want to make sure it doesn't run in play mode, because that is totally not optimization friendly.
Thanks. that's exactly the solution I ended up going with, but that's a good point about play mode. I've disabled it as there will be around 50-100 instances of this object in a given scene.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081217im_/https://answers.unity.com/themes/thub/images/avi.jpg)