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