NGUI系统教程 Siki 记录

Unity-NGUI 系统教程

Toolbar

可以拖上自己的预制体
有时拖不了,总之点击该节点、矩形工具、2/3D、展开Pannel或者Sprite

UIROOT0

紫色是显示的范围
展开Pannel,矩形视图,才有右键选项

公共的

widget

基准点
深度
高宽 原生
比例,自由,基准高,基准宽

Anhor

同一,基准IRoot
自己的左边基准 基准物体 的 哪个位置

中间,边边中中
顶点,边边边边

Label标签

找字体
C:\Windows\Fonts
在这里插入图片描述

字体适应
高宽自己适应
钳制
高宽系统适应
宽度自己适应,高度系统适应

动态字体

font maker
字体,动态字体,生成预制体
保存了样式,可以更换字体

TypeWriter

速度
框要够大,文字大小不变化

TextList文本滚动视图

TextList, 挂Label,ScrollBar,Label赋值,Text从上到下chat相反
ScrollBar
Label,

ZoomBoard缩放板

ZoomBoard
Drag 触发器, DragResize
Board 设锚点

Sprite

数学象限坐标系,原点在中心,单位像素

展开才有缩放框出现,才可以右键操作

Sliced九宫格

按钮九宫格
edit,打开预制体,border

Filed冷却

filled

Panel画板

分类UI组件,类似空节点

图片遮罩
裁剪
全显示

Button

节点
盒触发器
NGUI Button

选中节点右键Attach

函数挂节点,节点拖到Button

Label一般加载图片下

顺序
hover
click(一个地方点击释放)
press
release
out

play sound

Atlas图集

new
add/update
材质,预制体,.mat

13 Slider滑动条

  • List item

Bg Front Point都独立,比较方便
在这里插入图片描述

Slider color

颜色

ScrollBasr滚动条

Bg sprite、Button、Trigger、ScrollBar(size,TopToDown)
Front sprite(LRBT边距)、Button、Trigger、

PopList拉列表

UIPopularList有Fonts
Animated动画

keep value那里可以选择初始值

事件监听可能需要Trim掉current.value

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

public class Options : MonoBehaviour
{


    public enum Grade 
    {
        EASY,
        NORMAL,
        HARD
    }

    public enum InputType
    {
        MOUSE,
        KEYBOARD,
        TOUCH
    }

    public  Grade grade = Grade.NORMAL;
    public  InputType inputType = InputType.TOUCH;

    public  float volume = 1;
    public  bool isFullScreen = true;


    public void OnVolumeChange()
    {
        volume= UIProgressBar.current.value;
        print("volume");
    }
    public void OnGradeChange()
    {
        switch (UIPopupList.current.value)//Trim
        {
            case "入门":
                grade = Grade.EASY;
                break;
            case "普通":
                grade = Grade.NORMAL;
                break;
            case "困难":
                grade = Grade.HARD;
                break;
        }
            
           
    }
    public void OnInputTypeChange()
    {
        switch (UIPopupList.current.value)
        {
            case "触摸":
                inputType =InputType.TOUCH;
                break;
            case "键盘":
                inputType =  InputType.KEYBOARD; ;
                break;
            case "鼠标":
                inputType = InputType.MOUSE;
                break;
        }
    }
    public void OnIsFullScreenChange()
    {
        isFullScreen = UIToggle.current.value;
    }
}

CheckBox单选框

collider
toggle
拖一个打钩
开始状态
动画

Tween 动画

Tween Position

start A
end B
playforward,A->B
一开始不显示可以不激活,play时自动激活

    public TweenPosition startOptions;
    public TweenPosition completeOptions;

    public void startScene()
    {
        startOptions.PlayForward();
        completeOptions.PlayReverse();
    }
    public void optionsScene()
    {        
        startOptions.PlayReverse();
        completeOptions.PlayForward();                   
    }

InputField输入框

拖UIInput,触发器,Input拖Label

widget设置开始位置

类型,密码
校验类型,float string之类
限制n位数

submit,回车才更新(submi0t类型)
change,变化就更新

22 技能冷却

在这里插入图片描述

using UnityEngine;

