读一读

开关选项,用来选择选项和开关触发

Interactable 是否可交互

Transition 过渡效果,看Button

Is On 是否被选择

Group 一个Toggle Group,在一个组里的Toggle只能选择一个


private Button ddButton;
this.ddButton = transform.Find("Button-dd").GetComponent<Button>();

public void OnDdPressEvent() {
        Debug.Log("hello button");
        this.ddButton.gameObject.transform.Find("Text").GetComponent<Text>().text = "我被修改饿了";
}
this.ddButton.onClick.AddListener(OnDdPressEvent);

通过脚本添加事件监听。


Button的作用当然就是用来被人按的啦

Interactable 是否可交互

Transition 按钮的状态过度:

  1. None 无效果

  2. Color Tint颜色过度

  3. Sprite Swap图片过度

  4. Animation动画过度


private RawImage ddRawImage;
this.ddRawImage = transform.Find("RawImage-dd").GetComponent<RawImage>();

IEnumerator getImage() {
        WWW www = new WWW("http://chicai.group/wp-content/uploads/2017/10/1-6.png");
        yield return www;
        Texture ss = www.texture;
        this.ddRawImage.texture = ss;
}

StartCoroutine(getImage());

通过WWW获取链接上的图片,改变RawImage显示的图片


Raw Image同样的也是用来显示图片的,但是不同于Image的是,它可以显示任何的图片

图片可以使www下载下来的,可以是Sprite精灵

Texture 图片资源

Color 颜色

UV Rect 图片偏移和缩放


private Image ddImage;
public Sprite a;
this.ddImage = transform.Find("Image-dd").GetComponent<Image>();
this.ddImage.sprite = this.a;

访问Image组件的sprite属性,修改使用的图片精灵


Image就是用来显示图片的Sprite类型

Source Image 图片文件,导入为Sprite类型的图片

Color 颜色

Image Type:Simple原样,Sliced切片,Tiled平铺,Filled填充模式


切片和平铺表示图片做九宫格处理后,切片会拉伸中间部分其余不变,平铺表示中间部分发生重复填充,四周不变


填充模式的

Fill Method 填充的类型

Fill Origin 填充起点

Fill Smount 填充的程度,1为填充完整


using UnityEngine.UI;
this.ddText = transform.Find("Text-dd").GetComponent<Text>();
this.ddText.text = "Hello world";

使用UI组件时,需引入 UnityEngine.UI.

通过名字查找到Text-UI获取组件Text

再访问它的属性,参考Property


文本UI,就是用来显示文本的

text 要用来显示的文本,主要属性

Font 字体

Font Style 设置粗体斜体等

Font Size 字体大小

Line Space 多行文本的行高

Rich Text 是否为富文本

Alignment 水平垂直的排列方式

Horizontal Overflow: wrap适应在多行 overflow可以超出边界

Vertical Overflow: overflow可以超出下边界 truncate剔除超出的部分

Best Fit 自适应

Color 文本的颜色


UGUI是NGUI同一个作者开发的,UGUI是内置的

创建UI物体时,会自动创建Canvas和EventSystem

UI物体需要放置在Canvas物体下,就如贴纸是要贴在画布上一般

EventSystem帮助UI物体传递事件给系统