image.png
1、键盘输入选择条码 ,挂在每一个按钮上 index:0~35
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class jianpan : MonoBehaviour,IPointerClickHandler
{
public int index; //36个按钮,判断哪个按钮
public void OnPointerClick(PointerEventData eventData)
{
if (UI_TiaoMa.instance.Last_TiaoMa < 8)
{
//记录条码索引用于建立3D物体条码
UI_TiaoMa.instance.Index_TiaoMa.Add(index);
//显示的条码
GameObject temp = Instantiate(UI_TiaoMa.instance.UI_TiaoMa_Prefab.gameObject);
temp.GetComponentInChildren<Text>().text = transform.GetComponentInChildren<Text>().text;
temp.transform.SetParent(UI_TiaoMa.instance.BG_TiaoMa_S[UI_TiaoMa.instance.Last_TiaoMa].transform);
temp.transform.localPosition = Vector3.zero;
//8位条码限定
UI_TiaoMa.instance.Last_TiaoMa += 1;
}
}
}
2、制作条码脚本,配合jianpan脚本使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI_TiaoMa : MonoBehaviour
{
public static UI_TiaoMa instance;
public int Last_TiaoMa; //已显示的条码最后一位的索引
public Transform[] BG_TiaoMa_S = new Transform[8]; //上方显示的条码UI的8个父物体
public Transform UI_TiaoMa_Prefab; //上方显示的要复制的条码UI
public GameObject Obj_TiaoMa_Fu_Prefab; //3D物体条码父物体
public List<int> Index_TiaoMa; //条码数字索引
public GameObject[] Obj_TiaoMa = new GameObject[36]; //要复制的3D物体条码
public Button[] jianpan = new Button[36]; //键盘按钮
public Button Btn_queding; //确定按钮
public Button Btn_quxiao; //取消按钮
public Button Btn_qingchu; //清除按钮
void Awake()
{
instance = this;
}
private void OnEnable()
{
Btn_qingchu.onClick.AddListener(clickBtn_qingchu);
Btn_quxiao.onClick.AddListener(clickBtn_quxiao);
Btn_queding.onClick.AddListener(clickBtn_queding);
}
private void OnDisable()
{
Btn_qingchu.onClick.RemoveListener(clickBtn_qingchu);
Btn_quxiao.onClick.RemoveListener(clickBtn_quxiao);
Btn_queding.onClick.RemoveListener(clickBtn_queding);
}
void Update()
{
//当最后一位是8,即填满了上方的8位条码,才能点击确定生成3D物体
if (Last_TiaoMa.Equals(8))
{
Btn_queding.interactable = true;
}
else
{
Btn_queding.interactable = false;
}
}
/// <summary>
/// 确定按钮,生成3D条码
/// </summary>
void clickBtn_queding()
{
//生成3D条码物体
GameObject Temp_TiaoMaPar = Instantiate(Obj_TiaoMa_Fu_Prefab);
Btn_Date.instance.TiaoMa = Temp_TiaoMaPar;
for (int i = 0; i < 8; i++)
{
GameObject Temp_tiaoma = Instantiate(Obj_TiaoMa[Index_TiaoMa[i]]);
Temp_tiaoma.transform.eulerAngles = new Vector3(0,180,0);
Temp_tiaoma.transform.SetParent(Temp_TiaoMaPar.transform);
Temp_tiaoma.transform.localPosition = Vector3.zero + (new Vector3(-0.16f, 0, 0) * i);
}
//UI隐藏
Btn_Date.instance.UI_TiaoMa.gameObject.SetActive(false);
Btn_Date.instance.isShowUI_TiaoMa = false;
//打开拖动
Btn_Date.instance.BtnTiaoMa.GetComponent<BtnGongjuTest>().enabled = true;
//PaiZhao
Btn_Date.instance.BtnPaiZhao.interactable = true;
//赋值拖动条码按钮
Btn_Date.instance.BtnTiaoMa.GetComponent<BtnGongjuTest>().obj = Temp_TiaoMaPar;
clickBtn_qingchu();
}
/// <summary>
/// 取消按钮
/// </summary>
void clickBtn_quxiao()
{
Btn_Date.instance.UI_TiaoMa.gameObject.SetActive(false);
}
/// <summary>
/// 清除按钮,删除所有上方已显示的UI。重新选择
/// </summary>
void clickBtn_qingchu()
{
if (Last_TiaoMa > 0)
{
Last_TiaoMa = 0;
//删除上方显示的UI
for (int i = 0; i < 8; i++)
{
if (BG_TiaoMa_S[i].transform.childCount != 0)
{
Destroy(BG_TiaoMa_S[i].transform.GetChild(0).gameObject);
}
}
//清除保存的条码下标
Index_TiaoMa.Clear();
}
}
}
网友评论