Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements "RequiredType" validation attribute #346

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

demirmusa
Copy link

@demirmusa demirmusa commented Nov 23, 2022

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 #351

@demirmusa demirmusa changed the title Implements RequiredType attribute Implements "RequiredType" validation attribute Nov 23, 2022
@demirmusa
Copy link
Author

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

@demirmusa
Copy link
Author

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

@dbrizov
Copy link
Owner

dbrizov commented Feb 2, 2023

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
Copy link
Author

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
Copy link

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

@TylerTemp
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Request] What about adding "RequiredType" validation attribute
4 participants