Giter VIP home page Giter VIP logo

easybuttons's Introduction

Easy buttons for the Unity default inspector

openupm

These tiny scripts add the ability to quickly show a button in the inspector for any method.

Installation

OpenUPM

Once you have the OpenUPM cli, run the following command:

openupm install com.madsbangh.easybuttons

Or if you don't have it, add the scoped registry to manifest.json with the desired version:

  "scopedRegistries": [
    {
      "name": "package.openupm.com",
      "url": "https://package.openupm.com",
      "scopes": [
        "com.madsbangh.easybuttons",
        "com.openupm"
      ]
    }
  ],
  "dependencies": {
    "com.madsbangh.easybuttons": "1.4.0"
  }

Git URL

Project supports Unity Package Manager. To install the project as a Git package do the following:

  1. In Unity, open Window -> Package Manager.
  2. Press the + button, choose "Add package from git URL..."
  3. Enter "https://github.com/madsbangh/EasyButtons.git#upm" and press Add.

Unity Package

Alternatively, you can add the code directly to the project:

  1. Clone the repo or download the latest release.
  2. Add the EasyButtons folder to your Unity project or import the .unitypackage

How To Use

  1. Add the Button attribute to a method.

    using EasyButtons; // 1. Import the namespace
    using UnityEngine;
    
    public class ButtonsExample : MonoBehaviour
    {
        // 2. Add the Button attribute to any method.
    	[Button]
    	public void SayHello()
        {
            Debug.Log("Hello");
        }
    }
  2. You should now see a button at the top of the component with the same name as the method:

    Button in the inspector

    Result

  3. Add the Button attribute to a method with parameters.

    using EasyButtons;
    using UnityEngine;
    
    public class Test : MonoBehaviour
    {
        [Button]
        public void ButtonWithParameters(string message, int number)
        {
            Debug.Log($"Your message #{number}: '{message}'");
        }
    }
  4. You can now enter parameter values and invoke the method in the inspector:

    Button with parameters

Attribute Options

The Button attribute has different options that allow customizing the button look.

Mode - indicates when the button is enabled. You can choose between the following options:

  • AlwaysEnabled - the button is enabled in edit mode and play mode.

  • EnabledInPlayMode - the button is only enabled in play mode.

  • DisabledInPlayMode - the button is only enabled in edit mode.

Spacing - allows to have some space before or after the button. The following options can be used:

  • None - no spacing at all.

  • Before - adds space above the button.

  • After - adds space below the button.

Expanded - whether to expand the parameters foldout by default. It only works with buttons that have parameters.

Custom Editors

If you want to show buttons in a custom editor, you can use the ButtonsDrawer class defined in EasyButtons.Editor.

Instantiate ButtonsDrawer in OnEnable if possible, then draw the buttons with help of the DrawButtons method, as in the example:

[CustomEditor(typeof(Example))]
public class CustomEditor : ObjectEditor
{   
    private ButtonsDrawer _buttonsDrawer;

    private void OnEnable()
    {
        _buttonsDrawer = new ButtonsDrawer(target);
    }

    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        _buttonsDrawer.DrawButtons(targets);
    }
}

You can also draw only specific buttons:

// Draw only the button called "Custom Editor Example"
_buttonsDrawers.Buttons.First(button => button.DisplayName == "Custom Editor Example").Draw(targets);

You can search for a specific button using its DisplayName or Method (MethodInfo object the button is attached to.)

Notes

  • Older versions of Unity might not understand the reference to EasyButtons runtime in the EasyButtons editor assembly definition. If you experience issues, you can re-add the reference, or remove the asmdefs completely.

easybuttons's People

Contributors

amyadzuki avatar bulleador avatar lawendt avatar madsbangh avatar omarwkh avatar rfadeev avatar ribbanya avatar semantic-release-bot avatar solidalloy avatar trulyspinach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

easybuttons's Issues

Button does not seem to fire function for instances of a class?

When adding a button and then adding a component which uses the button, pressing the button does not seem to fire the function on the component. When using it to instantiate objects in editor, it appears to not actually retrieve the transform of the object it is being fired on.

Semantic release CI action is broken

It seems the semantic release is not carried out automatically anymore. Some dependencies are deprecated.

I am not very familiar with semantic-release-bot, but will have a look when I have some time.

In the meanwhile, if anyone with experience wants to have a look, please don't hesitate to help out <3

Multi-object editing not supported

Multi-selecting objects of the same type, which use EasyButtons, yields the message: "Multi-object editing not supported" as shown on the screenshot below.

Usually a custom editor would be decorated with the CanEditMultipleObjects attribute to allow for multi-object editing.

There is no good reason for disallowing multi-object editing by default when using EasyButtons.

dv7ioerwaaei0p2

Mode button modes

Hey, love this library. I have a case where I need a button this is only enabled in play mode (because it starts an animation sequence for testing).

Maybe a ButtonMode.EnabledInPlayModeOnly?

Feel free to close this if it seems too niche, I can see this is probably a rare case and might start eroding the "easy" in EasyButtons.

Feature request - support for lists/arrays?

Hello! I've not looked into this but I was wondering if support for lists/arrays could, in theory, be added? They're serializable types, but I'm not sure how this implementation works.
Ta!

Doesn't work on ScriptableObjects

Button doesn't appear for scriptable objects, which is quite useful.

One solution is to duplicate the editor code, and change the editor type to ScriptableObject as such:
[CustomEditor(typeof(ScriptableObject), true)]
or change the original editor type to Object, which will work for both scriptable objects and monobehaviours. I'm just not sure about any other implications it can cause.

Custom editor must inherit ObjectEditor for button attribute to work

If there is custom editor for the class which uses Button attribute, buttons won't be drawn if this custom editor is inherited from Editor. So this code wont's show any buttons in the inspector

[CustomEditor(typeof(ButtonsExample))]
public class ButtonsExampleEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        // Other custom editor code
    }
 }

Changing to inheriting ObjectEditor helps

[CustomEditor(typeof(ButtonsExample))]
public class ButtonsExampleEditor : ObjectEditor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        // Other custom editor code
    }
 }

But this means for any script which uses Button attribute, custom editor inheritance should be changed (which can be undesired and sometimes not possible). I tried to tackle the problem with PropertyDrawer and DecoratorDrawer but they work only on fields, so no idea for how to solve this. Maybe it's worth mentioning in readme.

Use UI Toolkit rendering instead of old IMGUI - Code provided

In "ObjectEditor.cs" just replace

public override void OnInspectorGUI() { DrawDefaultInspector(); _buttonsDrawer.DrawButtons(targets); }

with

public override VisualElement CreateInspectorGUI()
{
	VisualElement container = new VisualElement();

	InspectorElement.FillDefaultInspector(container, serializedObject, this);

	IMGUIContainer iMGUIContainer = new IMGUIContainer(() =>
	{
		_buttonsDrawer.DrawButtons(targets);
	});
	container.Add(iMGUIContainer);

	return container;
}

It's just more performant in the Editor.

Doesn't properly serialise Vector2Int

A relatively new datatype albeit, Vector2Int does indeed support native Unity serialisation (and is even present in the SerializedProperty datatype enum). EasyButtons does not recognise it as such.

Expected Behaviour:

  1. Define a method with the [Button] attribute containing a Vector2Int parameter.
  2. EasyButtons shows a method dropdown in the inspector, allowing the user to enter a Vector2Int value.
    image

Actual Behaviour:

  1. EasyButtons shows an error message stating "Unable to draw a non-serialized type."
    image

Move example scripts to separate "Examples" directory

The example scripts are mixed with the actual tool code, making it harder to get an overview of the relevant parts. They should be moved to a separate subdirectory:
EasyButtons/Examples and
EasyButtons/Examples/Editor

Space between buttons

Thanks for the awesome attribute, it's a total Unity lifesaver. Is there a way to easily add spaces between buttons (like how [Space] will add empty space to an inspector when placed before a field)?

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.