Last active
November 10, 2024 13:09
-
-
Save LotteMakesStuff/dd785ff49b2a5048bb60333a6a125187 to your computer and use it in GitHub Desktop.
Code running pack! two property drawing scripts that make it super easy to trigger code via buttons in the inspector, and get feedback! [TestButton] draws you little buttons that call a method on your MonoBehaviour, and [ProgressBar] give you a great way to show feedback from long running methods. These are *super* helpful for things like proced…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class ProgressBarAttribute : PropertyAttribute | |
{ | |
public bool hideWhenZero; | |
public string label; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
[CustomPropertyDrawer(typeof(ProgressBarAttribute))] | |
public class ProgressBarDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
if (property.propertyType != SerializedPropertyType.Float) | |
{ | |
GUI.Label(position, "ERROR: can only apply progress bar onto a float"); | |
return; | |
} | |
if ((attribute as ProgressBarAttribute).hideWhenZero && property.floatValue <= 0) | |
return; | |
var dynamicLabel = property.serializedObject.FindProperty((attribute as ProgressBarAttribute).label); | |
EditorGUI.ProgressBar(position, property.floatValue/1f, dynamicLabel == null ? property.name : dynamicLabel.stringValue); | |
} | |
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | |
{ | |
if ((attribute as ProgressBarAttribute).hideWhenZero && property.floatValue <= 0) | |
return 0; | |
return base.GetPropertyHeight(property, label); | |
} | |
} |
@dearamy this is an error thrown by unity when you GameObject.BroadcastMessage(..) in the editor. It dosnt show if your in play mode. Its super annoying, but totally harmless.
Do note that GameObject.BroadcastMessage
calls that function in every MonoBehaviour on the GameObject, which might not be what you want in case you use this attribute on multiple components.
@Flassari Yes, you need to be careful with naming your methods!
Vote here to fix the error generated by GameObject.BroadcastMessage(): https://issuetracker.unity3d.com/issues/assertion-failed-on-expression-shouldrunbehaviour-error-is-thrown-when-prefab-with-custom-script-is-edited
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Always got this error: "Assertion failed on expression: 'ShouldRunBehaviour ()'
UnityEngine.GameObject:BroadcastMessage(String)
ButtonDrawer:OnGUI(Rect) (at Assets/Attributes/Editor/TestButtonDrawer.cs:21)
UnityEditor.DockArea:OnGUI()"
I'm using Unity5.5.1