//Subscribes a behaviour
 public static void SubscribeItem(OverridableMonoBehaviour behaviour)
 {
     if (instance != null && behaviour != null)
     {
         instance.ScheduleItemAddition(behaviour);
     }
 }
 //Unsubscribes a behaviour
 public static void UnsubscribeItem(OverridableMonoBehaviour behaviour)
 {
     if (behaviour && instance)
     {
         instance.ScheduleItemRemoval(behaviour);
     }
 }
    private void RemoveSpecificItemFromArray(OverridableMonoBehaviour behaviour)
    {
        int addAt = 0;

        OverridableMonoBehaviour[] tempArray = new OverridableMonoBehaviour[array.Length - 1];

        for (int i = 0; i < array.Length; i++)
        {
            if (array[i] == null)
            {
                continue;
            }
            else if (array[i] == behaviour)
            {
                continue;
            }
            tempArray[addAt] = array[i];
            addAt++;
        }

        array = new OverridableMonoBehaviour[tempArray.Length];

        for (int i = 0; i < tempArray.Length; i++)
        {
            array[i] = tempArray[i];
        }

        count = array.Length;
    }
    private void ScheduleItemAddition(OverridableMonoBehaviour behaviour)
    {
        if (!CheckIfArrayContainsItem(regularArray, behaviour) && behaviour is IUpdatable)
        {
            ExtendAndAddItemToArray(ref regularArray, behaviour, ref regularUpdateArrayCount);
        }

        if (!CheckIfArrayContainsItem(fixedArray, behaviour) && behaviour is IFixedUpdatable)
        {
            ExtendAndAddItemToArray(ref fixedArray, behaviour, ref fixedUpdateArrayCount);
        }

        if (!CheckIfArrayContainsItem(lateArray, behaviour) && behaviour is ILateUpdatable)
        {
            ExtendAndAddItemToArray(ref lateArray, behaviour, ref lateUpdateArrayCount);
        }


        //Old method using reflections

        /*
         * if (!CheckIfArrayContainsItem(regularArray, behaviour) && behaviour.GetType().GetMethod("UpdateMe").DeclaringType != typeof(OverridableMonoBehaviour))
         * {
         *  ExtendAndAddItemToArray(ref regularArray, behaviour, ref regularUpdateArrayCount);
         * }
         *
         * if (!CheckIfArrayContainsItem(fixedArray, behaviour) && behaviour.GetType().GetMethod("FixedUpdateMe").DeclaringType != typeof(OverridableMonoBehaviour))
         * {
         *  ExtendAndAddItemToArray(ref fixedArray, behaviour, ref fixedUpdateArrayCount);
         * }
         *
         * if (!CheckIfArrayContainsItem(lateArray, behaviour) && behaviour.GetType().GetMethod("LateUpdateMe").DeclaringType != typeof(OverridableMonoBehaviour))
         * {
         *  ExtendAndAddItemToArray(ref lateArray, behaviour, ref lateUpdateArrayCount);
         * }
         */


        //Some functions to help determine if your script is trying to subscribe without having Interfaces

        /*
         * if (!CheckIfArrayContainsItem(regularArray, behaviour) && behaviour.GetType().GetMethod("UpdateMe").DeclaringType != typeof(OverridableMonoBehaviour) && (behaviour is IUpdatable) == false)
         * {
         *  Debug.LogWarning(behaviour + "found exception");
         * }
         * if (!CheckIfArrayContainsItem(regularArray, behaviour) && behaviour.GetType().GetMethod("LateUpdateMe").DeclaringType != typeof(OverridableMonoBehaviour) && (behaviour is ILateUpdatable) == false)
         * {
         *  Debug.LogWarning(behaviour + "found exception");
         * }
         * if (!CheckIfArrayContainsItem(regularArray, behaviour) && behaviour.GetType().GetMethod("FixedUpdateMe").DeclaringType != typeof(OverridableMonoBehaviour) && (behaviour is IFixedUpdatable) == false)
         * {
         *  Debug.LogWarning(behaviour + "found exception");
         * }
         */
    }
Exemple #5
0
    public OverridableMonoBehaviour[] ExtendAndAddItemToArray(OverridableMonoBehaviour[] original, OverridableMonoBehaviour itemToAdd)
    {
        int size = original.Length;

        OverridableMonoBehaviour[] finalArray = new OverridableMonoBehaviour[size + 1];
        for (int i = 0; i < size; i++)
        {
            finalArray[i] = original[i];
        }
        finalArray[finalArray.Length - 1] = itemToAdd;
        return(finalArray);
    }
 private void AddItemToArray(OverridableMonoBehaviour behaviour)
 {
     if (array == null)
     {
         array = new OverridableMonoBehaviour[1];
     }
     else
     {
         System.Array.Resize(ref array, array.Length + 1);
     }
     array[array.Length - 1] = behaviour;
     count = array.Length;
 }
    //This is called to expand the array in case it got overflowed. Generates garbage and should be avoided by setting an adequate startArrayCount.
    private OverridableMonoBehaviour[] ExtendArraySize(OverridableMonoBehaviour[] original)
    {
        int size = original.Length;

        OverridableMonoBehaviour[] finalArray = new OverridableMonoBehaviour[size + expandAmountOnOverflow];

        for (int i = 0; i < size; i++)
        {
            finalArray[i] = original[i];
        }

        arraysWereChanged = true;
        return(finalArray);
    }
