1.环形进度条

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

2.图形匹配

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI; /// <summary>
/// 被拖拽的慷慨块
/// </summary>
public class DragBrick : MonoBehaviour { private List<Transform> childrenTra = new List<Transform>();//慷慨块下的小方块
private List<GameObject> targetGo = new List<GameObject>();//小方块匹配的空方块
private GameObject[] tempGo;
private Vector3 posOffset; void Start()
{
Transform[] tra = transform.GetComponentsInChildren<Transform>();
for (int i = 1; i < tra.Length; i++)//tra[0]是自身。故不算上
childrenTra.Add(tra[i]);
} public void DragStart()
{
posOffset = transform.position - Input.mousePosition;
transform.SetAsLastSibling();
} public void DragUpdate()
{
transform.position = Input.mousePosition + posOffset;
} /// <summary>
/// 注意点:
/// 1.被射线检測的物体要带有Collider
/// 2.被射线检測的物体的z轴 > 发出射线检測的物体的z轴
/// 3.当没有给Raycast函数传layerMask參数时,只只忽略使用IgnoreRaycast层的碰撞器。 /// </summary>
public void DragEnd()
{
tempGo = new GameObject[childrenTra.Count];
int suitableBrickAmount = 0;//能正确匹配的砖块数目 for (int i = 0; i < childrenTra.Count; i++)
{
RaycastHit hit;
if (Physics.Raycast(childrenTra[i].position, Vector3.forward, out hit))
{
tempGo[i] = hit.transform.gameObject;
suitableBrickAmount++;
}
else break;
} if (suitableBrickAmount == childrenTra.Count)//全然匹配
{
if (targetGo.Count == 0)//初次全然匹配
{
Match();
}
else//已经全然匹配,再次全然匹配
{
Clear();
Match();
}
}
else//不能全然匹配
{
Clear();
}
} void Match()
{
for (int i = 0; i < tempGo.Length; i++)
{
targetGo.Add(tempGo[i]);
targetGo[i].layer = 2;//忽略射线碰撞
Vector3 pos = targetGo[i].transform.position;
pos.z--;
childrenTra[i].position = pos;
childrenTra[i].GetComponent<Outline>().enabled = true;
}
} void Clear()
{
for (int i = 0; i < targetGo.Count; i++)
targetGo[i].layer = 0;
targetGo.Clear(); for(int i = 0;i < childrenTra.Count;i++)
childrenTra[i].GetComponent<Outline>().enabled = false;
}
}

3.多重血条

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

描写叙述:

1.当受到伤害较小时,出现“残血”效果

2.当受到伤害较大时,出现“流水”效果

3.多重血条主要由四种血条组成:

a.最底层血条

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

b.当前血条与下一血条,就像下图中的紫色和红色

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

c.过渡血条,一般为深红色

