Unity 左右布局排版,Grid布局的使用

1.新建一个空对象GameObject, width: 400, height:100,两个canvas(wordLeft(width:200,height:100),wordRight(width:200,height:100)),分割线Image(width:1,height:100)

wordLeft,wordRight 都挂载 Grid Layout Group

2.预制体word-input (包含图片,图片下有Text),图片挂载Vertical Layout  Group 自适应宽高布局,挂载脚本WordInputPrefab。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class WordInputPrefab : MonoBehaviour
{
    // Use this for initialization
    public Text word;


    private bool canClick  = false; //可以点击
    private bool isClick = false;  //是否1已点击
    void Start()
    {
        GetComponent<Button>().onClick.AddListener(onClickSelect);
    }

    // Update is called once per frame
    void Update()
    {

    }
    
    public void open() {

        canClick = true;

    }

   
    //点击选择变色
    private void onClickSelect() {
        if (canClick == false  || isClick == true) return;

        Text tx = GetComponentInChildren<Text>();
        
        if (tx != null) {
            GameObject g5 = GameObject.Find("Login5");
            Login5 login5 = g5.GetComponent<Login5>();
            if (login5.checkOrder(tx.text))
            {
                Color color;
                if (ColorUtility.TryParseHtmlString("#515151", out color))
                {
                    tx.color = color;
                    isClick = true;
                }
                
            }
        }
    }

}

3.GameObject 挂载脚本Login5

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
public class Login5 : MonoBehaviour
{
    // Use this for initialization
    public RectTransform content1;
    public RectTransform content2;
    public GameObject perfab;

        List<string> defaultList =new List<string> { "word1", "word2","word3", "word4", "word5", "word6" };


    void Start()
    {
    }
        

    // Update is called once per frame
    void Update()
    {
        
    }

    public void leftList() {

        //MnemonicWordsList mw = MnemonicWordsContent.GetComponent<MnemonicWordsList>();
        //print(mw.defaultList.Count);

        //默认数据
        List<string> defaultList =new List<string> { "word1", "word2","word3", "word4", "word5", "word6" };
        
        Color color;
        for (int i = 0; i < defaultList.Count; i++)
        {
           
            GameObject item = Instantiate(perfab, content1); //实例化对象

            WordInputPrefab wp = item.GetComponent<WordInputPrefab>();
            if (ColorUtility.TryParseHtmlString("#6C6C6C", out color)) {
                wp.word.color = color;   //默认颜色
            }

            VerticalLayoutGroup verticalLayout = item.GetComponent<VerticalLayoutGroup>();
            if (verticalLayout != null) {
                verticalLayout.padding = new RectOffset(5, 0, 0, 0);
            }
            wp.word.alignment = TextAnchor.MiddleLeft;  //靠左居中
            
            wp.word.text = (i+1).ToString()+".";

        }

        List<string> leftList = shuffleArray(defaultList);
        for (int i = 0; i < leftList.Count; i++)
        {
            GameObject item = Instantiate(perfab, content2);
            WordInputPrefab wp1 = item.GetComponent<WordInputPrefab>();
            wp1.open();
            wp1.word.text = leftList[i];
        }
    }
    //打乱
    private List<string> shuffleArray(List<string> array) {
        System.Random random = new System.Random();
        return array.OrderBy(x=>random.Next()).ToList();
    }

    private List<string> selectList = new List<string>();
//是否按照默认顺序选择
    public bool checkOrder(string text)
    {
        int l = selectList.Count;
        
        if (defaultList[l] == text)
        {

            selectList.Add(text);
            setSelectList(l,text);
            return true;
        }
        else {
            print(text);
            return false;
        }
    }

//选择的列表变色
    public void setSelectList(int index,string text) {

        Transform tf = content1.GetChild(index);
        Text tx = tf.GetComponentInChildren<Text>();
        tx.text = (index + 1).ToString()+". "+text;
        Color color;
        if (ColorUtility.TryParseHtmlString("#FFFFFF", out color)) {
            tx.color = color;
        }
    }

    public void clearData() {

        selectList.Clear();
        foreach(Transform child in content1.transform) {
            Destroy(child.gameObject);
        }
        foreach (Transform child2 in content2.transform) {
            Destroy(child2.gameObject);
        } 

    }



}

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值