[Unity]尝试创建一个圆规,当鼠标点击时,圆规会向前移动。
有时在玩游戏时,你会看到用户界面上有一个圆圈形的仪表盘,它可以递减或递增,这次我想做一个类似的东西。
首先,准备一张透明背景的 png 圆图。
图片应放置在统一位置。
接下来,在画布中放置图像,这次命名为 “圆形”。
将 Cricle 图像组件的 SourceImage 设置为准备好的 png 图像,将图像类型设置为填充,将填充方法设置为径向 360,将填充原点分别设置为顶部。
这一次,我们在 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