Hi there,
I created attributes, which are auto attach components to your fields
Github : GitHub - Nrjwolf/unity-auto-attach-component-attributes: Auto attach components in inspector
About
Once I was faced with the problem of null checking in unity. You can read about this here Unity Blog
In short, if you use something like
private Transform m_CachedTransform
public Transform transform
{
get
{
if (m_CachedTransform == null)
m_CachedTransform = InternalGetTransform();
return m_CachedTransform;
}
}
You will have performance problem, because unity overrides ==, also you with this point, you should be carreful with ? operator.
So, I started to think, how to attach components without caching it every time in Awake/Start functions.
Example of using :
[FindObjectOfType]
[SerializeField] private Camera m_Camera;
[GetComponent]
[SerializeField] private Image m_Image;
[GetComponentInChildren(true)] // include inactive
[SerializeField] private Button m_Button;
[GetComponentInChildren("Buttons/Button1")] // Get the component from the children by path "Buttons/Button1" in hierarchy
[SerializeField] private Button m_Button;
[AddComponent] // Add component in editor and attach it to field
[SerializeField] private SpringJoint2D m_SpringJoint2D;
Also, you can easily turn it on/off, because attributes will not give you opportunity to use default drag&drop
Enjoy!
Telegram : https://t.me/nrjwolf_live
Discord : https://discord.gg/jwPVsat
Reddit : https://www.reddit.com/r/Nrjwolf/
Twitter : https://twitter.com/nrjwolf