前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity-UGUI

Unity-UGUI

作者头像
祝你万事顺利
发布2019-05-29 15:59:19
7400
发布2019-05-29 15:59:19
举报
文章被收录于专栏:Unity游戏开发Unity游戏开发

UGUI与NGUI区别

differentBetNGUIAndUGUI.PNG

画布 Canvas 画布是摆放容纳所有的UI元素的区域,所有的UI元素需要在Canvas上组装。 Canvas 组件

Canvas.PNG

相机 正交相机:

透视相机:

CanvasScaler UIScaleMode:缩放模式 ScaleWithScreensize 根据屏幕大小进行调整

Graphic RayCaster组件 用于解决UI和3D场景穿透的问题

Image 改变一个图片

代码语言:javascript
复制
if (GUILayout.Button("ChangeImg",gUIStyle))
        {
            GetComponent<Image>().sprite = Resources.Load<Sprite>("ChooseBtn01");
        }

RawImage组件 是用来交互的组件 , 但是有些时候我们希望某张图片仅仅是用来显示 , 不想它跟任何 物体交互 , 有这样的组件吗 ?这就是RawImage. RawImage是用来显示非交互的图像控件 , 一般用于装饰或者图标 , RawImage 支持任何类型的 纹理 (Image 控件只支持 Sprite 类型的纹理.) 通过RawImage播放一个videoClip

代码语言:javascript
复制
void PlayAMovie()
    {
        MovieTexture movieTexture = GetComponent<RawImage>().mainTexture as MovieTexture;
        movieTexture.Play();
    }

Text Line Spacing:行间距 Rich Text:富文本

<b>text</b> --粗体 <i>text</i> --斜体 <size=10>text</size> --自定义字号 <color=red>text</color> --自定义颜色 <color=#a4ffff>text</color> --自定义颜色(16进制)

eg:

11<color=red>11111111</color>11111

可变参数富文本赋值

代码语言:javascript
复制
    public string NewTextRich(string input,params string[] colors)
    {
        string output = string.Format(input, colors);
        return output;
    }

Button 几种给Button挂Onclick的方法

代码语言:javascript
复制
private void ButtonClickEvent1()
    {
        Button.ButtonClickedEvent buttonClickedEvent = new Button.ButtonClickedEvent();
        buttonClickedEvent.AddListener(() => { print("11"); });
        button.onClick = buttonClickedEvent;
    }
代码语言:javascript
复制
private void ButtonClickEvent2()
    {
        button.onClick.AddListener(()=> { print("2!!!!!!!!"); });
    }

任何UI物体添加点击事件 第一种方法,指定具体的事件

specify individual delegates

代码语言:javascript
复制
private EventTrigger eventTrigger;
void Awake () {
        //button = GetComponent<Button>();
        eventTrigger = gameObject.AddComponent<EventTrigger>();
    }
private void Start()
    {
        RegisterEvent2();
    }
private void RegisterEvent()
    {
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerDown;
        entry.callback.AddListener((data) => { OnPointerDown((PointerEventData)data); });
        eventTrigger.triggers.Add(entry);
    }

第二种方法重写EventTrigger

You could extend EventTrigger, and override the functions for the events you are interested in intercepting; as shown in this example:

代码语言:javascript
复制
using UnityEngine;
using UnityEngine.EventSystems;

public class EventTriggerExample : [EventTrigger](EventSystems.EventTrigger.html)
{
    public override void OnBeginDrag([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnBeginDrag called.");
    }

    public override void OnCancel([BaseEventData](EventSystems.BaseEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnCancel called.");
    }

    public override void OnDeselect([BaseEventData](EventSystems.BaseEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnDeselect called.");
    }

    public override void OnDrag([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnDrag called.");
    }

    public override void OnDrop([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnDrop called.");
    }

    public override void OnEndDrag([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnEndDrag called.");
    }

    public override void OnInitializePotentialDrag([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnInitializePotentialDrag called.");
    }

    public override void OnMove([AxisEventData](EventSystems.AxisEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnMove called.");
    }

    public override void OnPointerClick([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnPointerClick called.");
    }

    public override void OnPointerDown([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnPointerDown called.");
    }

    public override void OnPointerEnter([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnPointerEnter called.");
    }

    public override void OnPointerExit([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnPointerExit called.");
    }

    public override void OnPointerUp([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnPointerUp called.");
    }

    public override void OnScroll([PointerEventData](EventSystems.PointerEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnScroll called.");
    }

    public override void OnSelect([BaseEventData](EventSystems.BaseEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnSelect called.");
    }

    public override void OnSubmit([BaseEventData](EventSystems.BaseEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnSubmit called.");
    }

    public override void OnUpdateSelected([BaseEventData](EventSystems.BaseEventData.html) data)
    {
        [Debug.Log](Debug.Log.html)("OnUpdateSelected called.");
    }
}

Toggle 添加Event事件

代码语言:javascript
复制
        Toggle toggle = GetComponent<Toggle>();
        Toggle.ToggleEvent toggleEvent = new Toggle.ToggleEvent();
        toggleEvent.AddListener((a) => { print("when" + a); });
        toggle.onValueChanged = toggleEvent;

DropDown

代码语言:javascript
复制
添加Options与添加OnValueChange的事件
void Start () {
        Dropdown dropdown = GetComponent<Dropdown>();
        List<Dropdown.OptionData> optionDatas = new List<Dropdown.OptionData>();
        for (int i = 0; i < 3; i++)
        {
            Dropdown.OptionData item = new Dropdown.OptionData("张三李四王五" + i);
            optionDatas.Add(item);
        }
        dropdown.AddOptions(optionDatas);

        Dropdown.DropdownEvent dropdownEvent = new Dropdown.DropdownEvent();
        dropdownEvent.AddListener((number) => { print("now : " + number); });
        dropdown.onValueChanged = dropdownEvent;
    }

Unity circleImage 对图片进行裁剪

InputField Events

Property:

Function:

On Value Change

A UnityEvent that is invoked when the text content of the Input Field changes. The event can send the current text content as a string type dynamic argument.

End Edit

A UnityEvent that is invoked when the user finishes editing the text content either by submitting or by clicking somewhere that removes the focus from the Input Field. The event can send the current text content as a string type dynamic argument.

代码语言:javascript
复制
        InputField.OnChangeEvent onChangeEvent = new InputField.OnChangeEvent();
        onChangeEvent.AddListener((str) => { print("当前字符串长度" + str.Length); });
        input.onValueChanged = onChangeEvent;


        InputField.SubmitEvent submitEvent = new InputField.SubmitEvent();
        submitEvent.AddListener((str) => { print("丢失焦点 获得字符串" + str); });
        input.onEndEdit = submitEvent;
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.05.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档