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 /
  • Help Room /
avatar image
0
Question by JannickL · Jan 23, 2017 at 05:47 PM · scripting problemscriptingproblem

How can i avoid to repeating the same code for a different class?

Hey together, is there a proper way to avoid repeating the exactly same code (just for a different class?)

Here is the code:

 public void DifferentClasses<T>() where T : class 
     {
         if (typeof(T) == typeof(Class1))
         {
             Class1[] cs = FindObjectsOfType<Class1>();
             foreach (Class1 c in c)
             {
                 c.curLoadTime = 0;
                 c.loadingImage.fillAmount = 0;
             }
         }
 
         if (typeof(T) == typeof(Class2))
         {
             Class2[] cs = FindObjectsOfType<Class2>();
             foreach (Class2 c in cs)
             {
                 c.curLoadTime = 0;
                 c.loadingImage.fillAmount = 0;
             }
         }
 }

At least i want to have method were i can send in some classes at let them execute the same code.

Thank you

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by EDevJogos · Jan 23, 2017 at 06:46 PM

Create a interface with a method like DoSomething(); and implement it on the classes that you looking for, then in your generic just say that T has this interface implemented:

 public void DifferentClasses<T>() where T : class : IMyInterface

With this then you can do:

 T[] cs = FindObjectsOfType(typeof(T)) as T[];
 foreach (T c in cs)
 {
        c.DoSomething();
 }

For details on interface see:

https://www.youtube.com/watch?v=g3UUnCO_pDs

Comment
Add comment · 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
0

Answer by HenryStrattonFW · Jan 23, 2017 at 06:29 PM

Yes, and you're already doing part of it, generic parameters, you are not limited to a single parameter!

So perhaps something like this would be of use.

 public void DifferentClasses2<T, C>()
         where T : class
         where C : class
     {
         if( typeof( T ) == typeof( C ) )
         {
             C[] cs = FindObjectsOfType<C>();
             foreach( C c in cs )
             {
                 // Do stuff on C here
             }
         }
     }

Just keep in mind, that as far as the compiler is concerned your foreach type of C will only have access to the things in the constrained base type (in this case class) So depending on what classes you are trying to work on here you may wish to constrain T and C to types that are a little higher up in the class hierarchy.

you could even call this from your different classes method if you wanted, like so.

 public void DifferentClasses<T>() where T : class 
 {
     DifferentClasses2<T, Class1>();
     DifferentClasses2<T, Class2>();
     DifferentClasses2<T, Class3>();
     DifferentClasses2<T, Class4>();
     // and so on, you get the idea.
 }
Comment
Add comment · 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

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

83 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 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

How do I stop the player from shaking in Unity2D? 2 Answers

Unity crashes when compiling this script. Where is the problem? 0 Answers

Try part of C# not being Read 1 Answer

UnityCar - Controlling Speed with a duration? 2 Answers

How do I change the scene after 2 objects are destroyed (SetActive false) 1 Answer


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