public class Cd : MonoBehaviour
{
    [Tooltip("技能冷却图片,一般是有加了灰色")]public UISprite skillTop;
    [Tooltip("技能冷却的进度百分比的一个显示Label")]public UILabel processLabel;//从1冷却中到0完成
    [Tooltip("技能冷却的时间")]public float coolTime = 5f;
    [Tooltip("计时器")]public float timer = 0f;
    [Tooltip("默认是顺时针,冷却是顺时针还是逆时针")]public bool isClockWise = true;
    //
    [Tooltip("技能冷却图片的FillAmout开始是0")]private float from = 0f;//
    [Tooltip("技能冷却图片的FillAmout结束是1")]private float to = 1f;
    [Tooltip("技能已经冷却完成?")] private bool isCooled = true;
    // Start is called before the first frame update
    void Start()
    {
        skillTop.fillAmount =from;
        skillTop.invert = isClockWise;
    }

    // Update is called once per frame
    void Update()
    {
        
        CoolingSkill();
    }

    public void CoolingSkill()
    {
        if (!isCooled)//冷却
        {
            if (timer > coolTime)//冷却完成
            {
                isCooled = true;

            }
            timer += Time.deltaTime;
            skillTop.fillAmount = to - timer / coolTime;
        }
        else if (isCooled)//冷却完成
        {
            timer = 0f;
            skillTop.fillAmount = from;//重置
        }
        processLabel.text = (100 - skillTop.fillAmount * 100).ToString("0") + "%";//显示


    }
    public void ReleaseSkill()//按钮施放技能
    {
        isCooled = false;
        //TODO
    }
}

29 聊天室

if (uiInput.value == “”) return;
不是==null 也不是 = =“请输入聊天内容”
在这里插入图片描述

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

public class TextList : MonoBehaviour
{
    [Tooltip("公屏上的内容")]
    public UITextList textList;//
    [Tooltip("输入框里的内容")]
    public UIInput uiInput;
    [Tooltip("滚动条")]
    public UIScrollBar uiScrollBar;

    private List<string> nameList = new List<string> { "庞青云", "赵二虎", "姜午阳" };
    // Start is called before the first frame update
    void Start()
    {
        textList = GetComponentInChildren < UITextList>();
        textList.Add("安礼章第六怨在不舍小过,患在不豫定谋。福在积善,祸在积恶。饥在贱农,寒在惰织。安在得人,危在失士。富在迎来,贫在弃时。上无常操,下多疑心。轻上生罪,侮下无亲。近臣不重,远臣轻之。自疑不信人,自信不疑人。枉士无正友,曲上无直下。危国无贤人,乱政无善人。爱人深者求贤急,乐得贤者养人厚。国将霸者士皆归,邦将亡者贤先避。地薄者大物不产,水浅者大鱼不游,树秃者大禽不栖,林疏者大兽不居。山峭者崩,泽满者溢。弃玉取石者盲,羊质虎皮者柔。衣不举领者倒,走不视地者颠。柱弱者屋坏,辅弱者国倾。足寒伤心,民怨伤国。山将崩者下先隳,国将衰者民先毙。根枯枝朽,民困国残。与覆车同轨者倾,与亡国同事者灭。见已失者慎将失,恶其迹者须避之。畏危者安,畏亡者存。夫人之所行,有道则吉,无道则凶。吉者百福所归;凶者百祸所攻。非其神圣,自然所钟。务善策者无恶事,无远虑者有近忧。同志相得,同仁相忧,同恶相党,同爱相求,同美相妒,同智相谋。同贵相害,同利相忌,同声相应,同气相感。同类相依,同义相亲,同难相济,同道相成,同艺相规,同巧相胜。此乃数之所得,不可与理违。释己而教人者逆,正己而化人者顺。逆者难从,顺者易行,难从则乱,易行则理。如此理身、理家、理国,可也!");
    }

    public void OnInputSubmit()
    {
        if (uiInput.value == "") return;
        
            textList.Add(nameList[Random.Range(0, nameList.Count)] + ":" + uiInput.value);
            uiInput.value = null;
            uiScrollBar.value = 1;      
    }
}

33 背包

在这里插入图片描述

拖拽

DropAndDropItem
拖拽一次后,触发器就不激活了,加上base.OnDragDropRelease(surface);拖拽后触发器仍然激活

DragOnClone,拖拽即复制

knapsack背包

public class MyDragDrop : UIDragDropItem
{
    protected override void OnDragDropRelease(GameObject surface)
    {
        print(  surface );
    }
}

拖拽交换

