Wednesday, November 9, 2022
HomeGame DevelopmentEasy methods to prolong a C# Unity class and override a way...

Easy methods to prolong a C# Unity class and override a way in a grandparent class


I’m utilizing C# in a Unity undertaking and am making an attempt to know whether it is attainable and how you can prolong courses in order that I can implement my very own customized code with out modifying the inventory courses – which is able to permit me to simply replace the inventory code base sooner or later.

That is an instance – right here is a straightforward illustration of the inventory code:

public summary class StockClassA : StockClassX  
{
    public int MethodIWantToOverride()
        {
          return 0;
        }
}

// THIS IS A COMPONENT PLACED IN A UNITY OBJECT AND ITS METHODS CALLED
// BY OTHER COMPONENTS (these different parts have a saved reference to this)
[Serializable]
 ...
public class StockClassB : StockClassA
{
    // ... numerous public strategies ...  (I'll wish to override a few of these as properly)
}

In inventory type, numerous parts in unity makes use of the reference to StockClassB and name the MethodIWantToOverride() – as in :

StockClassB.MethodIWantToOverride();

The issue is that if I prolong StockClassB, add my alternative methodology and replace all of the references within the unity parts to my new class, my new methodology by no means will get known as.
Instance of my code:

// my class must be of sort StockClassB as a result of different courses
// are on the lookout for that sort (and I dont wish to change the kind
// as a result of that might require altering the inventory code in these parts)
public class  MYStockClassB : StockClassB  
{
    public new int MethodIWantToOverride()
    {
        return 1;
    }
 }

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments