在某一个父物体下根据配置文件创建Toggle,同时Toggle点击之后可以在指定的Image和Text上显示配置内容

一、需求分析

       根据配置文件创建Toggle  需要写一个读取配置文件的方法获取需要创建的Toggle的信息,同时使用一个数据结构将这些数据存储起来方便后续使用
       Toggle选中之后显示指定的内容 这里根据Toggle的选中状态显示指定的内容,所以需要给Toggle的父物体加上ToggleGroup组件防止,Toggle多选造成的错误,在显示图片的时候需要考虑图片的大小问题,所以在制作UI的时候Image的父物体也需要添加上合适的布局组件

二、实现过程

1、UI搭建

2、逻辑具体实现

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class ChangeSpriteByToggleIsOn : MonoBehaviour
{
    private string configTextContent = "ha,haha,hahaha";
    private string configImageContent = "ha,haha,hahaha";
    
    public Transform objParent;
    public Image showImage;
    public Text showText;

    void Start()
    {
        GetArrayByConfigContent(configTextContent, configImageContent);//这里可以直接使用一个List<List<string>>的集合来存储
        CreateObjByConfigFile(objParent, "Toggle", GetArrayByConfigContent(configTextContent, configImageContent)[0],
            "ChangeSpriteByToggleIsOn/");
        ToggleIsOnEvent(objParent, showImage,
            showText, GetArrayByConfigContent(configTextContent, configImageContent)[1], "ChangeSpriteByToggleIsOn/");
    }

    void Update()
    {
    }


    /// <summary>
    /// 读取配置文件数据相关(这里也可以使用字典来实现)
    /// </summary>
    /// <param name="configTextContent">需要显示的所有的Text的字符串</param>
    /// <param name="configImageContent">需要显示的所有的image的名字的字符串</param>
    /// <returns></returns>
    static List<List<string>> GetArrayByConfigContent(string configTextContent, string configImageContent)
    {
        string[] textArray = configTextContent.Split(',');
        string[] imageArray = configImageContent.Split(',');
        List<List<string>> textAndImageList = new List<List<string>>() { textArray.ToList(), imageArray.ToList() };
        return textAndImageList;
    }

    /// <summary>
    /// 通过配置文件创建物体
    /// </summary>
    /// <param name="objParent">需要创建物体的父物体</param>
    /// <param name="templateObjName">创建物体的标准模板名字</param>
    /// <param name="objNameList">需要创建物体的名字列表</param>
    /// <param name="templateFilePath">创建物体的标准模板所属路径</param>
    static void CreateObjByConfigFile(Transform objParent, string templateObjName, List<string> objNameList,
        string templateFilePath)
    {
        ToggleGroup toggleGroup = objParent.GetComponent<ToggleGroup>();
        string temp = templateFilePath + templateObjName;
        if (objNameList.Count > 0)
        {
            for (int i = 0; i < objNameList.Count; i++)
            {
                GameObject obj = GameObject.Instantiate(Resources.Load<GameObject>(temp), objParent);
                obj.GetComponentInChildren<Text>().text = objNameList[i];
                obj.GetComponent<Toggle>().group = toggleGroup;
            }
        }
        else
        {
            Debug.LogError("配置文件出错!!!!");
        }
    }

    /// <summary>
    /// Toggle点击事件
    /// </summary>
    /// <param name="togParent">Toggle的父物体</param>
    /// <param name="showImage">需要显示图片的UI</param>
    /// <param name="showText">需要显示内容的UI</param>
    /// <param name="imageNameList">图片名字的集合</param>
    /// <param name="path">图片所属文件夹</param>
    static void ToggleIsOnEvent(Transform togParent, Image showImage, Text showText,
        List<string> imageNameList,
        string path)
    {
        foreach (var toggle in togParent.GetComponentsInChildren<Toggle>())
        {
            toggle.onValueChanged.AddListener((isOn) =>
            {
                if (isOn)
                {
                    int index = toggle.transform.GetSiblingIndex();
                    string tempPath = path + imageNameList[index];
                    showImage.sprite = Resources.Load<Sprite>(tempPath);
                    showImage.SetNativeSize();
                    if (showText != null)
                        showText.text = toggle.GetComponentInChildren<Text>().text;
                }
            });
        }
    }
}

三、效果演示

演示视频

转发标明出处!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值