一种是设置父节点后,然后localPositio=Vector3.zero

	transform.parent = surface.transform;
	transform.localPosition = Vector3.zero;

一直是直接记录开始的position(但我们需要设置父节点,更好管理)

	transform.position = surface.transform.position;

tag与childCount都可以判断下面有没有物品

Knapsack背包

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

public class MyDragDrop : UIDragDropItem
{
    [Tooltip("开始拖拽的物体的位置")]
    private Vector3 startPos=Vector3.zero;

    protected override void OnDragDropStart()
    {
        base.OnDragDropStart();

       startPos=  base.transform.position;//记录开始位置
    }
    protected override void OnDragDropRelease(GameObject surface)
    {

        base.OnDragDropRelease(surface);
        print(  surface.tag );

        if (surface.tag == "Grid")
        {
            transform.position = surface.transform.position;//物体放入格子里

            //transform.parent = surface.transform;
            //transform.localPosition = Vector3.zero;
        }
        if (surface.tag == "Good")
        {   
            //位置交换
            transform.position=surface.transform.position;
            surface.transform.position = startPos;

        }
    }
}

往背包加入物品

itemList,物体的预制体
gridList,格子的节点列表

从itemList中随机一个good物品
视频放置物品的格子位置也是初始化的,我是从头到尾
在这里插入图片描述

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

public class GoodList : MonoBehaviour
{
    [Tooltip("宏观上item的百科")]
    public List<GameObject> itemList = new List<GameObject>();
    [Tooltip("背包中格子的List")]
    public List<Transform> gridList = new List<Transform>();
    [Tooltip("背包中物品的List,有count")]
    public List<Transform> goodList = new List<Transform>();
    [Tooltip("初次生成的地方有不同的物品,开始从头索引")]
    public int indexInStartDifferent = 0;

    // Start is called before the first frame update
    void Start()
    {
        foreach (Transform t in transform.GetComponentInChildren<Transform>())
        {
            gridList.Add(t);
        }
    }

    // Update is called once per frame
    void Update()
    {
        CreateItem();
    }

    void CreateItem()//点击生成物品的测试函数
    {
        if (Input.GetMouseButtonDown(0))
        {
            //1 生成
            int itemIndex = Random.Range(0, itemList.Count);

            for (int i = 0; i < gridList.Count; i++)
            {
                string itemName= itemList[itemIndex].GetComponent<UISprite>().spriteName;
                string goodName= gridList[i].childCount ==0 ? "" : gridList[i].GetChild(0).GetComponent<UISprite>().spriteName ;
                print(goodName+","+itemName);//测试
                bool isFind = (itemName==goodName);//新增的sprite名字在格子的子节点中是否能找到相同的

                if (isFind)
                {
                    print("相同");
                    IncreaseGood(i);
                    break;
                }
                else if (!isFind)
                {
                    if (gridList[i].childCount > 0)
                    {
                        if (i >= gridList.Count) break;
                        print("不同");
                        continue;
                    }
                    else
                    {
                        print("没有");
                        CreateGood(i,itemIndex);
                        break;
                    }
                }
            }

        }
    }
  
    void CreateGood(int gridIndex,int itemIndex)
    {
        Transform good = Instantiate(itemList[itemIndex], gridList[gridIndex]).transform;//生成
        good.parent = gridList[gridIndex].transform;//父节点
        good.transform.localPosition = Vector3.zero;
        good.transform.localScale = new Vector3(1f, 1f, 1f);
        goodList.Add(good.transform);
    }
    void IncreaseGood(int gridIndex)
    {
        Transform t = gridList[gridIndex].transform;
        int count = int.Parse(t.GetChild(0).GetChild(0).GetComponent<UILabel>().text);
        t.GetChild(0).GetChild(0).GetComponent<UILabel>().text = (++count).ToString();
    }

   
}

37 扣血条

掉血的预制体

3个Tween,逐渐变高、变小、变透明
在这里插入图片描述

代码

在这里插入图片描述

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

public class Attack : MonoBehaviour
{

    [Tooltip("掉血的预制体")] public GameObject prefab;
    //
    [Tooltip("血条")] public Transform hpBar;
    [Tooltip("伤害值")] public float attack=5f;
    [Tooltip("最大血量")] public float maxHp=100f;
 
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Instantiate(prefab, transform.position, transform.rotation);
            hpBar.GetComponent<UISlider>().value -= attack / maxHp;
        }  
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值