Exemple #8
0
    public bool CheckIfArrayContainsItem(OverridableMonoBehaviour[] arrayToCheck, OverridableMonoBehaviour objectToCheckFor)
    {
        int size = arrayToCheck.Length;

        for (int i = 0; i < size; i++)
        {
            if (objectToCheckFor == arrayToCheck[i])
            {
                return(true);
            }
        }

        return(false);
    }
Exemple #9
0
    public OverridableMonoBehaviour[] ShrinkAndRemoveItemToArray(OverridableMonoBehaviour[] original, OverridableMonoBehaviour itemToRemove)
    {
        int size = original.Length;

        OverridableMonoBehaviour[] finalArray = new OverridableMonoBehaviour[size - 1];
        for (int i = 0; i < size; i++)
        {
            if (original[i] == itemToRemove)
            {
                continue;
            }

            finalArray[i] = original[i];
        }
        return(finalArray);
    }
Exemple #10
0
    public OverridableMonoBehaviour[] ExtendAndAddItemToArray(OverridableMonoBehaviour[] original, OverridableMonoBehaviour itemToAdd, int originalCount)
    {
        int size = original.Length;

        if (size == originalCount)
        {
            OverridableMonoBehaviour[] finalArray = new OverridableMonoBehaviour[size * 2];
            for (int i = 0; i < size; i++)
            {
                finalArray[i] = original[i];
            }
            original = finalArray;
        }
        original[originalCount] = itemToAdd;
        return(original);
    }
Exemple #11
0
    private void AddItemToArray(OverridableMonoBehaviour behaviour)
    {
#if NETFX_CORE
        var type   = behaviour.GetType();
        var method = type.GetMethod("UpdateMe");
        if (method.DeclaringType != typeof(OverridableMonoBehaviour))
        {
            regularArray = ExtendAndAddItemToArray(regularArray, behaviour);
            regularUpdateArrayCount++;
        }


        if (behaviour.GetType().GetMethod("FixedUpdateMe").DeclaringType != typeof(OverridableMonoBehaviour))
        {
            fixedArray = ExtendAndAddItemToArray(fixedArray, behaviour);
            fixedUpdateArrayCount++;
        }

        if (behaviour.GetType().GetMethod("LateUpdateMe").DeclaringType == typeof(OverridableMonoBehaviour))
        {
            return;
        }

        lateArray = ExtendAndAddItemToArray(lateArray, behaviour);
        lateUpdateArrayCount++;
#else
        if (behaviour.GetType().GetMethod("UpdateMe").DeclaringType != typeof(OverridableMonoBehaviour))
        {
            regularArray = ExtendAndAddItemToArray(regularArray, behaviour);
            regularUpdateArrayCount++;
        }

        if (behaviour.GetType().GetMethod("FixedUpdateMe").DeclaringType != typeof(OverridableMonoBehaviour))
        {
            fixedArray = ExtendAndAddItemToArray(fixedArray, behaviour);
            fixedUpdateArrayCount++;
        }

        if (behaviour.GetType().GetMethod("LateUpdateMe").DeclaringType == typeof(OverridableMonoBehaviour))
        {
            return;
        }

        lateArray = ExtendAndAddItemToArray(lateArray, behaviour);
        lateUpdateArrayCount++;
#endif
    }
    private void ScheduleItemRemoval(OverridableMonoBehaviour behaviour)
    {
        if (CheckIfArrayContainsItem(regularArray, behaviour))
        {
            ShrinkAndRemoveItemFromArray(ref regularArray, behaviour, ref regularUpdateArrayCount, ref regularUpdateArrayCount);
        }

        if (CheckIfArrayContainsItem(fixedArray, behaviour))
        {
            ShrinkAndRemoveItemFromArray(ref fixedArray, behaviour, ref fixedUpdateArrayCount, ref fixedUpdateArrayCount);
        }

        if (CheckIfArrayContainsItem(lateArray, behaviour))
        {
            ShrinkAndRemoveItemFromArray(ref lateArray, behaviour, ref lateUpdateArrayCount, ref lateUpdateArrayCount);
        }
    }
