简体   繁体   中英

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

The problem is at this place :

if (changeFillDir == true)
                    {
                        imageComp.fillAmount -= speed;
                    }

When it will get to 0 it will not continue to fill the image to the other direction just the image will stay empty. My guess is that the values of the FillAmount ragne is between 1 and 0.

Is there a way to make it happen ?

It's a bit of a hack, but you can switch to a negative scale value and start increasing the fillAmout again. This will make it fill in the other direction, taking the 0 point as pivot.

Assuming you are filling horizontally, flipping the scale would look something like this:

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

A working solution like I wanted :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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