繁体   English   中英

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

[英]How can I change image fillamount direction?

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);

一个像我想要的工作解决方案:

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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM