我的Unity项目目前存在很大问题。我正在尝试向SortedList添加一定数量的元素,但是当我开始游戏时,我收到了这个错误:
错误:“InvalidOperationException:由于操作无效,操作无效 对象的当前状态 System.Collections.Generic.SortedList
2[TrisCoordinates,System.Boolean].PutImpl (.TrisCoordinates key, Boolean value, Boolean overwrite) System.Collections.Generic.SortedList
2 [TrisCoordinates,System.Boolean]。新增 (.TrisCoordinates键,布尔值)“
以下是代码:
void AddCoords() {
coords.Add (new TrisCoordinates (280.62f, 2.217f, 144.84f), false);
coords.Add (new TrisCoordinates (280.65f, 0.9891f, 126.84f), false);
coords.Add (new TrisCoordinates (285.42f, 0.9891f, 129.49f), false);
coords.Add (new TrisCoordinates (281.64f, 2.9645f, 125.05f), false);
coords.Add (new TrisCoordinates (263.00f, 2.2f, 103.27f), false);
coords.Add (new TrisCoordinates (243.29f, 0.9891f, 101.96f), false);
coords.Add (new TrisCoordinates (243.10f, 0.9891f, 99.587f), false);
coords.Add (new TrisCoordinates (233.20f, 1.018f, 93.414f), false);
coords.Add (new TrisCoordinates (249.77f, 0.9891f, 112.95f), false);
coords.Add (new TrisCoordinates (247.87f, 0.9891f, 113.00f), false);
coords.Add (new TrisCoordinates (242.59f, 0.9891f, 112.92f), false);
coords.Add (new TrisCoordinates (240.59f, 0.9891f, 112.99f), false);
coords.Add (new TrisCoordinates (236.85f, 0.9891f, 112.99f), false);
coords.Add (new TrisCoordinates (234.79f, 0.9891f, 112.98f), false);
coords.Add (new TrisCoordinates (248.88f, 0.9891f, 126.71f), false);
coords.Add (new TrisCoordinates (231.15f, 1.0182f, 148.53f), false);
coords.Add (new TrisCoordinates (270.95f, 7.95f, 137.46f), false);
coords.Add (new TrisCoordinates (278.60f, 7.95f, 137.60f), false);
coords.Add (new TrisCoordinates (281.85f, 7.9292f, 137.60f), false);
coords.Add (new TrisCoordinates (270.58f, 7.95f, 135.05f), false);
coords.Add (new TrisCoordinates (264.75f, 9.8372f, 118.75f), false);
coords.Add (new TrisCoordinates (256.27f, 7.93f, 103.37f), false);
coords.Add (new TrisCoordinates (205.63f, 1.037f, 407.70f), false);
coords.Add (new TrisCoordinates (209.02f, 1.037f, 407.70f), false);
coords.Add (new TrisCoordinates (216.61f, 1.037f, 407.70f), false);
coords.Add (new TrisCoordinates (219.56f, 1.037f, 407.70f), false);
coords.Add (new TrisCoordinates (226.32f, 1.037f, 403.22f), false);
coords.Add (new TrisCoordinates (226.03f, 1.037f, 401.04f), false);
coords.Add (new TrisCoordinates (225.98f, 1.037f, 394.01f), false);
coords.Add (new TrisCoordinates (225.97f, 1.037f, 392.41f), false);
coords.Add (new TrisCoordinates (202.00f, 1.037f, 392.56f), false);
coords.Add (new TrisCoordinates (201.03f, 1.037f, 394.67f), false);
}
TrisCoordinates是一个如此制作的课程:
public class TrisCoordinates {
private float x;
private float y;
private float z;
public TrisCoordinates(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public float GetX() {
return x;
}
public float GetY() {
return y;
}
public float GetZ() {
return z;
}
}
关于是什么原因的任何想法?
答案 0 :(得分:5)
如果要将类用作SortedList的键,则应该实现IComparable,因为为了能够排序,排序列表应该能够比较两个TrisCoordinates实例。
以下是虚拟比较,但它至少应该在您的代码中没有例外。
public class TrisCoordinates : IComparable<TrisCoordinates>
{
private float x;
private float y;
private float z;
public TrisCoordinates(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public float GetX()
{
return x;
}
public float GetY()
{
return y;
}
public float GetZ()
{
return z;
}
public int CompareTo(TrisCoordinates other)
{
return (x + y + z).CompareTo(other.x + other.y + other.z);
}
}
答案 1 :(得分:0)
您使用排序列表,程序应该能够对列表进行排序,因为您的类不能实现IComparable,这是不可能的。您只需要继承它:public class TrisCoordinates : IComparable<TrisCoordinates>
并实施public int CompareTo(TrisCoordinates otherCoordinates)
以使其有效:
public class TrisCoordinates : IComparable<TrisCoordinates>
{
public float X { get; private set; }
public float Y { get; private set; }
public float Z { get; private set; }
public TrisCoordinates(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
public int CompareTo(TrisCoordinates otherCoordinates)
{
if (X > otherCoordinates.X)
return 1;
if (X < otherCoordinates.X)
return -1;
if (Y > otherCoordinates.Y)
return 1;
if (Y < otherCoordinates.Y)
return -1;
if (Z > otherCoordinates.Z)
return 1;
if (Z < otherCoordinates.Z)
return -1;
return 0;
}
}