2016-08-14 3 views
-1

я написать небольшой скрипт для автоматического г ввести Rect Params в единство пользовательского файла шрифта, но я встретил эту ошибку:IndexOutOfRangeException - Единство C#

IndexOutOfRangeException: Индекс массива находится вне диапазона. FontCustomEditor.setRects() (в Assets/Editor/FontCustomEditor.cs: 56)

Это мой код:

using UnityEngine; 
using UnityEditor; 
using System.Collections; 

[CustomEditor(typeof(Font))] 
public class FontCustomEditor : Editor { 
    public int countX = 10; 
    public int countY = 10; 
    public string[] horOrient = new string[] { "Left to Right", "Right to Left" }; 
    public int horOrientIndex = 0; 
    public string[] verOrient = new string[] { "Down to Up", "Up to Down" }; 
    public int verOrientIndex = 0; 
    public string characters = "ABCDEFJKLMNOPQRSTUVWXY"; 
    public Font font; 

    private bool showParams; 


    public override void OnInspectorGUI() 
    { 
     DrawDefaultInspector(); 
     showParams = EditorGUILayout.Foldout(showParams, "Automatic Set Rect Positions"); 
     if (showParams) { 
      countX = EditorGUILayout.IntField("Count x", countX); 
      countY = EditorGUILayout.IntField("Count y", countY); 
      horOrientIndex = EditorGUILayout.Popup(horOrientIndex, horOrient); 
      verOrientIndex = EditorGUILayout.Popup(verOrientIndex, verOrient); 
      characters = EditorGUILayout.TextField("Characters", characters); 

      if (GUILayout.Button("Apply", GUILayout.Width(100))) 
      { 
       setRects(); 
      } 
     } 
    } 

    public void setRects() { 
     font = (Font)target; 
     float rectX = 1f/countX, rectY = 1f/countY; 
     int i, j, counter; 
     CharacterInfo[] chr = new CharacterInfo[characters.Length]; 

     for (i = 0; i < countX; i++) 
     { 
      for (j = 0; j < countY; j++) 
      { 
       counter = (i * countX + j); 

       if (counter <= characters.Length) { 

        float uvx, uvy; 

        uvx = Mathf.Abs((j - ((countX - 1) * horOrientIndex)) * rectX); 
        uvy = Mathf.Abs((i - ((countY - 1) * verOrientIndex)) * rectY); 

        chr[counter + 0] = new CharacterInfo(){ //Line 56 chr.Length = 22; counter = 0; 
         index = characters[counter] + 0, 
         uvBottomLeft = new Vector2(uvx, uvy), 
         uvBottomRight = new Vector2(uvx, uvy + rectY), 
         uvTopLeft = new Vector2(uvx + rectX, uvy), 
         uvTopRight = new Vector2(uvx + rectX, uvy + rectY) 
        }; 
       } 
      } 
     } 

     font.characterInfo = chr; 
    } 
} 

Помогите мне пожалуйста. Я не понимаю, что я делаю неправильно.

ответ

2

Когда counter <= characters.Length, в частности, когда counter == characters.Lengthcharacters[counter] выдает исключение IndexOutOfRangeException.

Индексы в C# основаны на нуле.

P.S. Ваше намерение сделать chr[counter + 0]?

+0

Я пытаюсь найти проблему в типе счетчика) – user3520223

Смежные вопросы