Code Monkey home page Code Monkey logo

unity-auto-attach-component-attributes's Introduction

Unity auto attach component via attributes

Forum Thread https://forum.unity.com/threads/auto-attach-components-via-attributes.928098/

Installation

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.

Preview video

Play

Example

 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

About

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

unity-auto-attach-component-attributes's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.