Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Making a GUI.textArea read-only?

Discussion in 'Scripting' started by JJJohan_legacy, Jan 4, 2011.

  1. JJJohan_legacy

    JJJohan_legacy

    Joined:
    Dec 24, 2010
    Posts:
    13
    Hello,

    I've been working on my little project for a while now and am trying to fix up all loose ends, which in this case is the menu. The user is brought to a profile selection screen and here the user creates a profile. I want the player to click on a line (which kind of contrasts my question) where the selected line is then the chosen profile. Right now I'm thinking about determining where in relation to the height of the GUI.textArea is to determine which entry was selected.

    All that is fine and dandy, it's just the hands-off policy I'm worried about:

    Right now, the list is saved to a text file and each entry is correctly displayed in the GUI.textArea. What I want to know specifically, is, if the GUI.textArea can be made read-only, as in, the player can not edit it? Is it as simple as using something other than a textArea or is there some kind of method that has to be performed? A simple Google search for 'unity3d textarea read only' didn't come up with my solution unfortunately.
     
  2. saurabh

    saurabh

    Joined:
    Sep 9, 2010
    Posts:
    70
    Hi,
    Instead of using the GUI.textarea you should try GUI.Label,as you said u want a read only value so i think GUI.Label can fulfill that requirement.
     
  3. JJJohan_legacy

    JJJohan_legacy

    Joined:
    Dec 24, 2010
    Posts:
    13
    Ah, thanks! It didn't occur to me that the 'Label' property is fine with multi-line content. That solves my problem!
     
  4. saurabh

    saurabh

    Joined:
    Sep 9, 2010
    Posts:
    70
    Most Welcome!
     
  5. MariuszKowalczyk

    MariuszKowalczyk

    Joined:
    Nov 29, 2011
    Posts:
    299
    I know this thread is old, but maybe somebody will be looking for the answer on google or something. Actually it's very easy to make the area read-only.

    Instead of using it this way:
    Code (csharp):
    1. strChat = GUILayout.TextArea(strChat);
    Just use it this way:
    Code (csharp):
    1. GUILayout.TextArea(strChat);
    Don't assign the return value to the string you are using :)

    It is better than the label solution mentioned above, because you will be able to select the text, copy it, etc.
     
    Bunny83 and aa610615623 like this.
  6. glennbg

    glennbg

    Joined:
    Nov 5, 2012
    Posts:
    3
    Old thread, but still worth correcting: Ignoring the result of TextArea() is incorrect, because on mobile the touchscreen keyboard will still open on tap. I'd suspect it'd also cause cursor changes on pointer-based platforms.
     
  7. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    626
    I know it is an old thread, but saying for those who come here for the answer and working solution.
    You can use EditorGUILayout.SelectableLabel and the TextArea or TextField GUI Style to draw a read-only TextField or TextArea, like this:
    Code (CSharp):
    1. // Readonly TextArea
    2. EditorGUILayout.SelectableLabel(text, EditorStyles.textArea, options);
    3.  
    4. // Readonly TextField
    5. EditorGUILayout.TextField(text, EditorStyles.textField, options);
    And we can have them in an external class like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public static class ExtraEditorGUILayout
    5. {
    6.  
    7.     public static void ReadonlyTextArea(string text, params GUILayoutOption[] options)
    8.     {
    9.         EditorGUILayout.SelectableLabel(text, EditorStyles.textArea, options);
    10.     }
    11.  
    12.     public static void ReadonlyTextField(string text, params GUILayoutOption[] options)
    13.     {
    14.         EditorGUILayout.TextField(text, EditorStyles.textField, options);
    15.     }
    16.  
    17. }
    Hope this helps.
    Thanks!
     
  8. celinedrules

    celinedrules

    Joined:
    Jan 31, 2013
    Posts:
    16
    Just surround the TextField with DisableGroup

    Code (CSharp):
    1. EditorGUI.BeginDisabledGroup(true);
    2. EditorGUILayout.TextField("ID", "Some Unique ID");
    3. EditorGUI.EndDisabledGroup();
     
    Rupture13 likes this.