Answer the question
In order to leave comments, you need to log in
How to change mass properties of elements?
I now need to change the alpha slowly for several elements, I do this
Update(){
ActiveAlpha();
}
private void ActiveAlpha(){
float alf += Time.deltaTime;
for(int n = 0; n < ArrayObj; ++n){
ArrayObj[n].gameObject.GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, alf);
}
}
Answer the question
In order to leave comments, you need to log in
You create a script AlphaChanger.cs, it contains the following code
private SpriteRenderer spriteRenderer; //Кэшируем компонент
[SerializeField] private float speed = 1; //Скорость фейдинга
private void Awake()
{
spriteRenderer = GetComponent<SpriteRenderer>();
}
public void Show()
{
StartCoroutine(SetVisibility(1));
}
public void Hide()
{
StartCoroutine(SetVisibility(0));
}
private IEnumerator SetVisibility(float finish)
{
var color = spriteRenderer.color;
while (true)
{
color.a = Mathf.MoveTowards(color.a, finish, speed * Time.deltaTime);
spriteRenderer.color = color;
if (color.a == finish) break;
yield return null;
}
}
private AlphaChanger[] alphaChangers; //Кэшируем компоненты детей
private void Awake()
{
alphaChangers = GetComponentsInChildren<AlphaChanger>();
}
public void ShowAll()
{
alphaChangers.ForEach(a => a.Show());
}
public void HideAll()
{
alphaChangers.ForEach(a => a.Hide());
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question