0

I want to limit my string max length in text area in my custom inspector. I have tried to make code as below:

if(EditorGUI.EndChangeCheck()){
        if(_dTarget.mazeNumData.Length >= 338){
            _dTarget.mazeNumData.Remove((_dTarget.mazeNumData.Length - 1) - 3, 3);
        }
}

But it didn't work.Is anything wrong with my code?

My full Code :

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(DigitTotal))]
public class DigitTotalEditor : Editor {

    private DigitTotal _dTarget;

    public override void OnInspectorGUI() {
        _dTarget = (DigitTotal)target;

        DrawDefaultInspector();
        DrawCustomInspector();
    }

    void DrawCustomInspector() {
        GUIStyle guiStyle = EditorStyles.textArea;
        guiStyle.wordWrap = true;

        EditorGUI.BeginChangeCheck();

        _dTarget.mazeNumData = EditorGUILayout.TextArea(_dTarget.mazeNumData, guiStyle, new GUILayoutOption[] 
        { 
            GUILayout.Height(100f),
            GUILayout.Width(250f),
        });

        if(EditorGUI.EndChangeCheck()){
            if(_dTarget.mazeNumData.Length >= 338){
                _dTarget.mazeNumData.Remove((_dTarget.mazeNumData.Length - 1) - 3, 3);
            }
        }

        GUILayout.Space(5f);
        GUILayout.Label("Digits : " + _dTarget.mazeNumData.Length, EditorStyles.boldLabel);
    }
}
Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
Ali Akbar
  • 39
  • 10

1 Answers1

0

Define some constant length:

const int MAX_LEN = 1337;

Define some placeholder for the input:

string inputString = string.Empty;

In OnGUI() method just before printing ( assigning the string into GUI element ) do something like this:

//.. in OnGui before printing the text
if(inputString.Length > MAX_LEN)
    inputString = inputString.Substring(0, MAX_LEN - 1);

EDIT:

Editing your code to this :

if(dTarget.mazeNumData.Length > MAX_LEN)
    _dTarget.mazeNumData = _dTarget.mazeNumData.Substring(0, MAX_LEN - 1); 

_dTarget.mazeNumData = EditorGUILayout.TextArea(_dTarget.mazeNumData, guiStyle, new GUILayoutOption[] 
    { 
        GUILayout.Height(100f),
        GUILayout.Width(250f),
    });

Should do the trick.

mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
  • thanks for the answer. I changed my code as you suggested but still it didn't work. Here's the code : http://pastebin.com/n54XNKwQ – Ali Akbar Jan 12 '17 at 11:04
  • @AliAkbar edited your pastebin: [http://pastebin.com/Uu8C9bTQ](http://pastebin.com/Uu8C9bTQ) – mrogal.ski Jan 12 '17 at 11:12
  • thanks that worked ! but the problem is the last 3 chars will only got removed if I do click somewhere outside the text area :| – Ali Akbar Jan 12 '17 at 11:24