I have an empty Game Object inside a Canvas.
On this empty Game Object I have a Canvas Group and a script component.
In the inspector for the Canvas Group I set the alpha to .1.
Inside my script I have
voidStart(){
iTween.FadeTo(gameObject,iTween.Hash(“alpha”,1,“time”,1));
}
But nothing happens. The CanvasGroup remains transparent (.1 alpha)
Does anyone know how to fade the alpha of a canvas Group dynamically, with or without iTween?
sluice
September 26, 2014, 2:11pm
2
Here’s a video(@ ~17:30) that shows one way of doing it. If you want a to do it by code, see my EDIT (below)
EDIT:
I made it work, by code in just a few minutes. Here’s how:
First off, do as the video says:
Inside my Canvas, make a group:
With the Canvas Selected, Go to: Create / Empty Child
Add a Canvas Group to that empty child.
put all the UI elements you want to fade in that empty child.
Then, instead of going through the animator thingy, put this script on the new Canvas Group .
using UnityEngine;
using System.Collections;
public class FadeMe : MonoBehaviour
{
CanvasGroup canvasGroup;
void Awake()
{
canvasGroup = GetComponent<CanvasGroup>();
}
void Start ()
{
StartCoroutine("FadeOut");
}
IEnumerator FadeOut()
{
float time = 1f;
while(canvasGroup.alpha > 0)
{
canvasGroup.alpha -= Time.deltaTime / time;
yield return null;
}
}
}
1 Like
Kogar
September 26, 2014, 2:52pm
3
Itween only uses the alpha value of the gameobject. It does not know of the canvas group. So you would have to directly change the alpha value of the canvas group.
I’ll quote myself
A short search should have found the thread in the Developer Preview Forum which was still only on the second page.
With itween it should also be possible. Have looked something up. Maybe something like that? iTween for Unity by Bob Berkebile (pixelplacement)
CanvasGroup cg = GetComponent<CanvasGroup>();
float targetAlphaValue = 1; //between 0-1
if(cg.alpha!=targetAlphaValue)
cg.alpha = FloatUpdate(cg.alpha, float targetAlphaValue, 1f)
Ok so using iTween it is NOT possible to tween the alpha of a Canvas Group. Disappointed!
Why can’t I simply do gameObject.alpha=foo ?
Will use your recommended solution
itween tweens the renderer alpha, but unity 4.6Canvas is Graphic. You need to tween it yourself.
you can tween the whole gameobject:
void CrossFadeAlpha (GameObject gameObject, int target, float animationTime) {
foreach (Graphic graphic in gameObject.GetComponentsInChildren<Graphic>()) {
graphic.CrossFadeAlpha(target, animationTime, false);
} }
you can use iTween.ValueTo and point it to a method that modifies the CanvasGroup alpha
Great suggestion @polytropoi !