the idea is that handyPLUS has all, and exactly the same, functions as handy - but - one extra line of code.
So essentially, you could achieve this manually, by copying and pasting:
static class handyPLUS
{
function aa ( . paste from 'handy aa' . )
{
handy.aa( same );
one more line of code;
}
function bb ( . paste from 'handy bb' . )
{
handy.abb same );
one more line of code;
}
function cc ( . paste from 'handy cc' . )
{
handy.cc( same );
one more line of code;
}
}
Of course, that would be lame. What’s the proper way to do this in Unityscript? Cheers !
My attempt at solving this is the following construct:
//The Basic Class//
#pragma strict
public class Helper {
public var addedCode = function(){};
function aa() {
replace();
Debug.Log("aa");
addedCode();
}
function bb() {
replace();
Debug.Log("bb");
addedCode();
}
function replace(){}
}
//The Extended Class//
#pragma strict
public class HelperPLUS extends Helper{
//a Singleton pattern
private static var helperInstance = new HelperPLUS();
public static function Instance(){
if(helperInstance == null ) helperInstance = new HelperPLUS();
return helperInstance;
}
override function replace(){
addedCode = function(){
//add the extra code here.
Debug.Log("extended");
};
}
}
Calling the HelperPLUS methods is as always with singletons: HelperPLUS.Instance().aa()
I don’t know if this is what they call UnityScript or if it passes as real javascript, but it works on my editor.
Okay so this is c# - so if you need the answer to be exactly in unityscript I will convert this to a comment.
using UnityEngine;
using System.Collections;
public class Handy : HandyHelpers.HandyBase<HandyHelpers.HandyDefaultExtension>
{
}
public class HandyPlus : HandyHelpers.HandyBase<HandyHelpers.HandyPlusExtension>
{
}
namespace HandyHelpers
{
public class HandyBase<T> where T : IExtension, new()
{
protected static T mExtension = new T();
public static void aa( /* Params */ )
{
// Do some stuff
Debug.Log("aa");
mExtension.DoExtension();
}
public static void bb( /* Params */ )
{
// Do some stuff
Debug.Log("bb");
mExtension.DoExtension();
}
public static void cc( /* Params */ )
{
// Do some stuff
Debug.Log("cc");
mExtension.DoExtension();
}
}
public interface IExtension
{
void DoExtension();
}
public class HandyDefaultExtension : IExtension
{
public void DoExtension()
{
}
}
public class HandyPlusExtension : IExtension
{
public void DoExtension()
{
Debug.Log("Extension");
}
}
}
The namespace HandyHelpers is there to basically hide all the gruesome base classes. Then any line of code you want to execute after all functions can simply be slotted into DoExtension allowing you to easily extend handy into whatever you want.