c# - 如何更改图像填充量方向?
问题描述
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class loadingcolorful : MonoBehaviour
{
public float speed = 0.0f;
public bool fillAmount = false;
public bool rotate = false;
public bool changeRotationDir = false;
public bool changeFillDir = false;
private RectTransform rectTransform;
private Image imageComp;
// Use this for initialization
void Start()
{
imageComp = GetComponent<RectTransform>().GetComponent<Image>();
rectTransform = transform.parent.gameObject.transform.GetComponent<RectTransform>();
}
// Update is called once per frame
void Update()
{
if (fillAmount == true)
{
if (imageComp.fillAmount != 1f)
{
if (changeFillDir == true)
{
imageComp.fillAmount -= speed;
}
else
{
imageComp.fillAmount += speed;
}
}
else
{
imageComp.fillAmount = 0f;
}
}
else
{
if (imageComp.fillAmount == 0f)
{
imageComp.fillAmount = 0f;
}
else
{
imageComp.fillAmount = 1f;
}
}
if (rotate == true)
{
if (changeRotationDir == true)
{
rectTransform.Rotate(new Vector3(0, 0, -1));
}
else
{
rectTransform.Rotate(new Vector3(0, 0, 1));
}
}
}
}
问题出在这个地方:
if (changeFillDir == true)
{
imageComp.fillAmount -= speed;
}
当它达到 0 时,它不会继续将图像填充到另一个方向,只是图像将保持为空。我的猜测是 FillAmount ragne 的值介于 1 和 0 之间。
有没有办法让它发生?
解决方案
这有点小技巧,但您可以切换到负比例值并再次开始增加 fillAmout。这将使它填充另一个方向,以 0 点为轴心。
假设你是水平填充,翻转比例看起来像这样:
transform.localScale = new Vector3(-1, 1, 1);