[英]Unity2D: Inventory system - instantiated prefabs appearing in front of original object
我創建了一個可用的庫存系統,但是實例化的預制件遇到了麻煩。 基本上,用戶在開始時會得到兩個項目(item1和item2)。 如果他們願意,可以購買額外的物品(放下第2天的物品)放下,但是可以放下物品的區域有限制(4)。 如果該區域中的物品大於4,則除了實例化的預制件外,所有其他物品均被發送回它們的插槽,它們被發送到一個空的gameObject。
我的問題:
假設我將第2項中的兩個放置了下來(實例化的原始和預制件),然后將第1項中的三個放置了下來,所有的項都將被寄回,然后將第2項拖到該區域中后,當我繼續嘗試並將第1項(原始obj)拖到似乎要拖動實例化的預制件而不是原始obj的區域,從而使原始obj更改大小並更改圖層順序。
我的劇本:
private Vector3 screenPoint;
private Vector3 offset;
public Transform item1, item2;
public GameObject otherItems,otherItems2
void Awake() {
item1 = GameObject.FindWithTag ("item1").transform;
item2 = GameObject.FindWithTag ("item2").transform;
}
void OnMouseDrag()
{
//if (canDrag == true) {
Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
screenPoint.z = 10;
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint); // + offset
transform.position = curPosition;
//}
}
void Update()
{
//1
if (Input.GetMouseButtonUp (0)) {
if (!GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item1Placed) {
item1.localPosition = Vector3.zero;
rend1.sortingOrder = 1;
//otherItems.SetActive (false);
} else if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item1Placed) {
screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
rend1.sortingOrder = -1;
otherItems.SetActive (true);
//canDrag = false;
}
}
//2
if (Input.GetMouseButtonUp (0)) {
if (!GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item2Placed) {
item2.localPosition = Vector3.zero;
rend2.sortingOrder = 1;
//otherItems2.SetActive (false);
} else if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item2Placed) {
screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
rend2.sortingOrder = -1;
otherItems2.SetActive (true);
//slotHolder.SetActive (true);
//canDrag = false;
}
}
if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item1Placed) {
if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item2Placed) {
slotHolder.SetActive (true);
}
if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item2Placed) {
if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item1Placed) {
slotHolder.SetActive (true);
}
}
實例化預制件的腳本:
public void InstaniateItem() {
GameObject item = (GameObject)Instantiate (ItemPrefab);
item.transform.position = transform.position;
item.transform.parent = transform;
item.SetActive (true);
if (slot.childCount < 0) {
slotHolder.SetActive (false);
}
}
額外信息:
謝謝 :)
通常,請嘗試緩存GameObject.Find()
的結果,因為它會降低性能,並且在您的情況下,它會使代碼真正混亂。 請參閱文檔以獲取更多信息。
您的問題可能來自double if (Input.GetMouseButtonUp(0))
子句。 實際上,每次釋放單擊都會調用此名稱,因此每次單擊都會對這兩項進行檢查。 如果您已經有兩個項目的問題,那么當您擁有三個或四個項目時會發生什么? 也許您應該重新考慮您的方法。
以下是與您的代碼等效的代碼,希望它有助於您對正在發生的事情有所了解。
void Awake() {
item1 = GameObject.FindWithTag ("item1").transform;
item2 = GameObject.FindWithTag ("item2").transform;
SlotController slotCtrl = GameObject.Find ("Aera Floor").GetComponent<SlotController> ();
}
void Update()
{
if (Input.GetMouseButtonUp (0)) {
if (!slotCtrl.item1Placed) {
item1.localPosition = Vector3.zero;
rend1.sortingOrder = 1;
//otherItems.SetActive (false);
} else {
screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
rend1.sortingOrder = -1;
otherItems.SetActive (true);
//canDrag = false;
}
if (slotCtrl.item2Placed) {
item2.localPosition = Vector3.zero;
rend2.sortingOrder = 1;
//otherItems2.SetActive (false);
} else {
screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
rend2.sortingOrder = -1;
otherItems2.SetActive (true);
//slotHolder.SetActive (true);
//canDrag = false;
}
}
if (slotCtrl.item1Placed && slotCtrl.item2Placed) {
slotHolder.SetActive (true);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.