Skip to content

Instantly share code, notes, and snippets.

@emadkhezri
Created September 6, 2021 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emadkhezri/544e302b73416c43ea8fd96787327efd to your computer and use it in GitHub Desktop.
Save emadkhezri/544e302b73416c43ea8fd96787327efd to your computer and use it in GitHub Desktop.
Unity C# Attribute that avoid white space in the strings
//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