繁体   English   中英

在Unity游戏引擎中使用C#IComparer

[英]Using C# IComparer in Unity game engine

我正在为A *实现的图形编写Edge类。 我想稍后创建一个边缘列表,并使用IComparer接口根据边缘权重对它们进行排序。

下面是我在Unity 4.5f中实现的Edge类

using UnityEngine;
using System.Collections;

public class Edge : IComparer {

    Node destination;
    int weight;

    public Edge(Node destinationNode, int cost)
    {
        destination = destinationNode;
        weight = cost;
    }

    public Node getDestination()
    {
        return destination;
    }

    public int getCost()
    {
        return weight;
    }

    int IComparer.Compare(System.Object a, System.Object b)
    {
        if (((Edge)a).getCost() == ((Edge)b).getCost()) return 0;
        else if (((Edge)a).getCost() > ((Edge)b).getCost()) return 1;
        else return -1;
    }
}

但Unity抛出了以下错误

ArgumentException:没有实现正确的接口System.Collections.Generic.Comparer`1 + DefaultComparer [Edge] .Compare(。Edge x,。Edge y)(at / Users / builduser / buildslave / mono-runtime-and-classlibs / build /mcs/class/corlib/System.Collections.Generic/Comparer.cs:86)

我也尝试过使用IComparer<Edge>但它会抛出错误

非泛型类型'System.Collections.IComparer'不能与类型参数一起使用

Unity也不允许我使用IComparable,因为当我尝试使用它时它不理解:(它抛出

“未找到类型或命名空间”

我不知道什么是错的。 有人可以帮帮我吗?

我认为Unity希望您实现IComparable<Edge> (IComparable的通用版本,请参阅http://msdn.microsoft.com/fr-fr/library/4d7sx9hd%28v=vs.110%29.aspx

这个实现将非常接近你的,除了它不需要强制转换(因为输入类型已经是一些Edge)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM