Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by umurcg · Jan 31, 2017 at 05:27 PM · generics

Can I right a generic method for components having same member variable?

Hello,

I am trying to write a static enumeration function that makes transparent components that having color member variable. While I know how to write a generic function, I couldn't figure it out how to change color variable of T type object.

Here is my three fade method for image, rawimage, and text components. So basically I want to make these functions into one with a generic method.

   public static IEnumerator<float> _fadeInfadeOut(GameObject rawImage, float speed)
     {
         RawImage r = rawImage.GetComponent<RawImage>();
 
         Color textureColor = r.color;
         float a = textureColor.a;
 
 
 
         if (a == 0)
         {
             while (a < 1)
             {
                 a += Time.deltaTime * speed;
                 textureColor.a = a;
                 r.color = textureColor;
                 yield return 0;
             }
             textureColor.a = 1;
             r.color = textureColor;
 
         } else
         {
             while (a > 0)
             {
                 a -= Time.deltaTime * speed;
                 textureColor.a = a;
                 r.color = textureColor;
                 yield return 0;
             }
             textureColor.a = 0;
             r.color = textureColor;
 
         }
 
     }
 
     public static IEnumerator<float> _fadeInfadeOutImage(GameObject image, float speed)
     {
         Image i = image.GetComponent<Image>();
 
         Color textureColor = i.color;
         float a = textureColor.a;
 
 
 
         if (a == 0)
         {
             while (a < 1)
             {
                 a += Time.deltaTime * speed;
                 textureColor.a = a;
                 i.color = textureColor;
                 yield return 0;
             }
             textureColor.a = 1;
             i.color = textureColor;
 
         }
         else
         {
             while (a > 0)
             {
                 a -= Time.deltaTime * speed;
                 textureColor.a = a;
                 i.color = textureColor;
                 yield return 0;
             }
             textureColor.a = 0;
             i.color = textureColor;
 
         }
 
     }
 
     public static IEnumerator<float> _fadeInfadeOutText(GameObject text, float speed)
     {
         Text t = text.GetComponent<Text>();
 
         Color textureColor = t.color;
         float a = textureColor.a;
 
 
 
         if (a == 0)
         {
             while (a < 1)
             {
                 a += Time.deltaTime * speed;
                 textureColor.a = a;
                 t.color = textureColor;
                 yield return 0;
             }
             textureColor.a = 1;
             t.color = textureColor;
 
         }
         else
         {
             while (a > 0)
             {
                 a -= Time.deltaTime * speed;
                 textureColor.a = a;
                 t.color = textureColor;
                 yield return 0;
             }
             textureColor.a = 0;
             t.color = textureColor;
 
         }
 
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Vicarian · Jan 31, 2017 at 06:03 PM

@umurcg If you have a look at each of those types in Visual Studio, they all inherit MaskableGraphic, so that's the type you can filter on for the generic. Also, I don't think your IEnumerator requires a generic type, since you're always returning zero. The following ought to do:

 public static IEnumerator<T> _fadeInfadeOut(GameObject obj, float speed) where T: MaskableGraphic
 {
      T t = obj.GetComponent<T>();
  
      Color textureColor = t.color;
      float a = textureColor.a
  
      if (a == 0)
      {
          while (a < 1)
          {
              a += Time.deltaTime * speed;
              textureColor.a = a;
              t.color = textureColor;
              yield return null;
          }
          textureColor.a = 1;
          t.color = textureColor;
  
      }
      else
      {
          while (a > 0)
          {
              a -= Time.deltaTime * speed;
              textureColor.a = a;
              t.color = textureColor;
              yield return null;
          }
          textureColor.a = 0;
          t.color = textureColor;
      }
  }

If you do need to do something with the zero you return, you can add a second generic type parameter, as I'm sure you're aware.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image umurcg · Feb 01, 2017 at 06:36 PM 0
Share

I am using more efficient coroutine plug in which forces me to return 0. But I can use normal coroutine for that method. Thanks.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

62 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

differences: generic GetComponent. 3 Answers

Count returning 0 when called outside object~ 1 Answer

Generic MonoBehaviour with nested class serialization issue 1 Answer

Initialize UnityEvent via script 1 Answer

Why can't I use a variable of the same type as a static generic variable when using foreach? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges