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点击后又可以计时了
            }

        }

    }
}

原文地址:https://www.cnblogs.com/huang--wei/p/9534285.html

时间: 2024-10-19 07:02:09

unity之UI的相关文章

【TGUI】从零开始搭建一个基于Unity的UI库 01

作为一个初学Unity3D一个来月的小菜鸟,要想自己搭建一个UI库也是挺疯狂的,但是老大不给用NGUI,然后那时候Unity4.6正式版还没发布(或者说我还不知道Unity4.6有自带UI),然后就无所畏惧的写下来了,目前虽然功能很简陋,实现的控件也非常少,但是作为练手的目的已经达成了,所以放上来给大家分享. -------------------------------------------------- 分割线 ---------------------------------------

关于Unity中UI中的Button节点

Button是最常用的UI节点,包含的组件有 1.Image组件 显示Button的纹理,把Image贴图拖进Image组件中后,记得点击Set Native Size,显示贴图原始大小 2.Button组件 Interatable:打钩表示按钮可以被点击和交互,取消钩表示不能被点击和交互. Transition:过度效果,A.默认状态  B.鼠标划过效果Highlighted Sprite  C.按钮按下效果Pressed Sprite  D.按钮禁用状态Disabled Sprite 这里有

unity中UI的屏幕自适应代码

public void ScreenUISelfAdptation(Transform scaleUI) { float widthrate = UnityEngine.Screen.width / 1920.0f; float heightrate = UnityEngine.Screen.height / 1080.0f; float postion_x = scaleUI.GetComponent<RectTransform>().anchoredPosition.x * widthra

Unity中UI界面颤抖解决方法

将Render Mode中属性改为Screen Space - Camera 摄像机挂在Canvas属性下会出现UI界面颤抖的效果. UI界面颤抖解决方式:将Render Mode中属性改为Screen Space - Overlay,如下图所示:

UNITY的UI之Pivot与Anchor区别

Pivot Rotations, size, and scale modifications occur around the pivot so the position of the pivot affects the outcome of a rotation, resizing, or scaling. When the toolbar Pivot button is set to Pivot mode, the pivot of a Rect Transform can be moved

Unity UGUI——UI控件的鼠标快捷操作

调节UI控件布局的两种方式:使用Rect Tool工具,使用Rect Transfrom组件 Rect Tool个作用:允许开发者直接在Scene视图里,移动.缩放.旋转UI控件

Unity 新UI事件系统(EventSystem) Demo

新的UI系统消息传递被全新设计,该系统用MonoBehaviour 来实现自定义的接口,以便可以接受来自消息系统的回调.当一个对象被消息的执行指定,那么这个对象的所有实现了自定义接口的脚本将被通知,指定的方法将被执行. 1.定义一个接口ICustomMessageTarget继承IEventSystemHandler, 这样当发送此类型消息时,实现此接口的脚本所在的对象将会被通知. using UnityEngine; using UnityEngine.EventSystems; using

Unity UGUI——UI基础,Canvas

主题:画布--Canvas 内容:创建Canvas UI控件的绘制顺序

unity中UI界面显示FPS

直接上代码 using UnityEngine; using System.Collections; public class HUDFPS : MonoBehaviour { // Attach this to a GUIText to make a frames/second indicator. // // It calculates frames/second over each updateInterval, // so the display does not keep changi