[ad_1]
I used to be searching for the identical factor as OP and did not discover a solution right here, so this is my answer:
Imports:
utilizing System;
utilizing UnityEditor;
utilizing UnityEngine;
Simplest implementation:
string TextField(string textual content, string placeholder) {
var newText = EditorGUILayout.TextField(textual content);
if (String.IsNullOrEmpty(textual content)) {
var guiColor = GUI.shade;
GUI.shade = Color.gray;
EditorGUI.LabelField(GUILayoutUtility.GetLastRect(), placeholder);
GUI.shade = guiColor;
}
return newText;
}
Implementation supporting each TextField and TextArea utilizing generic methodology, in addition to inserting Label on the similar spot the place Text is rendered:
string TextField(string textual content, string placeholder) {
return TextEnter(textual content, placeholder);
}
string TextArea(string textual content, string placeholder) {
return TextEnter(textual content, placeholder, space: true);
}
personal string TextEnter(string textual content, string placeholder, bool space = false) {
var newText = space ? EditorGUILayout.TextArea(textual content) : EditorGUILayout.TextField(textual content);
if (String.IsNullOrEmpty(textual content.Trim())) {
const int textMargin = 2;
var guiColor = GUI.shade;
GUI.shade = Color.gray;
var textual contentRect = GUILayoutUtility.GetLastRect();
var place = new Rect(textual contentRect.x + textMargin, textual contentRect.y, textual contentRect.width, textual contentRect.top);
EditorGUI.LabelField(place, placeholder);
GUI.shade = guiColor;
}
return newText;
}
Usage:
string textual content = String.Empty;
string space = String.Empty;
void OnGUI() {
textual content = TextField(textual content, "Field placeholder");
space = TextArea(space, "Area placeholder");
}
Here’s the way it seems in Unity:
[ad_2]