動的に作成したボタンをタッチした時、そのオブジェクトを削除するという機能を実装しようと考えています。
下記スクリプト
createPrefab(string strqrText)
では、UpDate()メソッドでカメラから取得した値を渡し、情報が入っていればContent内にボタンを追加するという動きをしています。
ここから、タッチしたボタンを削除したいのですが実装方法がわからず、質問致しました。
プログラムの組み方が良いのかどうかもわかりません。
上記2点についてご教示いただければ幸いです。
よろしくお願いいたします。
C#
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.UI; 6using ZXing; 7using ZXing.QrCode; 8 9public class QRReader : MonoBehaviour 10{ 11 12 public int height = 1920; 13 public int width = 1080; 14 public int fps = 60; 15 private string strqrText; 16 private WebCamTexture webCamTexture; 17 public PQRCodeManager qrManager; 18 public GameObject QRList; 19 RawImage rawImage; 20 [SerializeField] 21 GameObject goQRTextResult; 22 Transform QRContent; 23 private List<GameObject> cloneList = new List<GameObject>(); 24 25 // Use this for initialization 26 private IEnumerator Start() 27 { 28 QRList = Resources.Load("prefab/StepButton") as GameObject; 29 goQRTextResult = GameObject.FindGameObjectWithTag("parent"); 30 WebCamDevice[] devices = WebCamTexture.devices; 31 //webカメラの検索 32 for (int i = 0; i < devices.Length; i++) 33 { 34 Debug.Log(devices[i].name); 35 if (devices[i].name == null) 36 { 37 break; 38 } 39 } 40 41 //0番目に取得したwebカメラを取得 42 WebCamDevice userWebCamDevice = WebCamTexture.devices[0]; 43 rawImage = this.GetComponent<RawImage>(); 44 webCamTexture = new WebCamTexture(userWebCamDevice.name, height, width, fps); 45 46 // WebCamを許可するかユーザーに確認 47 yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); 48 if (Application.HasUserAuthorization(UserAuthorization.WebCam) == false) 49 // 許可しないので抜ける 50 yield break; 51 52 //カメラ画像を回転させる 53 // iPad実機 ※アウトカメラ 54 // 90度回転して表示されるので本来の角度に戻す 55 rawImage.rectTransform.Rotate(new Vector3(0, 0, 90)); 56 // X-Yサイズを入れ替え 57 rawImage.rectTransform.sizeDelta = new Vector2(rawImage.rectTransform.sizeDelta.y, rawImage.rectTransform.sizeDelta.x); 58 59 // 鏡像表示されるので反転して本来の表示に戻す 60 rawImage.rectTransform.localScale = new Vector3( 61 -rawImage.rectTransform.localScale.x, 62 rawImage.rectTransform.localScale.y, 63 rawImage.rectTransform.localScale.z); 64 65 rawImage.texture = webCamTexture; 66 webCamTexture.Play(); 67 68 this.qrManager = new PQRCodeManager(); 69 } 70 71 // Update is called once per frame 72 void Update() 73 { 74 try 75 { 76 strqrText = this.qrManager.read(webCamTexture); 77 } 78 catch (NullReferenceException ex) 79 { 80 Debug.Log("myLight was not set in the inspector"); 81 } 82 createPrefab(strqrText); 83 } 84 85 //子オブジェクトの作成 86 void createPrefab(string strqrText) 87 { 88 try 89 { 90 if (strqrText.Equals("-1")) 91 { 92 Debug.Log("The value is -1"); 93 } 94 else 95 { 96 //Scroll Viewに要素を追加する 97 GameObject clone = Instantiate(QRList) as GameObject; 98 cloneList.Add(clone); 99 clone.transform.Find("ProcessText").GetComponent<Text>().text = strqrText; 100 clone.transform.SetParent(goQRTextResult.transform, false); 101 clone.transform.name = strqrText; 102 clone.transform.GetComponent<Button>().onClick.AddListener(() => OnClick()); 103 } 104 } 105 catch (NullReferenceException Exception) 106 { 107 108 } 109 } 110}