/** *Copyright(C) 2017 by HT307035570 *All rights reserved. *FileName: Btn.cs *Author: Joel *Version: 1.0 *UnityVersion:5.6.1f1 *Date: 2017-07-12 *Description: 触碰更新进度条 *History: */ using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Btn : MonoBehaviour { public static Btn instance; public bool flag = false;//定义flag开关初始为false public float FiA;//定义变量接受fill aount 值 void Awake() { if (instance != null) { Destroy(this.gameObject); } else { instance= this; } } void Start() { times = transform.GetComponent<Image>().fillAmount; } /// <summary> /// 触发按钮时打开开关,否则开关关闭,初始数值 /// </summary> /// <param name="other">触碰到的按钮</param> private void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Hand") { flag = true; } else { flag = false; transform.GetComponent<Image>().fillAmount = 0; FiA = transform.GetComponent<Image>().fillAmount; } } /// <summary> /// 当离开开关,关闭开关初始化数值 /// </summary> /// <param name="other">离开的按钮</param> private void OnTriggerExit2D(Collider2D other) { if (other.tag == "Hand") { falg = false; transform.GetComponent<Image>().fillAmount = 0; FiA= transform.GetComponent<Image>().fillAmount; } } /// <summary> /// 选中按钮后执行的方法 /// </summary> private void FixedUpdate() { if (flag && FiA<1) { transform.GetComponent<Image>().fillAmount += 0.1f * Time.deltaTime; FiA = transform.GetComponent<Image>().fillAmount; } if(FiA>=1&&flag) { flag = false; //进度条充满后执行的方法 click(); } } //需要实现的方法 public void click() { } }
Unity3D里经常会用到触发Ui,以上代码可以实现触摸时加载进度条,离开时进度条初始化!一般用来开始游戏,停留按住一定时间后加载所要执行的方法!