Daniel Lip
Daniel Lip

Reputation: 11319

Is there any way to detect what ui type the object is?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Fader : MonoBehaviour
{
    #region FIELDS
    public GameObject fadeOutUIGameobjectImage;
    public float fadeSpeed = 0.8f;
    public bool fadeOnStart = false;

    private Image fadeOutUIImage;

    private void Start()
    {
        if(fadeOnStart == true)
        {
           StartCoroutine(Fade(FadeDirection.Out));
        }
    }

    public enum FadeDirection
    {
        In, //Alpha = 1
        Out // Alpha = 0
    }
    #endregion

    #region FADE
    public IEnumerator Fade(FadeDirection fadeDirection)
    {
        fadeOutUIGameobjectImage.SetActive(true);

        float alpha = (fadeDirection == FadeDirection.Out) ? 1 : 0;
        float fadeEndValue = (fadeDirection == FadeDirection.Out) ? 0 : 1;
        if (fadeDirection == FadeDirection.Out)
        {
            while (alpha >= fadeEndValue)
            {
                SetColorImage(ref alpha, fadeDirection);
                yield return null;
            }
            fadeOutUIGameobjectImage.SetActive(false);
        }
        else
        {
            fadeOutUIGameobjectImage.SetActive(true);
            while (alpha <= fadeEndValue)
            {
                SetColorImage(ref alpha, fadeDirection);
                yield return null;
            }
        }
    }
    #endregion

    #region HELPERS
    public IEnumerator FadeAndLoadScene(FadeDirection fadeDirection, string sceneToLoad)
    {
        yield return Fade(fadeDirection);
        SceneManager.LoadScene(sceneToLoad);
    }

    private void SetColorImage(ref float alpha, FadeDirection fadeDirection)
    {
        if (fadeOutUIImage == null)
        {
            fadeOutUIImage = fadeOutUIGameobjectImage.GetComponent<Image>();
        }

        fadeOutUIImage.color = new Color(fadeOutUIImage.color.r, fadeOutUIImage.color.g, fadeOutUIImage.color.b, alpha);
        alpha += Time.deltaTime * (1.0f / fadeSpeed) * ((fadeDirection == FadeDirection.Out) ? -1 : 1);
    }
    #endregion
}

In this case it's Image but if for example the variable fadeOutUIGameobjectImage is a Text or CanvasRenderer or only Canvas or a Cube or Cylinder or any ui element then in the two lines :

private Image fadeOutUIImage;

And

fadeOutUIImage = fadeOutUIGameobjectImage.GetComponent<Image>();

It's related to Image type, Is there a way to find the type automatic ? Sometimes I might want to fade a Cube or a Canvas or a CanvasRenderer or Text or TextMesh or TextMeshPro or Image or MeshRenderer or maybe making an array of fadeOutUIGameobjectImage instead a single one like now.

The idea is to detect the type automatic.

Upvotes: 0

Views: 386

Answers (2)

zambari
zambari

Reputation: 5035

Image, RawImage, Text etc all inherit from a base type Graphic (which provides basic access for things like Color).

On the other hand, MeshRenderer, LineRenderer etc inherit from Renderer, which provides access to Material etc.

as long as you don't need child specific features, you can use the base type and stay agnostic to what the actual type is

Upvotes: 0

Kale_Surfer_Dude
Kale_Surfer_Dude

Reputation: 914

Use is operator?

if(obj is Image)
     //... Do something for Image
else if(obj is TextMeshProUGUI)
     //... Do something for text mesh pro UGui

Upvotes: 3

Related Questions