Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

c# - Stacking images in Unity

I have 4 buttons and each of this button activates an image (Please see the link - Stacking). One script is attached to all the 4 buttons. There are 4 placeholders. I am using an approach of stack to stack images one after another. That is, if the first slot is empty, the image goes there. The button has two functions, to add image and to remove image. If the first slot's image is removed, the second slot's image is placed on 1st slot and third slot's image is placed on 2nd slot. How do I add push and pop to this scenario?

Edit: I tried with List but I am lost. Could there be a way simpler approach?

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);

  }
 }
}
  


Stacking

question from:https://stackoverflow.com/questions/65903458/stacking-images-in-unity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...