Unity—UGUI

每日一句:读数、学习 去更远的地方,才能摆脱那些你不屑一顾的圈子

目录

InputFiled输入框

例:用户名和密码

Toggle组件

案例:冷却效果


InputFiled输入框

 

Text Component 输入文本组件

Text输入内容

Character Limit 输入字符限制,0不限制

Content Type输入文本内容类型

{Standard 允许所有输入

Autocorrected

Integer Number十进制

Alphanumeric字母A-Z,a-z,0-9

Name 强制每个单词首字母大写

Email Address

Password所有输入将其显示为星号

Pin允许整数将其显示为星号

}

Placeholder提示本文组件

Caret Blink Rate光标闪烁频率

Caret Width光标宽度

Custom Caret Color是否自定义光标颜色

Selection Color选中文本的颜色

例:用户名和密码

 

public class Loading : MonoBehaviour

{

    public InputField username;

    public InputField passward;

    public GameObject tip;

    public GameObject message;

    public void LoadNext()

    {

        string user = username.text;

        string pass = passward.text;

        if(user=="gloriously"&&pass=="20030126")

        {

            SceneManager.LoadScene(1);

            message.SetActive(true);

        }

        else

        {

            tip.SetActive(true);

            //若输入错误提示信息,两分钟后消失

            StartCoroutine(Hidetip());

        }

    }

    IEnumerator Hidetip()

    {

        yield return new WaitForSeconds(2);

        tip.SetActive(false);

    }

    

}

Toggle组件

切换按钮

把两个Toggle放在一个空物体里,在空物体上加Toggle Group组件

 

 

Slider——Image Type

{Simple 会等比例被拉伸

slider 边框不会被拉伸(先将图片变为九宫格切片,有边框才会受影响)

Tiled平铺

Filled填充[主要是形式]}

Outline组件 ——边框

Shadow组件——给字体添加阴影

案例:冷却效果

 

public class Skill : MonoBehaviour

{

    float coldTime = 2;//冷却时间

    private float timer = 0;

    private bool isCold = false;

    private Image coldMask;

    // Start is called before the first frame update

    //两张图片形成父子物体,子物体:攻击冷却图片,父物体挂载button

    void Start()

    {

        coldMask = transform.Find("ColdMask").GetComponent<Image>();

    }



    // Update is called once per frame

    void Update()

    {

        if(isCold)

        {

            timer += Time.deltaTime;

            coldMask.fillAmount = (coldTime - timer) / coldTime;

        }

        if(timer>coldTime)

        {

            isCold = false;

            coldMask.fillAmount = 0;

            timer = 0;

        }

    }

    public void OnSkillClick()

    {

        if(isCold==false)//点击按钮开始攻击,开始缓解冷却,

        {

            isCold = true;

            timer = 0;

            coldMask.fillAmount = 1;//完全冷却

        }

    }

}

Mask遮罩  需要框的Image有颜色,想要不显示背景,show Mask Graphic不勾

控制滑动列表到最近的页面,当拖拽结束那一刻,检测位置离那个页面近

 

private ScrollRect scroll;

    private float[] pagePosition = new float[4] { 0, 0.33f, 0.66f, 1 };

    void Start()

    {

        scroll = GetComponent<ScrollRect>();       

    }

    private float targetPosition = 0;

    private bool isMove = false;

    public Toggle[] toggleArray;

    public void EndDragDemo()

    {

        float currentPosition = scroll.horizontalNormalizedPosition;

        int index = 0;

        float offset = currentPosition - pagePosition[0];

        for (int i = 1; i < 4; i++)

        {

            if (Mathf.Abs(currentPosition - pagePosition[i]) < offset)

            {

                index = i;

                offset = Mathf.Abs(currentPosition - pagePosition[i]);

            }

        }

        Debug.Log("aaa");

        targetPosition = pagePosition[index];

        isMove = true;

        toggleArray[index].isOn = true;

        //scroll.horizontalNormalizedPosition = pagePosition[index];

    }

    void Update()

    {

        if (isMove)

        {

            scroll.horizontalNormalizedPosition = Mathf.Lerp(scroll.horizontalNormalizedPosition, targetPosition, Time.deltaTime * 6);

            if (Mathf.Abs(scroll.horizontalNormalizedPosition - targetPosition) < 0.001f)

            {

                isMove = false;

                scroll.horizontalNormalizedPosition = targetPosition;

            }

        }

    }

    public void MoveTopage1(bool isOn)

    {

        if (isOn)

        {

            isMove = true;

            targetPosition = pagePosition[0];

        }

    }

    public void MoveTopage2(bool isOn)

    {

        if (isOn)

        {

            isMove = true;

            targetPosition = pagePosition[1];

        }

    }

    public void MoveTopage3(bool isOn)

    {

        if (isOn)

        {

            isMove = true;

            targetPosition = pagePosition[2];

        }

    }

    public void MoveTopage4(bool isOn)

    {

        if (isOn)

        {

            isMove = true;

            targetPosition = pagePosition[3];

        }

}

通过页面按钮控制滚动列表跳转

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity UGUIUnity游戏开发中常用的用户界面实现方法。UGUIUnity自带的用户界面系统,可以通过创建UI元素来构建游戏的界面。在Unity中,UGUI可通过创建Button、Text、Image等UI组件来实现交互和展示。 在UGUI中,通常选择ISO模型进行2D平面开发。通过锁定界面可以防止镜头发生偏移,确保界面的稳定性。 添加Button点击事件的方法如下: 1. 在Hierarchy面板中创建一个空物体GameObject。 2. 在Project面板中创建一个新的C#脚本,命名为OnClickButton,并将该脚本添加到GameObject上。 3. 双击OnClickButton脚本,在打开的脚本编辑器中输入相应的代码。该代码的作用是在点击按钮后输出"You click the button."和若干次"Yes"。 4. 在Click方法中使用print函数输出相关信息,并通过循环打印"Yes"特定次数。 5. 完成脚本的编写后,将脚本保存并返回Unity编辑器界面。 以上是关于Unity UGUI的简要介绍和Button点击事件的添加方法。希望对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [超详细的Unity UGUI教学](https://blog.csdn.net/qq_37701948/article/details/106682377)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值