Forum Thread https://forum.unity.com/threads/auto-attach-components-via-attributes.928098/
Add this as a package to your project by adding the below as an entry to the dependencies in the /Packages/manifest.json
file:
"nrjwolf.games.attachattributes": "https://github.com/Nrjwolf/unity-auto-attach-component-attributes.git"
For more information on adding git repositories as a package see the Git support on Package Manager in the Unity Documentation.
using Nrjwolf.Tools.AttachAttributes;
[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;
[GetComponentInParent] // Get component from parent
[SerializeField] private Canvas m_Canvas;
Now all components will automatically attach when you select your gameobject in hierarchy
You can turn it on/off in component context menu or via Tools/Nrjwolf/AttachAttributes
This asset help you to auto attach components into your serialized fields in inpector. I started use it to avoid every time assign components in Awake/Start
function.
So, you can ask why I need it? Well, maybe you use code like this and do not know, that this is bad for perfomance
private Transform m_CachedTransform
public Transform transform
{
get
{
if (m_CachedTransform == null)
m_CachedTransform = InternalGetTransform();
return m_CachedTransform;
}
}
You can read about here: https://blogs.unity3d.com/ru/2014/05/16/custom-operator-should-we-keep-it/
Telegram : https://t.me/nrjwolf_games
Discord : https://discord.gg/jwPVsat
Reddit : https://www.reddit.com/r/Nrjwolf/
Twitter : https://twitter.com/nrjwolf