[Unity]嘗試建立一個圓形儀表,當鼠標點擊時會向前移動。


有時候在玩遊戲時,您會看到 UI 上有一個圓形的量表,會減少或增加,這次我想做一個類似的東西。

首先,準備一個透明背景的圓形 png 圖片。

圖片應放置在 unity 中。

接下來,在畫布中放置圖片,這次命名為「圓」。

將 Cricle Image 元件的 SourceImage 設定為準備好的 png 圖片,將 Image Type 設定為 Filled,Fill Method 設定為 Radial360,Fill Origin 設定為 Top。

這次在 Circle 中建立了一個腳本,說明如下。

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

public class Circle : MonoBehaviour {
  // Use this for initialization
  void Start () {
    this.GetComponent<Image>().fillAmount = 0;
  }

  // Update is called once per frame
  void Update () {
    if(Input.GetMouseButton (0)){
      this.GetComponent<Image>().fillAmount += 0.01f;
      if( this.GetComponent<Image>().fillAmount >= 1.0f ) {
        this.GetComponent<Image>().fillAmount = 0;
      }
    }
  }
}

當滑鼠左鍵按下時,圓規會向前捲動,當滑鼠放開時,圓規會停止。
由於這是範例程式碼,所以圓形量表會在圓滿時返回 0。

如果您喜歡,請看看範例程式碼。m

– 範例 -.