unity之UI

Posted huang--wei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity之UI相关的知识,希望对你有一定的参考价值。

1.Vector3坐标

技术分享图片

2.地球,月球,太阳的旋转关系

技术分享图片

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

public class spere01 : MonoBehaviour {
    public GameObject moon;
    public GameObject sun;
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        // transform.Rotate(Vector3.up, 2, Space.World);//up表示围绕上面轴旋转
        moon.transform.Rotate(0, 1, 0);//月球自传
        transform.Rotate(0, 1, 0);//地球自转
        sun.transform.Rotate(Vector3.up);//太阳自转
        moon.transform.RotateAround(transform.position, Vector3.up, 1);//月亮围绕地球转
        transform.RotateAround(sun.transform.position, Vector3.up, 1);//地球围绕太阳转

    }
}

 

技术分享图片

 

3.UI之游戏界面

技术分享图片

 

 登录错误提示:

技术分享图片

 

 提示登录失败用:

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

public class test : MonoBehaviour {

    Text test01;
    float time = 0;
    public Image image;
    float sum = 100;
    bool b = false;
    // Use this for initialization
    void Start () {
        image = image.GetComponent<Image>();
        test01 = this.GetComponent<Text>();
        
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            image.fillAmount = 1;
            sum = 100;
            b = false;
        }
        time += Time.deltaTime;
        if (time >0.1f)
        {
            sum--;
          
            if (sum <0)
            {
                sum = 0;
                b= true;
            }
        }
        image.fillAmount = sum / 100;

        }
        
    }

 

进入游戏就有声音:
 slider.maxValue = 100;
        slider.minValue = 0;
        slider.value = 50;//赋初值
  if (toggle.isOn==true)
        {

            audio.mute = true;//静音
        }
        else
        {
            audio.mute = false;
        }
       
        audio.volume =(float) (slider.value/100);//把音量条的值赋给背景音乐

技术分享图片

 技术分享图片

 

//总代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class UIcontrol : MonoBehaviour {
    public InputField InputUserName;
    public InputField InputPassWord;
    public Button button;
    public GameObject failText;
    public Button closeSetting;
    public GameObject Panel;
    public Slider slider;
    public Toggle toggle;
    Audiosource audio;
     
    
    // Use this for initialization
    void Start () {
        InputUserName = InputUserName.GetComponent<InputField>();
        InputPassWord = InputPassWord.GetComponent<InputField>();
        button = button.GetComponent<Button>();
        closeSetting = closeSetting.GetComponent<Button>();
        slider = slider.GetComponent<Slider>();
        audio = GetComponent<AudioSource>();
        slider.maxValue = 100;
        slider.minValue = 0;
        slider.value = 50;

    }
    
    public void GetButton()
    {
        if (InputUserName.text == "huangwei" && InputPassWord.text == "123")
        {
            SceneManager.LoadScene(1);
        }
        else
        {
            failText.gameObject.SetActive(true);//登录失败出现
        }
    }
    public void OpenSetting()
    {
        Panel.gameObject.SetActive(true);//打开设置
    }
    public void CloseSetting()//关闭设置
    {
        Panel.gameObject.SetActive(false);
    }
    // Update is called once per frame
    float time;

    void Update()
    {
        if (this.gameObject.activeSelf)
        {
            time += Time.deltaTime;//计时器
            if (time > 3)
            {
                failText.gameObject.SetActive(false);//登录失败消失
                time = 0;
            }
        }

        if (toggle.isOn==true)
        {

            audio.mute = true;//静音
        }
        else
        {
            audio.mute = false;
        }
       
        audio.volume =(float) (slider.value/100);//把音量条的值赋给背景音乐
    }
}
4.进度条slider:

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

public class test : MonoBehaviour {

    Text test01;
    int num = 0;
    float time;

    public Slider slider;
    // Use this for initialization
    void Start () {
        test01 = this.GetComponent<Text>();
        slider = slider.GetComponent<Slider>();
        slider.maxValue = 100;
        slider.value = slider.minValue;
    }
    
    // Update is called once per frame
    void Update () {
        time += Time.deltaTime;
        //if (time >0.1f)
        //{
        //    num++;
        //    if (num >100)
        //    {
        //        num = 100;
        //        test01.text = num + "  %";
        //    }
        //    else
        //    {
        //        slider.value = num;
        //        time = 0;
        //    }


        //}
        //test01.text = num + "  %";
        //另一种方法

        if (time > 10)
        {
            time = 10;
        }
        slider.value = time * 10;
            test01.text = (int)(time*10)+ "  %";
    }
}
 

 

5.

技能冷却:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CoolSkill : MonoBehaviour
{
    public Image image;
    float time;
    float f;
    public Text text;
    bool b = false;
    bool bb = true;
    // Use this for initialization
    void Start()
    {
        image = image.GetComponent<Image>();
        text = text.GetComponent<Text>();
        image.fillAmount = 0;//默认可以发出技能
    }
    public void GetBool()
    {
        if (bb)//限制技能开启后才能使用
        {
            b = true;
            bb = false;
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (b)
        {
            time += Time.deltaTime;
            if (time <= 5)//技能控制在5秒冷却
            {
                f = (5 - time);//5秒倒计时
                image.fillAmount = (f) / 5;//image也在360度递减
                text.text = (f).ToString();//文本输出倒计时
                if (f < 0.1f && f >= 0)/控制在0.1秒以内结束时才可以重新开启技能
                {
                    bb = true;//重新开启技能Button可以点击了
                }
            }
            else
            {
                time = 0;//超过5秒后时间置零
                b = false;/tton点击后又可以计时了
            }


        }

    }
}

 

 

 

 

以上是关于unity之UI的主要内容,如果未能解决你的问题,请参考以下文章

Unity 基础 之 代码动态监听UI交互组件汇总

GJM :Unity UI 之 UGUI 开发

Unity5.3——UI之Canvas

unity之UI

通过 RayCast Unity 与 UI 交互

Unity 工具 之 SoftMask软遮罩 实现 UI 边缘渐变过渡的简单使用介绍