首页 > 解决方案 > 在 Unity 中堆叠图像

问题描述

我有 4 个按钮,每个按钮都会激活一个图像(请参阅链接 - 堆叠)。一个脚本附加到所有 4 个按钮。有 4 个占位符。我正在使用一种堆栈的方法来一个接一个地堆叠图像。也就是说,如果第一个插槽是空的,则图像会放在那里。该按钮有两个功能,添加图像和删除图像。如果删除了第一个插槽的图像,则将第二个插槽的图像放置在第一个插槽上,并将第三个插槽的图像放置在第二个插槽上。如何在这种情况下添加推送和弹出?

编辑:我尝试使用 List 但我迷路了。有没有更简单的方法?

public class ActivateStackImages : MonoBehaviour {

public GameObject ImageGameObject_To_Add; //Each instance of this script has unique image
public Vector3[] PositionOfImages; //4 positions
private ActivateStackImages [] activateImages; //Script added to 4 buttons

public bool Slot_1_Filled = false;
public bool Slot_2_Filled = false;
public bool Slot_3_Filled = false;
public bool Slot_4_Filled = false;

public bool isImageAdded = false;

    private void Awake () {
        activateImages= FindObjectsOfType (typeof (ActivateStackImages )) as ActivateStackImages [];

    }


    public void ActivateGraph () {

        if (this.isImageAdded == false) {
            for (int i = 0; i < activateImages.Length; i++) {

                if (activateImages[i].Slot_1_Filled == false) {
                    ImageGameObject_To_Add.SetActive (true);
                    ImageGameObject_To_Add.GetComponent<RectTransform> ().anchoredPosition = PositionOfImages[0];

                    activateImages[i].Slot_1_Filled = true;


                } else if (activateImages[i].Slot_2_Filled == false) {
                    ImageGameObject_To_Add.SetActive (true);
                    ImageGameObject_To_Add.GetComponent<RectTransform> ().anchoredPosition = PositionOfImages[2];

                    activateImages[i].Slot_2_Filled = true;


                } else if (activateImages[i].Slot_3_Filled == false) {
                    ImageGameObject_To_Add.SetActive (true);
                    ImageGameObject_To_Add.GetComponent<RectTransform> ().anchoredPosition = PositionOfImages[2];

                    activateImages[i].Slot_3_Filled = true;


                } else if (activateImages[i].Slot_4_Filled == false) {
                    ImageGameObject_To_Add.SetActive (true);
                    ImageGameObject_To_Add.GetComponent<RectTransform> ().anchoredPosition = PositionOfImages[3];

                    activateImages[i].Slot_4_Filled = true;


                }
            }

            this.isImageAdded = true;

  } else if (this.isImageAdded == true) {
            ImageGameObject_To_Add.SetActive (false);

  }
 }
}
  


堆叠

标签: c#unity3d

解决方案


推荐阅读