用Slider來控制Cube旋轉
Slider是滑動條。
效果:
就是就是獲取Slider的value值。但這里我們用動態改變
先看看Slider的事件:
當改變value的時候觸發。
現在寫代碼
using UnityEngine; using System.Collections; public class cube : MonoBehaviour { public float speed; // Use this for initialization void Start() { } // Update is called once per frame void Update() { transform.Rotate(Vector3.up * Time.deltaTime * speed); } public void c(float i) { speed = i; } }
public void c(float i)就的動態改變值。這里參數類型必須是float,
你會發現 “Dynamic float”下面顯示了方法名
仔細觀察。你會發現。Button對象會有Button組件。同樣Slider也會有Slider組件
所以。slider組件可以添加到任何對象上。當對象添加了slider組件。那么該對象就有滑動功能了。如果用UGUI自帶的slider對象來做血條是可以實現。但效果並不優美。所以可以用美工處理好的圖片來做血條
比如:
可以看出來。這里有3張圖片。
1:底圖(Image1)
2:背景圖片(background)
3:前景圖片(Fill)
給Image1對象添加Slider組件,把Fill拖拽到Fill Rect
把Fill中的Image組件修改
這樣就能從右像左移動
還需要取消鼠標跟Image1的交互。既不能用鼠標修改。只能通過代碼修改Value的值
這樣就完成了血條的UI。然后就是代碼修改血條的血量了
游戲中血條都會慢慢減少。有一個緩動效果。慢慢過渡

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using UnityEngine; 6 using UnityEngine.UI; 7 /// <summary> 8 /// 進度條緩動效果 9 /// </summary> 10 public class progressbarSlow : MonoBehaviour 11 { 12 //private static progressbarSlow instance; 13 14 15 //public float maxVlaue; 16 // public int v; 17 // public int initVlue; 18 public int targetValue; // 19 //public float smooting = 2; 20 21 public static Slider slider; 22 23 public bool start; 24 25 //public static progressbarSlow Instance 26 //{ 27 // get 28 // { 29 // if (instance == null) 30 // instance = new progressbarSlow(); 31 32 // return instance; 33 // } 34 //} 35 36 //private progressbarSlow() { } 37 38 39 public delegate void CallBack(); 40 public CallBack back; 41 public float timer = 1; 42 43 Image fill; 44 45 46 //停止 47 public float stop; 48 49 50 //執行次數 51 public int index = 1; 52 53 void Awake() 54 { 55 slider = this.GetComponent<Slider>(); 56 slider.wholeNumbers = false; //float過渡 57 fill = this.transform.Find("Fill").GetComponent<Image>(); 58 //maxVlaue = slider.maxValue; 59 //initVlue = 0; //初始化值 60 index = 1; 61 timer = 8; //最大值 62 fill.fillAmount = 0; 63 //stop = 1f / slider.maxValue; 64 65 66 } 67 void Start() 68 { } 69 public float curr = 1; 70 void Update() 71 { 72 //Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * smooting)&& targetValue != slider.value; 73 74 if (start) 75 { 76 timer -= Time.deltaTime; 77 78 //slider.value = Math.Abs((targetValue - timer) / targetValue); 79 80 //if (targetValue <= slider.value) 81 //{ 82 // //timer = 1; 83 // start = false; 84 // curr = targetValue; 85 // //slider.value = targetValue; 86 // if (back != null) 87 // back(); 88 //} 89 stop = ((float)Math.Round(1f / slider.maxValue, 2)); 90 fill.fillAmount = (8 - timer) / 8; 91 92 float ab = fill.fillAmount; 93 if (ab > stop * index) 94 { 95 index++; 96 start = false; 97 if (back != null) 98 back(); 99 } 100 101 if (fill.fillAmount == 1) 102 { 103 index = 1; 104 start = false; 105 timer = 8; 106 } 107 //時間到 108 //if (timer <= 0) 109 //{ 110 // timer = 1; 111 // start = false; 112 // curr = targetValue; 113 // //slider.value = targetValue; 114 // if (back != null) 115 // back(); 116 //} 117 } 118 } 119 120 public int TargetValue 121 { 122 set 123 { 124 targetValue = value; 125 } 126 } 127 128 public bool Starts 129 { 130 set 131 { 132 start = value; 133 } 134 } 135 136 public CallBack Callback 137 { 138 set 139 { 140 back = value; 141 } 142 } 143 }