Skip to content

RequireComponent attribute #1

@paulpach

Description

@paulpach

This is pretty awesome, ty, I have a suggestion:

You implemented GetComponent, and GetComponentProperty.

But I have a suggestion that does not have runtime cost, and that makes sure that the component exists. For example:

public partial class MyComponent : MonoBehaviour{
     [RequiredComponent] private TMP_Text text;
}

would generate:

[RequireComponent(typeof(TMP_Text))]
public partial class MyComponent : MonoBehaviour{
     [SerializeField] private TMP_Text text;

    public void OnValidate() {
        if (text == null)
            TryGetComponent<TMP_Text>(out text);
    }
}

By adding the [RequiredComponent] at the top of the class, it would make sure the component is added to the gameobject.

By adding the assignment in the OnValidate, this enforces the field is assigned in the editor with zero cost at runtime.

Ideally it would generate the OnValidate method, but if the user defines one, it would generate a ValidateFields() method that the user defined OnValidate has to call.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions