This solution includes a custom attribute PercentageAttribute
and a property drawer PercentDrawer
that work together
to provide a convenient way of displaying float values as percentages in the Unity Editor.
Open Unity and go to Window
> Package Manager
.
Click the +
button in the top left corner.
Select Add package from git URL...
.
Enter the following URL: https://github.com/RimuruDev/Unity-PercentDrawer.git
Click Add
to install the package.
If error -
Library/PackageCache/unity-percent-drawer@390d399580/Sources/Editor/PercentDrawer.cs(22,34):
error CS0246: The type or namespace name 'PercentageAttribute' could not be found (are you missing a using directive or an assembly reference?)
Go next -> Option 2: Install from Releases
.unitypackage
file.Assets
> Import Package
> Custom Package...
..unitypackage
file and import it into your project.Editor
within your project's Assets
directory if it does not already exist. Unity requiresEditor
.Attributes
within your Assets
directory to store custom attributes.PercentageAttribute.cs
file inside the Attributes
folder.PercentDrawer.cs
file inside the Editor
folder.Your folder structure should look like this:
Assets/
│
├── Editor/
│ └── PercentDrawer.cs
│
└── Attributes/
└── PercentageAttribute.cs
To use the PercentageAttribute
, you first need to define it in a script file:
// PercentageAttribute.cs
using UnityEngine;
public class PercentageAttribute : PropertyAttribute {}
Then, apply the [Percentage] attribute to any float field in your MonoBehaviour or ScriptableObject classes:
// SomeScript.cs
using UnityEngine;
public class SomeScript : MonoBehaviour
{
[Percentage]
public float dropChance;
}
When you select a GameObject with this script attached in the Unity Editor, you'll see the dropChance field displayed as a slider with a percentage value next to it.
If you wish to customize the behavior of the PercentDrawer
, you can edit the PercentDrawer.cs
file within
the Editor
folder. Comments in the code provide guidance on what each part does and how to adjust the layout.
For support, questions, or contributions, please open an issue on the repository's issue tracker.
Contributing
Contributions to improve the PercentageAttribute
and PercentDrawer
are welcome. Please follow the standard pull
request process to submit improvements.
This project is licensed under the MIT License - see the LICENSE file for details.
Made by RimuruDev