dbrizov / NaughtyAttributes

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

Implements "RequiredType" validation attribute #346

Open demirmusa opened 2 years ago

demirmusa commented 2 years ago

That pr implements RequiredType validation attribute. You can use RequiredType attribute on gameobjects or any kind of components to validate that gameobject property that use that attribute has required component(s).

Example Usage:

Checking an Interface

using NaughtyAttributes;
using UnityEngine;

public interface IMyInterface1
{
}

public class MyComponent1 : MonoBehaviour
{
    [RequiredType(typeof(IMyInterface1))] //make sure given GameObject has component with specific interface
    [SerializeField]
    private GameObject myObj;

    private void Start()
    {
        var myInterface = myObj.GetComponent<IMyInterface1>();
        //do something
    }
}

Checking a Component

using NaughtyAttributes;
using UnityEngine;

public class MyComponent2 : MonoBehaviour
{
}
public class MyComponent3 : MonoBehaviour
{
}

public class MyComponent1 : MonoBehaviour
{
    [RequiredType(typeof(MyComponent3))]//make sure given myComponent2 also has MyComponent3 too.
    [SerializeField] private MyComponent2 myComponent2;

    private void Start()
    {
        var myComponent3 = myComponent2.GetComponent<MyComponent3>();
        //do something
    }
}

Checking Multiple Types

using NaughtyAttributes;
using UnityEngine;

public interface IMyInterface1
{
}

public class MyComponent2 : MonoBehaviour
{
}

public class MyComponent3 : MonoBehaviour
{
}

public class MyComponent1 : MonoBehaviour
{
    //make sure given component has a component that Inherits IMyInterface1
    //make sure given component has MyComponent3 component
    //Note: They can be different components (one has MyComponent3 other one has IMyInterface1)
    //or same component (if MyComponent3 inherits IMyInterface1)
    [RequiredType(typeof(IMyInterface1), typeof(MyComponent3))]
    [SerializeField]
    private MyComponent2 myComponent2;

    private void Start()
    {
        var myComponent3 = myComponent2.GetComponent<MyComponent3>();
        var myInterface1 = myComponent2.GetComponent<IMyInterface1>();
        //do something
    }
}

Result NaughtyAttributesRequireType

Resolves https://github.com/dbrizov/NaughtyAttributes/issues/351

demirmusa commented 2 years ago

Hi @dbrizov The pr is completed now(I just did small renaming). Is there anything else I should do?

demirmusa commented 1 year ago

Hi @dbrizov Any update? I use NaughtyAttributes in all my projects. Has the project been left to be actively maintained?

dbrizov commented 1 year ago

Hi, @demirmusa I am still the only one maintaining the project. I am usually waiting for a batch of pull requests so that I can merge them all at the same time. I am planning on making an update in the next couple of weeks. Just need to find some spare time.

demirmusa commented 1 year ago

Hi @dbrizov, Thank you very much for your detailed explanation. If there is anything about Pr, you can ask me directly. (Or if you dont want to implement it)

Apart from that, I love this project and would like to contribute/help as much as I can.

cmwolke-gamedesign commented 3 months ago

Would really love to see this merged, any progress? :)

TylerTemp commented 3 months ago

For anyone who is still waiting for this PR, I have a project that can co-work with NaughtyAttributes with this feature so you don't need to switch:

using SaintsField;

public interface IMyInterface {}

public class MyInter1: MonoBehaviour, IMyInterface {}
public class MySubInter: MyInter1 {}

public class MyInter2: MonoBehaviour, IMyInterface {}

[RequireType(typeof(IMyInterface))] public SpriteRenderer interSr;
[RequireType(typeof(IMyInterface), typeof(SpriteRenderer))] public GameObject interfaceGo;

[RequireType(true, typeof(IMyInterface))] public SpriteRenderer srNoPickerFreeSign;
[RequireType(true, typeof(IMyInterface))] public GameObject goNoPickerFreeSign;

RequireType