using UnityEngine;
using System.Collections;
using UnityEngine.UI; //假设仅仅有一条血。那么一条血就是全部的血量
//假设有多条血。那么一条血就设定为一个固定值
public class MultiplyBloodBar : MonoBehaviour { public Image nowBar;//当前血条
public Image middleBar;//过渡血条
public Image nextBar;//下一血条
public Text countText;//剩下的血条数text private int count;//剩下的血条数(不包含当前血量)
private float nowBlood;//在一条血中的当前血量。如:100/1000则为100
private float oneBarBlood = 10000f;//一条血的容量,如:100/1000则为1000 private int colorIndex = 0;
public Color[] colors;//血条的颜色,注意Alpha值。默觉得0 private float slowSpeed = 0.1f;//受到重伤时( >oneBarBlood)或者处于加血状态,当前血条的流动速度
private float quickSpeed = 1f;//受到轻伤时( <oneBarBlood),当前血条的流动速度
private float speed;//当前血条採用的速度
private float middleBarSpeed = 0.1f;//过渡血条的流动速度 private float nowTargetValue;//当前血条移动的目标点
private float middleTargetValue;//过渡血条移动的目标点
private bool isBloodMove = false;//控制血条的移动 void Update()
{
MoveNowBar();//当前血条的流动
MoveMiddleBar();//过渡血条的流动
} /// <summary>
/// 传入总血量。初始化血条
/// </summary>
/// <param name="number"></param>
public void InitBlood(float number)
{
count = (int)(number / oneBarBlood);
nowBlood = number % oneBarBlood;
if (nowBlood == 0)
{
nowBlood = oneBarBlood;
count--;
} colorIndex = count % colors.Length;
nowBar.color = colors[colorIndex];
nowBar.fillAmount = nowBlood / oneBarBlood; if (count != 0)
{
int nextColorIndex = (colorIndex - 1 + colors.Length) % colors.Length;
nextBar.color = colors[nextColorIndex];
nextBar.gameObject.SetActive(true);
}
else
{
nextBar.gameObject.SetActive(false);
} middleBar.gameObject.SetActive(false); countText.text = count + "";
} /// <summary>
/// 血量变化,并依据伤害推断是否使用过渡血条
/// </summary>
/// <param name="number"></param>
public void ChangeBlood(float number)
{
nowBlood += number;
nowTargetValue = nowBlood / oneBarBlood;
isBloodMove = true; if ((number < 0) && (Mathf.Abs(number) <= oneBarBlood))//处于受伤状态而且伤害量较低时
{
speed = quickSpeed;
middleBar.gameObject.SetActive(true);
middleBar.transform.SetSiblingIndex(nextBar.transform.GetSiblingIndex() + 1);
middleBar.fillAmount = nowBar.fillAmount;
middleTargetValue = nowTargetValue;
}
else//处于受伤状态而且伤害量较大时。或者处于加血状态
{
speed = slowSpeed;
middleBar.gameObject.SetActive(false);
}
} /// <summary>
/// 普通血条的流动
/// </summary>
void MoveNowBar()
{
if (!isBloodMove) return; nowBar.fillAmount = Mathf.Lerp(nowBar.fillAmount, nowTargetValue, speed); if (Mathf.Abs(nowBar.fillAmount - nowTargetValue) < 0.01f)//到达目标点
isBloodMove = false; if (count == 0)
nextBar.gameObject.SetActive(false);
else
nextBar.gameObject.SetActive(true); if (nowBar.fillAmount >= nowTargetValue)
SubBlood();
else
AddBlood();
} /// <summary>
/// 过渡血条的流动
/// </summary>
void MoveMiddleBar()
{
//受到轻伤时( <oneBarBlood)。才会出现过渡血条
if (speed == quickSpeed)
{
middleBar.fillAmount = Mathf.Lerp(middleBar.fillAmount, middleTargetValue, middleBarSpeed);
if (Mathf.Abs(middleBar.fillAmount - 0) < 0.01f)
{
middleBar.transform.SetSiblingIndex(nextBar.transform.GetSiblingIndex() + 1);
middleBar.fillAmount = 1;
middleTargetValue++;
}
}
} void AddBlood()
{
float subValue = Mathf.Abs(nowBar.fillAmount - 1);
if (subValue < 0.01f)//到达1
{
count++;
countText.text = count.ToString(); nowBar.fillAmount = 0;
nowTargetValue -= 1;
nowBlood -= oneBarBlood; nextBar.color = colors[colorIndex]; colorIndex++;
colorIndex %= colors.Length;
nowBar.color = colors[colorIndex];
}
} void SubBlood()
{
float subValue = Mathf.Abs(nowBar.fillAmount - 0);
if (subValue < 0.01f)//到达0
{
//当前血条已经流动完,将过渡血条放置最前
middleBar.transform.SetSiblingIndex(nextBar.transform.GetSiblingIndex() + 2); if (count <= 0)
{
middleBar.gameObject.SetActive(false);
Destroy(this);
return;
};
count--;
countText.text = count.ToString(); nowBar.fillAmount = 1;
nowTargetValue += 1;
nowBlood += oneBarBlood; colorIndex--;
colorIndex += colors.Length;
colorIndex %= colors.Length;
nowBar.color = colors[colorIndex]; int nextColorIndex = colorIndex - 1 + colors.Length;
nextColorIndex %= colors.Length;
nextBar.color = colors[nextColorIndex];
}
} }

