我编写了一个小脚本,用于在统一自定义字体文件中自动引入rect参数但我遇到了这个错误:
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;
}
}
请帮帮我。我无法理解我做错了什么。
答案 0 :(得分:2)
特别是counter <= characters.Length
counter == characters.Length
时characters[counter]
IndexOutOfRangeException
会引发C#
例外。
chr[counter + 0]
中的索引从零开始。
P.S。你打算做什么 var deeper = function deeper() {
body.innerHTML = this.state;
}.bind(this);
obj.deep = obj.deep.bind(this); // you can bind an object method, and then reassign it to the original property
?