CD的实现:
两张精灵图片的配合形成动态的button,Mask可以形成圆形图标,
然后将脚本挂载到button上进行一个事件联系就可以形成技能的循环使用。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CD : MonoBehaviour {
public float leftTime;
public float totalTime;
private Image effectImage;
private Button cd_Button;
// Use this for initialization
void Start () {
effectImage = transform.FindChild("Image").GetComponent<Image>();
leftTime = totalTime;
cd_Button= transform.GetComponent<Button>();
cd_Button.interactable = false;
}
// Update is called once per frame
void Update () {
leftTime -= Time.deltaTime;
if (effectImage.fillAmount>0)
{
effectImage.fillAmount = leftTime / totalTime;
}
else
{
effectImage.fillAmount = 0;
cd_Button.interactable = true;
}
}
public void FireSkill()
{
Debug.Log("技能释放!");
cd_Button.interactable = false;
leftTime = totalTime;
effectImage.fillAmount = 1;
}
}
流光:
将流光图片精灵化,将脚本挂载到游戏对象上,然后将流光拖入到
里,然后将button拖入到最后的liu里。(这里需要注意,排序是系统对数字的识别有点奇怪,例如1到11的排列顺序是1,10,11,2,3,4,5,6,7,8,9)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class LiuGuang : MonoBehaviour {
public Sprite[] sprites;
public Image liu;
// Use this for initialization
void Start () {
Array.Sort(sprites, (x, y) => { return x.name.CompareTo(y.name); });
liu = GetComponent<Image>();
}
float count;
int num;
// Update is called once per frame
void Update () {
count++;
if (count>6)
{
if (num==sprites.Length-1)
{
num = 0;
}
else
{
num++;
}
liu.sprite = sprites[num];
count -= 6;
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;
public class Test30 : MonoBehaviour,IDragHandler,IEndDragHandler {
private Vector3 direction;
public float maxDistance;
public GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
player.transform.forward = new Vector3(direction.x, 0, direction.y);
int flag = Vector3.Distance(Vector3.zero, this.transform.localPosition) < 1f ? 0 : 1;
player.transform.Translate(Vector3.forward * flag * Time.deltaTime, Space.Self);
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
if (Vector3.Distance(Vector3.zero,transform.localPosition) >maxDistance)
{
direction = transform.localPosition - Vector3.zero;
transform.localPosition = direction.normalized * maxDistance;
}
}
public void OnEndDrag(PointerEventData eventData)
{
transform.localPosition = Vector3.zero;
}
}