1)重置uilabel的中心点(label中的文字左对齐):widget面板中修改pivot(overflow设置为resizefreely)

2)在固定区域内显示一段文字

3)AtlasMaker制作图集
在NGUI下open Atlas Maker 新建一个图集 选中texture中的图片进行导入操作,增 直接选中图片,删 点八叉 生成的图集放在新建的Atlasmaker文件夹中,包含有三个文件,mat,prefab和png


4)UISprite显示图片
创建UISprite组件,NGUI-Create-Sprite
选择图集

5)UISprite 面板属性
Sliced:九宫模式,适用于按钮背景图处理,不规则的背景图,先选择sliced的type ,再点击选中图片的edit进行切割


filled:进度模式 ,可展现读条动画

6)UILabel和UISprite制作
先新建NGUI的2D根节点,在UI Root下(紫色框内)右键添加背景Sprite,在背景Sprite上右键添加他的sprite和label child

7)使用代码动态创建UI Sprite UI物体
using UnityEngine;
using System.Collections;
public class CreateUISprite : MonoBehaviour {
//使用代码动态创建UI Sprite UI物体
private Transform m_Transform;
void Start () {
m_Transform = gameObject.GetComponent<Transform>();//查找进行赋值
//创建(实例化)一个GO对象
GameObject uiSprite = new GameObject("ElfAC");
//将uisprite设置为UI Root的子物体
uiSprite.GetComponent<Transform>().SetParent(m_Transform);
//重置scale
uiSprite.GetComponent<Transform>().localScale = Vector3.one;
//添加组件
UISprite sprite = uiSprite.AddComponent<UISprite>();
//读取图集 Resources.Load<T>("")该api需要 下面的名字对应的资源存放在Resources这个文件夹中
UIAtlas atlas = Resources.Load<UIAtlas>("GameAtlas") ;
//给组件指定图集
sprite.atlas = atlas;
//给组件指定图片
sprite.spriteName = "11000414";
}
void Update () {
}
}

8)UI Button 面板控制
1、按钮制作
先创建一个2D UI面板 添加UI Sprite 来展现一张图片或者一段文字 ;
给ui图片添加box collider组件,确定可以点击的区域(右键 attach找);
右键attach添加button script 完成


9)UIButton代码控制
1、按钮点击事件绑定
法一:面板属性栏绑定
①创建一个代码文件,定义一个公开的方法,挂载到一个游戏物体上。
②将该脚本拖拽到UIButton的OnClick事件上。

法二:代码绑定
①创建一个代码文件,挂载到按钮物体上(同法一)。
②定义一个叫做OnClick()的方法。
using UnityEngine;
using System.Collections;
public class MyButton : MonoBehaviour {
public void ButtonClick()
{
Debug.Log("我被点击了........");
}
private void OnClick()
{
Debug.Log("代码绑定的点击.......");
}
}
2、按钮交互声音
①Attach-->Play Sound Script添加组件
②Audio Clip指定一个声音文件
③指定Trigger出发该声音播放的时间,常用的是OnClick。

10)UI动态加载
1、将制作好的UI面板拖拽成为一个Prefab资源,放到Resources文件夹下;
2、使用Resources.Load()将该资源加载到内存中;
3、实例化该UI资源对象,放到UIRoot下完成显示;
相关API:NGUITools.AddChild(父对象,子对象);
NGUI提供的一个实例化物体,设置子物体的一个内置函数,操作UI尽量使用该NGUI封装函数。
using UnityEngine;
using System.Collections;
public class UIManager : MonoBehaviour {
private GameObject prefab_Info;
void Start () {
prefab_Info = Resources.Load<GameObject>("Info");
NGUITools.AddChild(gameObject, prefab_Info);
}
}

网友评论