Created
September 6, 2021 11:53
-
-
Save emadkhezri/544e302b73416c43ea8fd96787327efd to your computer and use it in GitHub Desktop.
Unity C# Attribute that avoid white space in the strings
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
//Attribute | |
public class NoWhiteSpaceAttribute : PropertyAttribute | |
{ | |
} | |
//Attribute-Drawer | |
[CustomPropertyDrawer(typeof(NoWhiteSpaceAttribute))] | |
public class NoWhiteSpacePropertyDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
if (property.propertyType == SerializedPropertyType.String) | |
{ | |
var stringValue = property.stringValue; | |
property.stringValue = stringValue.Replace(" ", ""); | |
} | |
EditorGUI.PropertyField(position, property, label); | |
} | |
} | |
//Example | |
/* | |
public class ExampleBehaviour : MonoBehaviour | |
{ | |
[NoWhiteSpace] public string text; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment