如何更改图像填充量方向?

时间:2019-12-30 18:36:02

标签: c# unity3d

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之间。

有没有办法做到这一点?

2 个答案:

答案 0 :(得分:2)

这有点hack,但是您可以切换为负比例值并再次开始增加fillAmout。它将以另一个方向填充,以0点为枢轴。

假设您正在水平填充,则翻转比例尺看起来像这样:

transform.localScale = new Vector3(-1, 1, 1);

答案 1 :(得分:0)

一种像我想要的可行解决方案:

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;
    public bool useBackGroundImage = true;

    private RectTransform rectTransform;
    private Image imageComp;
    private Image backGroundImage;

    // Use this for initialization
    void Start()
    {
        imageComp = GetComponent<RectTransform>().GetComponent<Image>();
        backGroundImage = transform.parent.gameObject.transform.GetComponent<Image>();

        UseBackgImage();
    }

    // Update is called once per frame
    void Update()
    {
        UseBackgImage(useBackGroundImage);

        if (fillAmount == true)
        {
            if (imageComp.fillAmount != 1f)
            {
                if (changeFillDir == true)
                {
                    if (imageComp.fillAmount == 0)
                    {
                        imageComp.fillAmount = 1f;
                    }
                    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));
            }
        }
    }

    private void UseBackgImage()
    {
        if (useBackGroundImage == true)
        {
            backGroundImage.enabled = true;
            rectTransform = transform.parent.gameObject.transform.GetComponent<RectTransform>();
        }
        else
        {
            backGroundImage.enabled = false;
            rectTransform = transform.GetComponent<RectTransform>();
        }
    }

    private void UseBackgImage(bool UseBackGroundImage)
    {
        if (useBackGroundImage == true)
        {
            backGroundImage.enabled = true;
        }
        else
        {
            backGroundImage.enabled = false;
        }
    }
}