Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JohnBaker · May 07, 2013 at 08:13 PM · inheritanceclassesgenerics

Extended class attributes/methods referenced as base class attributes/methods

I have a few classes that extend MonoBehaviour, for example; WidgetController and PanelController.

From these classes I extend various other classes; ButtonController : WidgetController and PanelLoginController : PanelController.

The definition of the WidgetController has a public PanelController attribute

public class WidgetController : MonoBehaviour { public PanelController panelController; ... }

Inherently all of the different types of widget controllers (buttons, checkboxes etc) have a PanelController panelControoler attribute. In the editor they are referenced to extended classes of PanelControllers, such as a PanelLoginController.

Any attributes or methods in PanelController are avaiable in the extended WidgetController, as expected. However, when I create a specialized method or attribute in a extented PanelContrller, the PanelController panelController attribute does not reflect these.

Example: public class PanelLoginController : PanelController {

 public UIInput inputUsername;
 public UIInput inputPassword;
 
 private void AttemptLogin() {
     string username = inputUsername.label.text;    
     string password = inputPassword.label.text;
 }
 

}

public class PanelLoginButtonLogin : ButtonController {

 void OnClick() {
     Debug.Log(panelController.GetType()); // returns PanelLoginController
     panelController.AttemptLogin(); // error
     panelController.inputUsername = ""; // error
     
     panelController.Invoke("AttemptLogin", 0f); // this works, but I cannot pass parameters and it seems like a work around 
 }
 

}

I am trying to wrap my head around Generics, but havn't quite figured them out yet. I believe they may be the required course of action, but I am not entirely sure.

One work around would be to define a new attribute of the extented panel controller in each extented widget controller- on construct cast the PanelController panelController to that attribute. This does not seem like the proper approach, and would require more code maintenance.

Any suggestions or solutions are greatly appreciated.

Cheers!

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
0

Answer by whydoidoit · May 07, 2013 at 09:25 PM

I'd do something like this:

    void OnClick() {

          var loginController = panelController as PanelLoginController;
          if(loginController == null) 
                throw new Exception("Panel controller wasn't a login controller");
  
          loginController.AttemptLogin();
          loginController.inputUserName = "";
             
    }

You could define a generic base class:

    public class WidgetController<T> : WidgetController where T : PanelController
    {
             public new T panelController
             {
                 get
                 {    
                      return base.panelController as T;
                 }
                 set 
                 {
                      base.panelController = value;
                 }
             }
    }

Then you could just do:

         public class PanelLoginButtonLogin : WidgetController<PanelLoginController>
         {
                 void OnClick()
                 {
                       panelController.AttemptLogin();
                       panelController.inputUserName = "":
                 }
         }
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 whydoidoit · May 11, 2013 at 07:50 AM 0
Share

Was this any help?

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

13 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

Related Questions

Inheriting from classes from other files (Javascript) 1 Answer

Class extends Object but does not inherit variables 1 Answer

Abstract class question 1 Answer

Defining and inheriting from a Javascript Class 0 Answers

Can't inherit from namespace 0 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