D
D
Daniel2021-01-21 19:16:53
Unity
Daniel, 2021-01-21 19:16:53

How can unity change the EditorGUI properties window through attributes?

I create my own attribute, which should be highlighted in red if the user has not defined the property value in the editor. And display a warning.
There is no documentation anywhere, or it ends at this point. I would like to see default implementations. And then I finger the methods to the sky.
Or is there an attribute that would output Warning under the property in the editor?

class WarningNotNullAttribute : PropertyAttribute
    {
        public string Desc;
        public string color="red";
        public WarningNotNullAttribute(string Desc)
        {
            this.Desc = Desc; 
        }
    }
    
    [CustomPropertyDrawer(typeof(WarningNotNullAttribute))]
    public class IntAttributeDrawer : PropertyDrawer
    {
        string desc;
        Color color;

        protected virtual void Init(SerializedProperty property)
        {
            if (attribute != null)
            {
                WarningNotNullAttribute a = (WarningNotNullAttribute)attribute;
                desc = a.Desc;
                
                var c =  System.Drawing.Color.FromName( a.color);
                color.r = c.R;
                color.g = c.G;
                color.b = c.B;

            }
        }

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property == null)
            {
                return;
            }

            Init(property);
            EditorGUI.BeginProperty(position, label, property);


            position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

            // Don't make child fields be indented
            int indent = EditorGUI.indentLevel;
            EditorGUI.indentLevel = 0;

            // Calculate rects
            Rect pathRect = new Rect(position.x, position.y, position.width - 6, position.height);

   
            /// вот тут через методы EditorGUI можно что-то рисовать. Но слишком не понятно.
          ////  если вызвать basse.OnGUI();  то там текст накладывается. 
  
            
            EditorGUI.(pathRect,v,FocusType.Passive);// TextField (pathRect, "not null",s);
            EditorGUI.indentLevel = indent;

            EditorGUI.EndProperty();
        }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question