Exemple #13
0
    private void RemoveSpecificItemFromArray(OverridableMonoBehaviour behaviour)
    {
        if (CheckIfArrayContainsItem(regularArray, behaviour))
        {
            regularArray = ShrinkAndRemoveItemToArray(regularArray, behaviour);
            regularUpdateArrayCount--;
        }

        if (CheckIfArrayContainsItem(fixedArray, behaviour))
        {
            fixedArray = ShrinkAndRemoveItemToArray(fixedArray, behaviour);
            fixedUpdateArrayCount--;
        }

        if (!CheckIfArrayContainsItem(lateArray, behaviour))
        {
            return;
        }

        lateArray = ShrinkAndRemoveItemToArray(lateArray, behaviour);
        lateUpdateArrayCount--;
    }
    private void ExtendAndAddItemToArray(ref OverridableMonoBehaviour[] original, OverridableMonoBehaviour itemToAdd, ref int amount)
    {
        int size = original.Length;

        bool wasFound = false;

        //Fit our behavour in some empty space.
        for (int i = 0; i < size; i++)
        {
            if (original[i] == null)
            {
                original[i]       = itemToAdd;
                arraysWereChanged = true;
                amount++;
                wasFound = true;
                break; //Return produces a stackoverflow exception here for some reason, so we're doing a bool check instead.
            }
        }

        //There was no empty space. Overflow!
        if (!wasFound)
        {
            regularArray = ExtendArraySize(regularArray);
            fixedArray   = ExtendArraySize(fixedArray);
            lateArray    = ExtendArraySize(lateArray);

            for (int i = 0; i < size; i++)
            {
                if (original[i] == null)
                {
                    original[i]       = itemToAdd;
                    arraysWereChanged = true;
                    amount++;
                    wasFound = true;
                    break; //Return produces a stackoverflow exception here for some reason, so we're doing a bool check instead.
                }
            }
        }
    }
Exemple #15
0
    private void AddItemToArray(OverridableMonoBehaviour behaviour)
    {
        if (behaviour.GetType().GetMethod("UpdateMe").DeclaringType != typeof(OverridableMonoBehaviour))
        {
            regularArray = ExtendAndAddItemToArray(regularArray, behaviour);
            regularUpdateArrayCount++;
        }

        if (behaviour.GetType().GetMethod("FixedUpdateMe").DeclaringType != typeof(OverridableMonoBehaviour))
        {
            fixedArray = ExtendAndAddItemToArray(fixedArray, behaviour);
            fixedUpdateArrayCount++;
        }

        if (behaviour.GetType().GetMethod("LateUpdateMe").DeclaringType == typeof(OverridableMonoBehaviour))
        {
            return;
        }

        lateArray = ExtendAndAddItemToArray(lateArray, behaviour);
        lateUpdateArrayCount++;
    }
Exemple #16
0
    public void RemoveSpecificItemAndDestroyIt(OverridableMonoBehaviour behaviour)
    {
        RemoveSpecificItemFromArray(behaviour);

        Destroy(behaviour.gameObject);
    }
Exemple #17
0
 public void RemoveSpecificItem(OverridableMonoBehaviour behaviour)
 {
     RemoveSpecificItemFromArray(behaviour);
 }
Exemple #18
0
 public void AddItem(OverridableMonoBehaviour behaviour)
 {
     AddItemToArray(behaviour);
 }
    private void ShrinkAndRemoveItemFromArray(ref OverridableMonoBehaviour[] original, OverridableMonoBehaviour itemToRemove, ref int amount, ref int arraySize)
    {
        int size = arraySize;

        if (size == 0)
        {
            size = 1;
        }

        int virtI = 0;

        if (original.Length - 1 != 0)
        {
            for (int i = 0; i < size; i++)
            {
                if (original[i] == itemToRemove)
                {
                    virtI++;
                }
                else
                {
                    original[i - virtI] = original[i];
                }
            }
        }

        amount--;
        arraysWereChanged = true;
    }
Exemple #20
0
 public static void RemoveSpecificItem(OverridableMonoBehaviour behaviour)
 {
     instance.RemoveSpecificItemFromArray(behaviour);
 }
Exemple #21
0
 public static void AddItem(OverridableMonoBehaviour behaviour)
 {
     instance.AddItemToArray(behaviour);
 }
Exemple #22
0
    public OverridableMonoBehaviour[] ShrinkAndRemoveItemToArray(OverridableMonoBehaviour[] original, int originalCount, OverridableMonoBehaviour itemToRemove)
    {
        int size = original.Length;

        for (int i = 0; i < size; i++)
        {
            if (original[i] == itemToRemove)
            {
                original[i] = original[originalCount - 1];
            }
        }
        return(original);
    }