[UnityUI]一些有趣的UI样例的更多相关文章

  1. bootstrap ui样例

    http://demo.codedefault.com/demo/ui/theadmin/samples/invoicer/settings.html

  2. JAVA简单Swing图形界面应用演示样例

    JAVA简单Swing图形界面应用演示样例 package org.rui.hello; import javax.swing.JFrame; /** * 简单的swing窗体 * @author l ...

  3. C++的性能C#的产能?! - .Net Native 系列《三》:.NET Native部署测试方案及样例

    之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...

  4. shell脚本实例-菜单样例

    1.9.1 实例需求 用户在进行Linux系统管理的过程中,经常需要用到查看进程的信息.用户的信息等常用的功能.本例针对这一需求,使用shell编程实现基本的系统管理 功能.通过本程序,可以按照要求实 ...

  5. Android平台调用Web Service:演示样例

    近期在学习Android,随着移动设备的流行,当软件走上商业化的道路,为了争夺市场,肯定须要支持Android的,所以開始接触了Android,只是仅仅了解皮毛就好,由于我们要做管理者嘛,懂点Andr ...

  6. java 状态模式 解说演示样例代码

    package org.rui.pattern; import junit.framework.*; /** * 为了使同一个方法调用能够产生不同的行为,State 模式在代理(surrogate)的 ...

  7. JavaFX 简单3D演示样例

    从Java8開始,在JavaFX中便添加了3D部分的内容,包含Camera,Material,Light,Shape3D等基础内容. 当然,JavaFX 3D应该是OpenJFX里眼下正在补充和完好的 ...

  8. [持续交付实践] pipeline使用:项目样例

    项目说明 本文将以一个微服务项目的具体pipeline样例进行脚本编写说明.一条完整的pipeline交付流水线通常会包括代码获取.单元测试.静态检查.打包部署.接口层测试.UI层测试.性能专项测试( ...

  9. Android中MVP模式与MVC模式比較(含演示样例)

    原文链接 http://sparkyuan.me/ 转载请注明出处 MVP 介绍 MVP模式(Model-View-Presenter)是MVC模式的一个衍生. 主要目的是为了解耦,使项目易于维护. ...

随机推荐

  1. Verilog学习笔记基本语法篇(八)········ 结构说明语句

    Verilog中的任何过程都可以属于以下四种结构的说明语句; 1) initial;  2) always;  3) task;   4) function; 1) initial说明语句: 一个程序 ...

  2. HDU 2829 斜率优化DP Lawrence

    题意:n个数之间放m个障碍,分隔成m+1段.对于每段两两数相乘再求和,然后把这m+1个值加起来,让这个值最小. 设: d(i, j)表示前i个数之间放j个炸弹能得到的最小值 sum(i)为前缀和,co ...

  3. python 跨域

    CORS跨域请求 CORS即Cross Origin Resource Sharing 跨域资源共享, 那么跨域请求还分为两种,一种叫简单请求,一种是复杂请求~~ 简单请求 HTTP方法是下列方法之一 ...

  4. 【02】xmind如何修改默认线条设置

    [02]xmind如何修改不同主题的默认线条设置 魔芋:每次都是曲线.更喜欢为直线.因为曲线的路线是不确定的,看起来就显示很凌乱. 用everything搜索defaultStyles.xml     ...

  5. C++类设计1(Class without pointer members)

    class complex{ public: complex (double r = 0, double i = 0):re(r), im(i){} //inline complex& ope ...

  6. [转]pickle python数据存储

    python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...

  7. selenium之定位以及切换frame

    总有人看不明白,以防万一,先在开头大写加粗说明一下: frameset不用切,frame需层层切! 很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明明元素就在那儿,用firebug ...

  8. Leetcode 396.旋转函数

    旋转函数 给定一个长度为 n 的整数数组 A . 假设 Bk 是数组 A 顺时针旋转 k 个位置后的数组,我们定义 A 的"旋转函数" F 为: F(k) = 0 * Bk[0] ...

  9. “玲珑杯”ACM比赛 Round #18

    “玲珑杯”ACM比赛 Round #18 Start Time:2017-07-15 12:00:00 End Time:2017-07-15 15:46:00 A -- 计算几何你瞎暴力 Time ...

  10. BZOJ 3569 DZY Loves Chinese II ——线性基

    [题目分析] 腊鸡题目卡题面. 大概的意思就是给一张无向图,每次删掉其中一些边,问是否联通. 首先想到的是Bitset,可以做到n^2/64.显然过不了. 然而这是lyd在给我们讲线性基的时候的一道题 ...