C# 如何让Unity 2D RTS装置选择工作?
我正在制作一个2d RTS风格的游戏,在这个游戏中,你可以通过点击和拖动选择框来选择游戏中的单位。当某个单元位于该选择框内且鼠标左键被释放时,该单元将被添加到选定列表中,其动画将从空闲动画更改为选定的空闲动画。再次绘制选择框后,将删除选定列表中的所有单位,并将其动画设置回空闲位置,然后将新选择框中的所有单位添加到列表中并设置为选定的空闲动画。问题是,如果“新选定”框中的这些单元中的任何一个之前已选定,则它们将切换到空闲动画并停留在那里,而不是转到选定动画。我在没有动画的情况下测试了它,它工作了,当再次选择时,它将保持为选定的精灵,而不是切换到空闲精灵并保持在那里。我是C语言的新手,所以这可能是一个非常简单的问题 谢谢 代码如下:C# 如何让Unity 2D RTS装置选择工作?,c#,unity3d,C#,Unity3d,我正在制作一个2d RTS风格的游戏,在这个游戏中,你可以通过点击和拖动选择框来选择游戏中的单位。当某个单元位于该选择框内且鼠标左键被释放时,该单元将被添加到选定列表中,其动画将从空闲动画更改为选定的空闲动画。再次绘制选择框后,将删除选定列表中的所有单位,并将其动画设置回空闲位置,然后将新选择框中的所有单位添加到列表中并设置为选定的空闲动画。问题是,如果“新选定”框中的这些单元中的任何一个之前已选定,则它们将切换到空闲动画并停留在那里,而不是转到选定动画。我在没有动画的情况下测试了它,它工作了,
ControllableUnits ControlUnitSelected;
public List<ControllableUnits> selectedControllableUnits;
private Vector3 mousePosition;
public void Awake()
{
selectedControllableUnits = new List<ControllableUnits>();
selectionAreaTransform.gameObject.SetActive(false);
ControlUnitSelected = this.gameObject.GetComponent<ControllableUnits>();
}
public void Update()
{
//Transforming Selection Area
if (Input.GetMouseButton(0))
{
Vector3 currentMousePosition = UnitControlClass.GetMouseWorldPosition();
Vector3 lowerLeft = new Vector3(
Mathf.Min(mousePosition.x, currentMousePosition.x),
Mathf.Min(mousePosition.y, currentMousePosition.y)
);
Vector3 upperRight = new Vector3(
Mathf.Max(mousePosition.x, currentMousePosition.x),
Mathf.Max(mousePosition.y, currentMousePosition.y)
);
selectionAreaTransform.position = lowerLeft;
selectionAreaTransform.localScale = upperRight - lowerLeft;
}
if (Input.GetMouseButtonDown(0))
{
mousePosition = UnitControlClass.GetMouseWorldPosition();
selectionAreaTransform.gameObject.SetActive(true);
}
//Creating the selection process
if (Input.GetMouseButtonUp(0))
{
Collider2D[] collider2DArray = Physics2D.OverlapAreaAll(mousePosition, UnitControlClass.GetMouseWorldPosition());
foreach (ControllableUnits control in selectedControllableUnits)
{
//if(ControlUnitSelected != null)
control.animPeasant.Play("Peasant_Idle");
control.isSelected = false;
}
selectedControllableUnits.Clear();
foreach (Collider2D collider2D in collider2DArray)
{
ControllableUnits controllableUnit = collider2D.GetComponent<ControllableUnits>();
if (controllableUnit != null)
{
controllableUnit.animPeasant.Play("Peasant_Idle_Selected");
controllableUnit.isSelected = true;
selectedControllableUnits.Add(controllableUnit);
}
}
selectionAreaTransform.gameObject.SetActive(false);
}
}
我想问题是当你多次调用Play时,Unity试图合并这些动画,最终导致一个随机状态 它可以通过给出精确的播放时间来解决
controllableUnit.animPeasant.Play("Peasant_Idle_Selected", 0, 0f);
检查animator窗口,查看当两次选择该单元时,状态如何变化。这是一个视频,显示了正在发生的事情,状态似乎在空闲选择和常规空闲之间来回移动。很抱歉,响应太晚,但它确实起了作用,感谢我在这方面停留了一段时间。