There is a code that gradually changes transparency on command (just an alpha component)

Call

InvokeRepeating("MoreTransparency", 0, 0.1f); InvokeRepeating("LessTransparency", 0, 0.1f); 

We carry out

 private void LessTransparency() { if (alphalvl >= 1) { CancelInvoke("LessTransparency"); } CancelInvoke("MoreTransparency"); alphalvl = alphalvl + 0.3f; ChangeColor(); } private void MoreTransparency() { if(alphalvl <= 0) { CancelInvoke("MoreTransparency"); } CancelInvoke("LessTransparency"); alphalvl = alphalvl - 0.3f; ChangeColor(); 

The code looks cumbersome and generally so-so. But it works. How can this be transformed into one beautiful method?

  • What is ChangeColor() ? Where do InvokeRepeating ("MoreTransparency" and InvokeRepeating ("LessTransparency"? Call? Transparency changes as blinking or one-time from one value to another (for example, one button is pressed - transparency decreases, another is increased - increased)? - Alexey Shimansky
  • @ Alexey Shimansky Clicked - InvokeRepeating was launched, the object gradually became transparent - non-transparent. Press another button - the object slowly dissolved. - Dmitrii

1 answer 1

It will be more correct so:

  private void LessTransparency() { alphalvl += 0.3f; if (alphalvl >= 1) { alphalvl = 1; CancelInvoke("LessTransparency"); } // CancelInvoke("MoreTransparency"); <--- зачем? ChangeColor(); } 

You can still try:

  void Start () { StartCoroutine("MoreTransparency"); } IEnumerator MoreTransparency() { while(alphalvl <= 1) { ChangeColor(); alphalvl += 0.1f; yield return new WaitForSeconds(0.1f); } } 

https://docs.unity3d.com/ScriptReference/Coroutine.html

Or universal function:

 void Start () { StartCoroutine("ChangeTransparency", -0.1); } IEnumerator ChangeTransparency(float val) { // Чтобы избежать бесконечного цикла. if (val != 0) while(val > 0 ? alphalvl <= 1 : alphalvl >= 0) { ChangeColor(); alphalvl += val; yield return new WaitForSeconds(0.1f); } } 
  • Duck is still two methods. Yes, and korutina. This is a complication of the code IMHO. And you need either a decrease in size or simplification. CancelInvoke("MoreTransparency"); It has a simple goal - if we press the button to remove transparency while the button is working, add transparency , they will interfere with each other. CancelInvoke("MoreTransparency"); breaks a competing process when starting a new one. PS I saw a new version with a universal function. I will test, I will unsubscribe about the results - Dmitrii
  • one
    Complicating a code is when the method of increasing transparency is why it is also responsible for the method of reducing. - Valera Kvip
  • Could you describe what this section of the code gives while(val > 0 ? alphalvl <= 1 : alphalvl >= 0) I understand this analogous code while(if (val >0){alphalvl <= 1}else{alphalvl >=0) And it is completely incomprehensible to me how it works. Explain please with text or more simple code, without a ternary operator - Dmitrii
  • one
    @Dmitrii, you understood correctly. It works like this: if val <0, then the transparency goes down. And you need to check that she did not become below zero. If val> 0, then transparency goes on increasing, then it should not exceed one. - Valera Kvip