- Home /
"'UnityEngine.GUI.DoTextField' is inaccessible due to its protection level."
I found a very cool snippet of code by Shaun on this page: http://forum.unity3d.com/threads/14505-non-selectable-textarea
And tweaked it a bit to come up with this:
var searchFieldID : int = GUIUtility.GetControlID(FocusType.Keyboard);
var searchFieldRect : Rect = GUILayoutUtility.GetRect((worldListFilter == "" ? 400 : 255), 28, "textfield");
var worldListFilterGUI : GUIContent = GUIContent(worldListFilter);
GUI.DoTextField(searchFieldRect, searchFieldID, worldListFilterGUI, false, (worldListFilter == "" ? 400 : 255), "TextField");
worldListFilter = worldListFilterGUI.text;
if (!searchFieldRect.Contains(Event.current.mousePosition) && GUIUtility.keyboardControl != searchFieldID && String.IsNullOrEmpty(worldListFilter))
GUI.Label(searchFieldRect, "Title, Author, or Description","textfieldhint");
It's supposed to display a search field UI style alt text when the user isn't ineracting with the field, but alas - won't compile and instead Unity throws an "'UnityEngine.GUI.DoTextField' is inaccessible due to its protection level" error.
Any ideas?
Answer by Eric5h5 · Dec 28, 2010 at 07:56 AM
There is no GUI.DoTextField function. Or rather, there is, but it's private (as the error basically says). Private APIs can no longer be accessed in Unity 3, so they might as well not exist...they were never intended to be used by user code in the first place, and are subject to change at any time.
Too bad. I nearly drove myself insane yesterday trying to set up a live search field that didn't loose focus as soon as the user entered the first char. Whenever the user would enter something, the rest of the GUI would change - and this would cause the field's id to change, and this would cause the field to loose focus. I could detect this and re-add the focus as the user was typing, but it would select everything in the field and clear the text they had already entered. Incredibly annoying, and I couldn't find any way to work around it...
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Reverse GUILayout 1 Answer
For Loop GUILayout.Label Problems 1 Answer
Open gui if player clicks on button 2 Answers