Skip to content

Instantly share code, notes, and snippets.

@yosizo yosizo/barAnchor.cs
Last active Dec 12, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.