3D游戏编程与设计9——UI系统

本文介绍了一种自制的3D UI场景,其中包含小动物角色进行对话的有趣设计。通过使用Unity和C#,实现了对话框的动态显示与滚动,以及通过动画触发器控制对话框的大小变化。
自制有趣的 UI 场景
  • 例如:“几个小动物(3D)开会,语句从每个动物头上飘出,当达到一个大小,会出现清晰的文字!如果文字较多,会自动滚动”

对话框的弹出与消失用动画机实现:
在这里插入图片描述
其中,Bubble 为对话框变大的动画,big 为对话框保持大状态的动画,DeleteBubble 为对话框消失的动画。

“big”触发时,对话框变大,然后保持大的状态;“small”触发时,对话框消失,回到“New State”,等待下一次“big”的触发。

实现代码 SceneController.cs 如下:

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

public class SceneController : MonoBehaviour {

    enum Talking_person  {LEFT,RIGHT};

    private List<string> dialogs;
    public Button leftBtn;
    public Button rightBtn;

    public Text leftText;
    public Text rightText;

    private Talking_person tp;
    private int dia_count;
    private bool scroll;
    private List<string> dia = new List<string>();

	// Use this for initialization
	void Start () {
        dialogs = new List<string>();
        dialogs.Clear();
        dialogs.Add("既然我回来了,就把你们安排得明明白白的");
        dialogs.Add("你为什么不放大");
        dialogs.Add("下路一直叫我去我怎么去啊,对面酒桶一直进我野区");
        dialogs.Add("这个人思想出了问题");


        Animator Ani = leftBtn.GetComponent<Animator>();
        Ani.SetTrigger("big");
        
        leftBtn.gameObject.transform.localScale = new Vector3(0, 0,0);
        Debug.Log(dialogs[0]);
        dia_count = 0;
        if (dialogs[dia_count].Length > 12)
        {
            leftText.text = dialogs[dia_count].Substring(0, 12);
            for (int i = 0; i < dialogs[dia_count].Length - 12; i++)
            {
                /*scroll = false;
                
                leftText.text = dialogs[dia_count].Substring(i + 1, 12);*/
                dia.Add(dialogs[dia_count].Substring(i + 1, 12));
            }
            StartCoroutine(BubbleOut());
        }
        else
        {
            leftText.text = dialogs[dia_count];
        }
        
        tp = Talking_person.LEFT;
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    IEnumerator BubbleOut()
    {
        yield return new WaitForSeconds(1f);
        StartCoroutine(DialogScroll());
    }

    IEnumerator DialogScroll()
    {
        yield return new WaitForSeconds(1.0f);
        if(tp == Talking_person.LEFT)
            leftText.text = dia[0];
        else
            rightText.text = dia[0];
        dia.Remove(dia[0]);
        if(dia.Count>0)
        {
            StartCoroutine(DialogScroll());
        }
    }

    private void Next()
    {
        if(tp == Talking_person.LEFT)
        {
            Animator Ani = leftBtn.GetComponent<Animator>();
            Ani.SetTrigger("small");
            Animator Ani2 = rightBtn.GetComponent<Animator>();
            Ani2.SetTrigger("big");

            dia_count = (dia_count + 1) % dialogs.Count;
            tp = Talking_person.RIGHT;
            if (dialogs[dia_count].Length > 12)
            {
                rightText.text = dialogs[dia_count].Substring(0, 12);
                for (int i = 0; i < dialogs[dia_count].Length - 12; i++)
                {
                    dia.Add(dialogs[dia_count].Substring(i + 1, 12));
                }
                StartCoroutine(BubbleOut());
            }
            else
            {
                rightText.text = dialogs[dia_count];
            }



        }
        else
        {
            Animator Ani = rightBtn.GetComponent<Animator>();
            Ani.SetTrigger("small");
            Animator Ani2 = leftBtn.GetComponent<Animator>();
            Ani2.SetTrigger("big");

            dia_count = (dia_count + 1) % dialogs.Count;
            tp = Talking_person.LEFT;
            if (dialogs[dia_count].Length > 12)
            {
                leftText.text = dialogs[dia_count].Substring(0, 12);
                for (int i = 0; i < dialogs[dia_count].Length - 12; i++)
                {
                    dia.Add(dialogs[dia_count].Substring(i + 1, 12));
                }
                StartCoroutine(BubbleOut());
            }
            else
            {
                leftText.text = dialogs[dia_count];
            }
        }
    }

    private void OnGUI()
    {
        if (GUI.Button(new Rect(650, 350, 60, 40), ">"))
        {
            Next();
        }
        
    }
}
需求响应动态冰蓄冷系统需求响应策略的优化研究(Matlab代码实现)内容概要:本文围绕“需求响应动态冰蓄冷系统需求响应策略的优化研究”展开,基于Matlab代码实现,重点探讨了冰蓄冷系统在电力需求响应背景下的动态建模优化调度策略。研究结合实际电力负荷电价信号,构建系统能耗模型,利用优化算法对冰蓄冷系统的运行策略进行求解,旨在降低用电成本、平衡电网负荷,并提升能源利用效率。文中还提及该研究为博士论文复现,涉及系统建模、优化算法应用仿真验证等关键技术环节,配套提供了完整的Matlab代码资源。; 适合人群:具备一定电力系统、能源管理或优化算法基础,从事科研或工程应用的研究生、高校教师及企业研发人员,尤其适合开展需求响应、综合能源系统优化等相关课题研究的人员。; 使用场景及目标:①复现博士论文中的冰蓄冷系统需求响应优化模型;②学习Matlab在能源系统建模优化中的具体实现方法;③掌握需求响应策略的设计思路仿真验证流程,服务于科研项目、论文写作或实际工程方案设计。; 阅读建议:建议结合提供的Matlab代码逐模块分析,重点关注系统建模逻辑优化算法的实现细节,按文档目录顺序系统学习,并尝试调整参数进行仿真对比,以深入理解不同需求响应策略的效果差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值