Automatically connect "correct components" in Unity
- Copy
ComponentConnector.cs
to your project. - Attach ComponentConnector component to root game object.
- Component Connect
Just add [ComponentConnect] attribute to your public field.
// Match with field name
[ComponentConnect] public GameObject someObject;
// Match with "AwesomeObj"
[ComponentConnect("AwesomeObj")] public GameObject awesomeObject;
- Get Component
Just add [GetComponent] attribute to your public field.
[GetComponent] public SpriteRenderer spriteRenderer;
- Button OnClick Event Connect (Bonus but not recommended)
Add [OnClick] attribute to method, and extend IComponentConnector interface to class.
public class SomeClass : MonoBehaviour, IComponentConnector {
[OnClick("SomeButton")]
public void OnClickSomeButton() { ... }
}
Component Connector basically performs a full scan operation after the change of the editor is detected or compiled.
Therefore, if there are many objects in the scene, performance degradation may occur within the editor, and in this case I recommend the following methods.
- Disable the ComponentConnector script that exists in the scene
- Perform a scan job directly using the Context Menu of the desired component
I have plan to change to a better algorithm in the future.
I do not responsibility for the issue that you encountered by using this code. But I saved my time for the projects below.
- Help Me! Endz (Google Play / App Store)
- King God Castle (Google Play / App Store)
Component Connector is under MIT license. See the LICENSE file for more info.