In this code:
public int x = 50;
[Space(x)]
public bool b;
I’d like to use the variable x in the space attribute but it’s giving me the error:
Cannot acces non-static field x in static context.
How do I make this work?
In this code:
public int x = 50;
[Space(x)]
public bool b;
I’d like to use the variable x in the space attribute but it’s giving me the error:
Cannot acces non-static field x in static context.
How do I make this work?
just make int static or const .
const int x = 50;
static int x = 50; (though i’m not sure if that works)
It’s not possible.
Attributes belong to the class, not the instance. They need to be completely resolvable at compile time.
If you indicate what you are trying to do, we might be able to help with a work around.
Sure! I have written these pieces of code:
public enum TypeOfCurve { Linear, SmoothStep, Custom }
public TypeOfCurve CurveType;
[Curve(0, 0.1f, 1f, 10f, true)]
public AnimationCurve SpeedCurve;
using UnityEngine;
public class CurveAttribute : PropertyAttribute
{
public float PosX;
public float PosY;
public float RangeX;
public float RangeY;
public bool b;
public CurveAttribute(float PosX, float PosY,float RangeX, float RangeY, bool b)
{
this.PosX = PosX;
this.PosY = PosY;
this.RangeX = RangeX;
this.RangeY = RangeY;
this.b = b;
}
}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(CurveAttribute))]
public class CurveDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
CurveAttribute curve = attribute as CurveAttribute;
if (property.propertyType == SerializedPropertyType.AnimationCurve)
{
if(curve.b)
EditorGUI.CurveField(position, property, Color.cyan, new Rect(curve.PosX, curve.PosY, curve.RangeX, curve.RangeY));
}
}
}
And basically I only want the variable ‘SpeedCurve’ to show up when ‘custom’ is selected in the ‘TypeOfCurve’ enum.
const worked in this case but this was just an example and for my real purpose, const isn’t going to work… Thank you though!
You can read variables from the component in a property drawer. I would suggest wrapping the curve and the enum in their own class, then writing a drawer for that class.
Sorry don’t quite get what you mean. So I can read variables from the ‘CurveAttribute’ class in the ‘CurveDrawer’ class, but then what do you mean with wrapping the curve and the end in their own class?
EDIT: Alright I didn’t show you before, but the curve and the enum are actually in a class already!
[Serializable]
public class RotationTimelineData
{
public enum TypeOfCurve
{
Linear,
SmoothStep,
Custom
}
public TypeOfCurve CurveType;
[Curve(0, 0.1f, 1f, 10f, true)] public AnimationCurve SpeedCurve;
}
I’ll attempt writing a drawer for the class!
Okay, so it would work normally but I have this:
The animation curve only is displayed when ‘custom’ is selected in the enum, but now the variables get displayed outside of the list. Probably because the list library I’m using already uses a drawer to display the elements in the class. I’ll try to override that drawer and maybe fix it that way. Thanks @Kiwasi !