Skip to content

Instantly share code, notes, and snippets.

@yosizo
Last active December 12, 2017 03:32
Show Gist options
  • Save yosizo/e8d67115a0ba2ab7c4c12b4344620531 to your computer and use it in GitHub Desktop.
Save yosizo/e8d67115a0ba2ab7c4c12b4344620531 to your computer and use it in GitHub Desktop.
uGUIでプログレスバーの動きに合わせて子要素を動かす ref: https://qiita.com/yosizo@github/items/8ce64624f598aab6a672
using UnityEngine;
using UnityEngine.UI;
public class barAnchor : MonoBehaviour
{
Image barImage;
[SerializeField]
Text textObj;
[SerializeField]
RectTransform anchorObj;
// Use this for initialization
void Start()
{
barImage = GetComponent<Image>();
}
// Update is called once per frame
void Update()
{
// イメージのfillAmountを変化させる
barImage.fillAmount += 0.01f;
if (barImage.fillAmount >= 1.0f)
{
barImage.fillAmount = 0f;
}
// テキストを更新
textObj.text = barImage.fillAmount.ToString("P1");
// fillAmountの値に合わせてアンカーを更新
anchorObj.anchorMin = new Vector2(barImage.fillAmount, anchorObj.anchorMin.y);
anchorObj.anchorMax = new Vector2(barImage.fillAmount, anchorObj.anchorMax.y);
anchorObj.anchoredPosition = Vector2.zero;
}
}
using UnityEngine;
using UnityEngine.UI;
public class circleAnchor : MonoBehaviour
{
Image barImage;
[SerializeField]
Text textObj;
[SerializeField]
RectTransform anchorObj;
// Use this for initialization
void Start()
{
barImage = GetComponent<Image>();
}
// Update is called once per frame
void Update()
{
// イメージのfillAmountを変化させる
barImage.fillAmount += 0.01f;
if (barImage.fillAmount >= 1.0f)
{
barImage.fillAmount = 0f;
}
// テキストを更新
textObj.text = barImage.fillAmount.ToString("P1");
// fillAmountの値に合わせてアンカーの角度を更新
anchorObj.eulerAngles = new Vector3(0, 0, barImage.fillAmount * -360);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment