Unity3d 换卡位置
我正在unity中创建一个纸牌游戏。我有4张卡片,当点击一个按钮时,我在4个区域上随机实例化了16次。每个区域包含4张随机卡。我想知道如何知道哪张牌在哪一个区域,然后将该特定牌的位置更改为另一个区域 这是附在我每张卡片上的脚本:Unity3d 换卡位置,unity3d,Unity3d,我正在unity中创建一个纸牌游戏。我有4张卡片,当点击一个按钮时,我在4个区域上随机实例化了16次。每个区域包含4张随机卡。我想知道如何知道哪张牌在哪一个区域,然后将该特定牌的位置更改为另一个区域 这是附在我每张卡片上的脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Click : MonoBehaviour { public GameObj
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Click : MonoBehaviour
{
public GameObject Canvas;
public GameObject MyArea;
private bool isDragging=false;
private bool isOverBottomArea=false;
private GameObject BottomArea;
private GameObject startParent;
private Vector2 startPosition;
private void Awake()
{
MyArea=GameObject.Find("MyArea");
Canvas=GameObject.Find("Canvas");
}
// Update is called once per frame
void Update()
{
if (isDragging)
{
transform.position= new Vector2(Input.mousePosition.x,Input.mousePosition.y);
transform.SetParent(Canvas.transform, true);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
isOverBottomArea=true;
BottomArea=collision.gameObject;
}
private void OnCollisionExit2D(Collision2D collision)
{
isOverBottomArea=false;
BottomArea=null;
}
public void StartDrag()
{
startParent= transform.parent.gameObject;
startPosition=transform.position;
if(startParent != MyArea)
{
isDragging=false;
}
else
{
isDragging=true;
}
}
public void EndDrag()
{
isDragging=false;
if (isOverBottomArea && startParent==MyArea )
{
transform.SetParent(BottomArea.transform, false);
}
else
{
transform.position=startPosition;
transform.SetParent(startParent.transform, false);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawCards : MonoBehaviour
{
public GameObject Card1;
public GameObject Card2;
public GameObject Card3;
public GameObject Card4;
public GameObject MyArea;
public GameObject LeftArea;
public GameObject RightArea;
public GameObject BottomArea;
List<GameObject> cards = new List<GameObject>();
void Start()
{
cards.Add(Card1);
cards.Add(Card2);
cards.Add(Card3);
cards.Add(Card4);
}
int count1 = 0;
public void OnClick()
{
count1++;
if (count1 == 1)
{
for (var i=0; i<4; i++)
{
GameObject playerCard = Instantiate(cards[Random.Range(0, cards.Count)], new Vector3(0,0,0), Quaternion.identity);
playerCard.transform.SetParent(MyArea.transform, false);
GameObject leftCard = Instantiate(cards[Random.Range(0, cards.Count)], new Vector3(0,0,0), Quaternion.identity);
leftCard.transform.SetParent(LeftArea.transform, false);
GameObject rightCard = Instantiate(cards[Random.Range(0, cards.Count)], new Vector3(0,0,0), Quaternion.identity);
rightCard.transform.SetParent(RightArea.transform, false);
GameObject bottomCard = Instantiate(cards[Random.Range(0, cards.Count)], new Vector3(0,0,0), Quaternion.identity);
bottomCard.transform.SetParent(BottomArea.transform, false);
}
}
}
}
这是附在按钮上的脚本,单击该按钮可实例化16张卡:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Click : MonoBehaviour
{
public GameObject Canvas;
public GameObject MyArea;
private bool isDragging=false;
private bool isOverBottomArea=false;
private GameObject BottomArea;
private GameObject startParent;
private Vector2 startPosition;
private void Awake()
{
MyArea=GameObject.Find("MyArea");
Canvas=GameObject.Find("Canvas");
}
// Update is called once per frame
void Update()
{
if (isDragging)
{
transform.position= new Vector2(Input.mousePosition.x,Input.mousePosition.y);
transform.SetParent(Canvas.transform, true);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
isOverBottomArea=true;
BottomArea=collision.gameObject;
}
private void OnCollisionExit2D(Collision2D collision)
{
isOverBottomArea=false;
BottomArea=null;
}
public void StartDrag()
{
startParent= transform.parent.gameObject;
startPosition=transform.position;
if(startParent != MyArea)
{
isDragging=false;
}
else
{
isDragging=true;
}
}
public void EndDrag()
{
isDragging=false;
if (isOverBottomArea && startParent==MyArea )
{
transform.SetParent(BottomArea.transform, false);
}
else
{
transform.position=startPosition;
transform.SetParent(startParent.transform, false);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawCards : MonoBehaviour
{
public GameObject Card1;
public GameObject Card2;
public GameObject Card3;
public GameObject Card4;
public GameObject MyArea;
public GameObject LeftArea;
public GameObject RightArea;
public GameObject BottomArea;
List<GameObject> cards = new List<GameObject>();
void Start()
{
cards.Add(Card1);
cards.Add(Card2);
cards.Add(Card3);
cards.Add(Card4);
}
int count1 = 0;
public void OnClick()
{
count1++;
if (count1 == 1)
{
for (var i=0; i<4; i++)
{
GameObject playerCard = Instantiate(cards[Random.Range(0, cards.Count)], new Vector3(0,0,0), Quaternion.identity);
playerCard.transform.SetParent(MyArea.transform, false);
GameObject leftCard = Instantiate(cards[Random.Range(0, cards.Count)], new Vector3(0,0,0), Quaternion.identity);
leftCard.transform.SetParent(LeftArea.transform, false);
GameObject rightCard = Instantiate(cards[Random.Range(0, cards.Count)], new Vector3(0,0,0), Quaternion.identity);
rightCard.transform.SetParent(RightArea.transform, false);
GameObject bottomCard = Instantiate(cards[Random.Range(0, cards.Count)], new Vector3(0,0,0), Quaternion.identity);
bottomCard.transform.SetParent(BottomArea.transform, false);
}
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类提款卡:单行为
{
公共游戏对象卡1;
公共游戏对象卡2;
公共游戏对象卡3;
公共游戏对象卡4;
公共游戏区;
公共游戏区;
公共游戏对象区域;
公共游戏区;
列表卡=新列表();
void Start()
{
卡片。添加(卡片1);
卡片。添加(卡片2);
卡片。添加(卡片3);
卡片。添加(卡片4);
}
int count1=0;
公共void OnClick()
{
count1++;
如果(count1==1)
{
对于(var i=0;i<p>),我会考虑将值存储到4个区域中,然后使用卡数据。我不完全知道一个区域是什么,或者如果一个区域可以有4张以上的卡,这意味着什么。
这里是一个拥有区域、卡片和区域管理器的非常通用的方法。我可以帮助澄清关于代码作用的任何细节,但我对其进行了广泛的评论。我没有将其集成到您的代码中,而是应该将其用作模板。我提供的代码未经测试,不应该只是作为您的代码进行复制粘贴这个问题仍然很笼统。若你们需要的话,我可以进一步解释
每个可移动卡上的卡类
// this class is on each of your cards
public class Card : MonoBehaviour,
{
// current index of this card
private int cardIndex = -1;
// stores the current zone this card is in
private Zone currentZone = null;
// init this object with the zone it is in and the card index it has
public void InitCard(Zone zone, int idx)
{
cardIndex = idx;
currentZone = zone;
}
// I am using the same EndDrag - I am assuming you are using an editor component of EventTrigger with an
// OnBeginDrag and OnEndDrag instead of implementing the IHandlers in code
public void EndDrag()
{
// determine if we are over a zone - I am going to use the MouseInput as with a drag,
// you would be dragging the object using the cursor or a finger
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// only look for the layer of the Zone objects
// make sure to add a layer to just the zones and make sure
// it is spelled / case-sensative so exactly the same
int layer_mask = LayerMask.GetMask("ZoneLayer");
// if we hit a zone, determine if it is a new zone and to move the card
if(Physics.Raycast (ray, out hit, Mathf.Infinity, layer_mask))
{
Zone newZone = hit.gameObject.GetComponent<Zone>();
// we hit a zone, is it the same zone?
if(currentZone == newZone)
{
// handle however you want to do a case where it is the same zone
// reset the card position possibly
return;
}
// we are in a new zone, so need to remove this card from the old zone and add it to the new one
// I am not sure how you are handling max card counts in each zone, etc. so this is very basic implementation
currentZone.RemoveCard(this);
newZone.AddCard(this);
// update our local zone reference
currentZone = newZone;
}
}
// setters / getters
public int CardIndex
{
set{cardIndex = value;}
get{return cardIndex;}
}
public Zone CurrentZone
{
set{currentZone = value;}
get{return currentZone;}
}
}
//该类在您的每张卡上
公共类卡:MonoBehavior,
{
//此卡的当前索引
私有int cardIndex=-1;
//存储此卡所在的当前区域
专用区域currentZone=null;
//使用该对象所在的区域和它拥有的卡索引初始化该对象
公共无效初始化卡(区域,int idx)
{
cardIndex=idx;
currentZone=区域;
}
//我使用的是相同的EndDrag-我假设您使用的是带有
//OnBeginDrag和OnEndDrag,而不是在代码中实现iHandler
公共void EndDrag()
{
//确定我们是否在一个区域上-我将使用鼠标输入作为拖动,
//您可以使用光标或手指拖动对象
雷卡斯特击中;
Ray-Ray=Camera.main.screenpointoray(输入.鼠标位置);
//仅查找分区对象的图层
//确保仅在分区中添加图层,并确保
//它的拼写和大小写完全相同
int layer_mask=LayerMask.GetMask(“ZoneLayer”);
//如果我们击中一个区域,确定它是否是一个新区域并移动卡
if(Physics.Raycast(光线、外击、数学无限、图层遮罩))
{
Zone newZone=hit.gameObject.GetComponent();
//我们进入了一个区域,是同一个区域吗?
if(currentZone==newZone)
{
//在同一区域内处理您想处理的情况
//尽可能重置卡位置
返回;
}
//我们在一个新区域,所以需要从旧区域移除此卡并将其添加到新区域
//我不知道你是如何处理每个区域的最大卡数等,所以这是非常基本的实现
currentZone.RemoveCard(此);
newZone.AddCard(这个);
//更新本地区域参考
currentZone=newZone;
}
}
//二传手
卡迪克斯公共酒店
{
设置{cardIndex=value;}
获取{return cardIndex;}
}
公共区域当前区域
{
设置{currentZone=value;}
获取{return currentZone;}
}
}
ZoneManager应该位于管理各个区域的对象上,因此所有这些数据可能位于画布对象上
// this class will manage each one of your zones
public class ZoneManager : MonoBehaviour
{
// list of our prefabs
[SerializeField] private List<GameObjects> allCardsPrefabList = new List<GameObjects>();
// create a list of zone data of size 4
[SerializeField] private Zone[] myZoneData = new Zone[MAX_ZONE_COUNT];
// max number of zones we can have
private const int MAX_ZONE_COUNT = 4;
// max number of cards we can have on Init
private const int MAX_INIT_CARD_COUNT = 4;
// init our zones in Start() - as I do not know if you are working with UI
// and UI components are only set before Start but not Awake
private void Start()
{
// iterate over each zone
for(int x = 0; x < MAX_ZONE_COUNT; ++x)
{
// iterate over 4 new cards
for(int y = 0; y < MAX_INIT_CARD_COUNT; ++y)
{
// I am using this overload of the Instantiate
// public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
// grab our new idx
int newCardIdx = Random.Range(0, cards.Count);
// spawn a new card childed to our zone at the new card idx
Card newCard = Instantiate(allCardsPrefabList[newCardIdx], myZoneData[x].transform, false).GetComponent<Card>();
// init our card zone / idx
newCard.Init(myZoneData[x], newCardIdx);
// add this card to a zone
myZoneData[x].AddNewCard(newCard);
}
}
}
}
//此类将管理您的每个区域
公共类分区管理员:单一行为
{
//我们的预制件清单
[SerializeField]私有列表allCardsPrefabList=新列表();
//创建大小为4的分区数据列表
[SerializeField]专用区域[]myZoneData=新区域[MAX_Zone_COUNT];
//我们可以拥有的最大区域数
私有常量int MAX_ZONE_COUNT=4;
//Init上可以拥有的最大卡数
私有常量int MAX_INIT_CARD_COUNT=4;
//在Start()中初始化我们的区域-因为我不知道您是否正在使用UI
//和UI组件仅在启动前设置,而不是唤醒
私有void Start()
{
//迭代每个区域
对于(int x=0;x