dbrizov / NaughtyAttributes

Attribute Extensions for Unity
MIT License
4.57k stars 465 forks source link

Naughty Attributes doesn't cooperate with a custom editor script, can it be fixed? #379

Closed darth-biomech closed 11 months ago

darth-biomech commented 11 months ago

I've snatched from the web this code that adds an extension for the editor to make Vector3 with a specific attribute to display a handle so that they could be edited visually in the scene view. However, I noticed that if this code is present in the project at all (Even if it isn't being assigned to MonoBehaviour) - it breaks many Naughty Attributes, specifically what I've noticed - Rollout and ReadOnly.

Could you please suggest what I need to change in this to make it compatible with the NA?

    [CustomEditor(typeof(MonoBehaviour), true)]
    public class DraggableHandleDraw : Editor
    {
        readonly GUIStyle style = new GUIStyle();

        void OnEnable()
        {
            style.fontStyle = FontStyle.Bold;
            style.normal.textColor = Color.white;
        }

        public void OnSceneGUI()
        {
            SerializedProperty property = serializedObject.GetIterator();
            while (property.Next(true))
            {
                if (property.propertyType == SerializedPropertyType.Vector3)
                {
                    FieldInfo field = serializedObject.targetObject.GetType().GetField(property.name);
                    if (field == null)
                    {
                        continue;
                    }

                    MonoBehaviour propertyOwner = serializedObject.targetObject as MonoBehaviour;
                    DraggablePoint[] draggablePoints = (DraggablePoint[]) field.GetCustomAttributes(typeof(DraggablePoint), false);
                    if (draggablePoints.Length > 0 && propertyOwner)
                    {
                        bool isRelative = draggablePoints[0].relative;
                        Vector3 end;
                        if(isRelative)
                        {
                            end = propertyOwner.transform.InverseTransformPoint(
                                Handles.PositionHandle(
                                    propertyOwner.transform.TransformPoint(
                                        property.vector3Value), propertyOwner.transform.rotation));
                            Handles.Label(propertyOwner.transform.TransformPoint(property.vector3Value), property.name);
                        }
                        else
                        {
                            end = Handles.PositionHandle(property.vector3Value, Quaternion.identity);
                            Handles.Label(property.vector3Value, property.name);
                        }
                        property.vector3Value = end;
                        serializedObject.ApplyModifiedProperties();
                    }
                }
            }
        }
    }
darth-biomech commented 11 months ago

Some folks helped me with it, and the issue was fixed by changing inheritance to NaughtyInspector, and replacing the original OnEnable() with

        protected override void OnEnable()
        {
            base.OnEnable();
            style.fontStyle = FontStyle.Bold;
            style.normal.textColor = Color.white;
        }