Unity C# - 从基础 class 动态调用 subclass
Unity C# - Calling subclass dynamically from the base class
我是 C# 和 Unity 的新手,我正在尝试弄清楚如何通过基础 class.
处理子classes
我正在制作纸牌游戏。我制作了一个 baseclass PlayerCard,其中包含子classes,例如 Minion : PlayerCard.
我目前的结构是 PlayerCard class 一开始就从牌组中这样实例化
public class PlayerDeck : MonoBehaviour{
[SerializeField] PlayerCard[] playerCardArray;
private int xPos = 2;
public void DrawCards()
{
var cardIndex = Random.Range(0, playerCardArray.Length);
AddCardToPlayer(playerCardArray[cardIndex]);
}
private void AddCardToPlayer(PlayerCard myCard)
{
Vector3 cardPos = new Vector3(transform.position.x + xPos, transform.position.y, transform.position.z);
PlayerCard newCard = Instantiate(myCard, cardPos, transform.rotation) as PlayerCard;
xPos = xPos + 2;
}
我想要我的基地class (PlayerCard) 处理所有平凡的事情,例如拖放和移动卡片,因为它们对所有卡片都是相同的。
private void OnMouseDown()
{
ToggleDrag();
}
void Update()
{
if (DraggingON) { Drag(); }
}
private void ToggleDrag()
{
Debug.Log(playerCard);
mouseOffset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, mouseScreenPoint.z));
if (DraggingON)
{
DraggingON = false;
SnapCardToPosition();
}
else
{
DraggingON = true;
SnapPositionX = transform.position.x; //Return to same x position if user has not hovered other available slot
SnapPositionY = transform.position.y; //Return to same y position if user has not hovered other available slot
//Variables above will be reset to default position x/y when user has hovered a slot but then moved object away from the slot, so reset to default
defaultPositionX = transform.position.x;
defaultPositionY = transform.position.y;
}
}
private void Drag()
{
Vector3 currentMouseScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, mouseScreenPoint.z);
Vector3 currentMousePosition = Camera.main.ScreenToWorldPoint(currentMouseScreenPoint) + mouseOffset;
transform.position = currentMousePosition;
}
我的问题在于,当调用 SnapCardToPosition 时,我希望 subclass(无论它是什么)接管并处理与它们被拖入的位置的交互。
private void SnapCardToPosition()
{
Vector2 NewCardPosition = new Vector2(SnapPositionX, SnapPositionY);
transform.position = NewCardPosition;
//Dynamically call the subclass here?
}
所以子class看起来像这样
public class Minion : PlayerCard{
[SerializeField] int Damage = 1;
public void HandleCardPlacement()
{
//Do something
}
我还没有弄清楚如何使用我的结构来实现这一点,或者它根本上是错误的吗?
- 让你的方法不是
private
(=只有这个class可以看到这个)而是protected
(=这个和任何派生的class可以看到这个)
- 并使您的方法
virtual
(if there is a common base behavior) or abstract
(if every card has to implement it from scratch) and override
在子class
在你的基地class例如
protected virtual void SnapCardToPosition()
{
Vector2 NewCardPosition = new Vector2(SnapPositionX, SnapPositionY);
transform.position = NewCardPosition;
}
然后在子class
protected override void SnapCardToPosition()
{
// make sure that the base implementation is executed first
base.SnapCardToPosition();
// do additional stuff
}
所以这里每个子class可以决定
- 是
override
方法还是简单地保持他默认的基础之一class
- 如果它覆盖它可以决定 if 和 when 通过
base.SnapCardToPosition();
[=35= 调用默认行为]
我是 C# 和 Unity 的新手,我正在尝试弄清楚如何通过基础 class.
处理子classes我正在制作纸牌游戏。我制作了一个 baseclass PlayerCard,其中包含子classes,例如 Minion : PlayerCard.
我目前的结构是 PlayerCard class 一开始就从牌组中这样实例化
public class PlayerDeck : MonoBehaviour{
[SerializeField] PlayerCard[] playerCardArray;
private int xPos = 2;
public void DrawCards()
{
var cardIndex = Random.Range(0, playerCardArray.Length);
AddCardToPlayer(playerCardArray[cardIndex]);
}
private void AddCardToPlayer(PlayerCard myCard)
{
Vector3 cardPos = new Vector3(transform.position.x + xPos, transform.position.y, transform.position.z);
PlayerCard newCard = Instantiate(myCard, cardPos, transform.rotation) as PlayerCard;
xPos = xPos + 2;
}
我想要我的基地class (PlayerCard) 处理所有平凡的事情,例如拖放和移动卡片,因为它们对所有卡片都是相同的。
private void OnMouseDown()
{
ToggleDrag();
}
void Update()
{
if (DraggingON) { Drag(); }
}
private void ToggleDrag()
{
Debug.Log(playerCard);
mouseOffset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, mouseScreenPoint.z));
if (DraggingON)
{
DraggingON = false;
SnapCardToPosition();
}
else
{
DraggingON = true;
SnapPositionX = transform.position.x; //Return to same x position if user has not hovered other available slot
SnapPositionY = transform.position.y; //Return to same y position if user has not hovered other available slot
//Variables above will be reset to default position x/y when user has hovered a slot but then moved object away from the slot, so reset to default
defaultPositionX = transform.position.x;
defaultPositionY = transform.position.y;
}
}
private void Drag()
{
Vector3 currentMouseScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, mouseScreenPoint.z);
Vector3 currentMousePosition = Camera.main.ScreenToWorldPoint(currentMouseScreenPoint) + mouseOffset;
transform.position = currentMousePosition;
}
我的问题在于,当调用 SnapCardToPosition 时,我希望 subclass(无论它是什么)接管并处理与它们被拖入的位置的交互。
private void SnapCardToPosition()
{
Vector2 NewCardPosition = new Vector2(SnapPositionX, SnapPositionY);
transform.position = NewCardPosition;
//Dynamically call the subclass here?
}
所以子class看起来像这样
public class Minion : PlayerCard{
[SerializeField] int Damage = 1;
public void HandleCardPlacement()
{
//Do something
}
我还没有弄清楚如何使用我的结构来实现这一点,或者它根本上是错误的吗?
- 让你的方法不是
private
(=只有这个class可以看到这个)而是protected
(=这个和任何派生的class可以看到这个) - 并使您的方法
virtual
(if there is a common base behavior) orabstract
(if every card has to implement it from scratch) andoverride
在子class
在你的基地class例如
protected virtual void SnapCardToPosition()
{
Vector2 NewCardPosition = new Vector2(SnapPositionX, SnapPositionY);
transform.position = NewCardPosition;
}
然后在子class
protected override void SnapCardToPosition()
{
// make sure that the base implementation is executed first
base.SnapCardToPosition();
// do additional stuff
}
所以这里每个子class可以决定
- 是
override
方法还是简单地保持他默认的基础之一class - 如果它覆盖它可以决定 if 和 when 通过
base.SnapCardToPosition();
[=35= 调用默认行为]