0

I know the range attribute constricts a field to a minimum and maximum value like:

[Range(0, 100)]
public float cur;

but, I want to achieve the following:

public float min = 0;
public float max = 100;
[Range(min, max)]
public float cur;

Is it possible?

Tayab
  • 311
  • 2
  • 13
  • 1
    Possible duplicate of [How to set dynamic value in my Attribute](http://stackoverflow.com/questions/6665187/how-to-set-dynamic-value-in-my-attribute) – Jason Watkins Mar 30 '16 at 17:51
  • Why not using a basic slider? It has min and max that does the job. Using the attribute looks fancy but is counter-intuitive. If you set range (20,50) and I put 10, it goes to 20 but I don't know why. A tooltip would be useful to inform though. But slider is just way easier. You can even get the event when the user sets it to use the new value and so on. – Everts Mar 31 '16 at 18:36
  • @fafase Going to get into all this editor stuff after my exams and code some sick ass editor GUIs for my scripts – Tayab Mar 31 '16 at 21:03

1 Answers1

2

No, that isn't possible. All Attribute parameters are evaluated at compile time, so they must be compile-time constants.

Jason Watkins
  • 3,766
  • 1
  • 25
  • 39
  • No way around this? It would really make my inspector look neater – Tayab Mar 30 '16 at 17:55
  • If there was a way around it, my answer would be completely wrong. No, attribute parameters *must* be compile time constants. – Jason Watkins Mar 30 '16 at 17:56
  • Is there any other way to make those two numbers i.e. min, max, end up on a slider dynamically? I don't know much about programming the Unity3D editor but I'll assume that you do and you can help me out – Tayab Mar 30 '16 at 17:58
  • I know nothing about Unity, but I do know C#. Say it with me one more time: Attribute parameters must be compile time constants. If you want to change the values at runtime, you need something other than an attribute – Jason Watkins Mar 30 '16 at 18:00
  • I get it I get it, but I was talking about doing it without the attribute by tapping in on the Unity editor and drawing the slider manually.. Your answer is correct but since you do not have knowledge about Unity I won't take your answer as definite. Thanks anyway – Tayab Mar 30 '16 at 18:05
  • 1
    @Tayab I guess this might help you: http://docs.unity3d.com/ScriptReference/PropertyDrawer.html – Gunnar B. Mar 30 '16 at 18:19
  • @GunnarB. was looking for something specific so I could avoid reading so much, but thanks it's the only useful answer so far – Tayab Mar 30 '16 at 18:27
  • @Tayab I think the lower part is doing something sort of what you want to. – Gunnar B. Mar 30 '16 at 18:30