Saturday, September 30, 2017

Unity tutorial: Optimize removes from List

 





 If you maintain long List of items in your code and you often need to remove some at arbitrary position within it, then you pay performance penalty as all items with higher index have to be copied one position left.

 This is how code for method List.RemoveAt() looks:

        public void RemoveAt(int index) {
            if ((uint)index >= (uint)_size) {
                ThrowHelper.ThrowArgumentOutOfRangeException();
            }
            Contract.EndContractBlock();
            _size--;
            if (index < _size) {
                Array.Copy(_items, index + 1, _items, index, _size - index);
            }
            _items[_size] = default(T);
            _version++;
        }

 If item you want to remove is not the last one, then Array.Copy() is called. If your list is long and you are removing items at beginning, then performance costs may be high. But, if you do not care about order of items, you can write simple extension method, that will swap item you want to remove with last item in list and then call RemoveAt() on it. As item to remove is now last one, no copying of array is needed. Here is code for FastRemoveAt method:

using System.Collections.Generic;
using UnityEngine;

public static class ListExtensions {

    public static void FastRemoveAt<T>(this IList<T> list, int index) {
        // fast remove swaps item to remove with last item and then removes it
        // it should avoid internal Array.Copy, but changes order of items in List

        int lastIndex = list.Count - 1;

        T tmp = list[index];
        list[index] = list[lastIndex];
        list[lastIndex] = tmp;

        list.RemoveAt(lastIndex);
    }
}

 For test I created list with 100k items. Then, in loop, I remove first item in it and also add new item to the end, so it keeps its length. This is test code:

        // test
        Debug.Log("=========================================");
        var startTime = Time.realtimeSinceStartup;

        var list = new List<int>();
        var val = 0;

        for (; val < 100000; val++) {
            list.Add(val);
        }

        for (int i = 0; i < 1000000; i++) {
            //list.RemoveAt(0);
            list.FastRemoveAt(0);
            list.Add(val++);
        }

        Debug.Log("Time taken = " + (Time.realtimeSinceStartup - startTime));
        Debug.Log("=========================================");

 With both methods RemoveAt() and FastRemoveAt() I got these results for various number of updates:


 As you can see, FastRemoveAt() is much faster then RemoveAt().





Sunday, September 17, 2017

Pirates! - the match 3 made it into Apple AppStore

 



 Our game Pirates! - the match 3 is finally finished and its iOS version just made it into Apple AppStore. You can get it here:


 Pirates! is classic match 3 game with many different tasks for players - dig treasures, open chests, fight enemies or fulfil other tasks you are given in each level. It also features lot of gems combinations to produce various gameplay effects. Graphics for game was made by Tomáš Kopecký and music was composed by Juraj Růžička from SoundRoseStudio.






Flat Jewels Match 3 at Yahoo! Japan

 


 On 11th September our game Flat Jewels Match 3 was published at Yahoo! Japan. Game was published by Worker Bee Inc. and you can play it on their site here.

 



Wednesday, July 12, 2017

Fruit Dating at Yahoo! Japan

 


 Our logical puzzle game Fruit Dating was published at Yahoo! Japan today. It was published by Worker Bee Inc. and you can play it here.


Friday, June 30, 2017

Shards available in Windows Store

 



 Shards - the brickbreaker is now available in Microsoft's Windows Store as UWP aplication for Windows 10 phones.

Get it on Windows 10

Thursday, June 29, 2017

Shards and Fruit Dating is available on Tizen Store

 


 In June I rewrote our older games Shards and Fruit Dating into Unity engine. Both are now published at Tizen Store and anyone with Tizen device can download and play them!


Shards


Shards at Tizen Store




Fruit Dating


Fruit Dating at Tizen Store




Wednesday, April 19, 2017

New games released: Elsa Jewels, JumpTuber and Brick Stacker

 





 It has been long since I last posted info about games I finished. So, here is list of games finished or released in first quarter of 2017.


Elsa Jewels


 Elsa Jewels was exclusively made for Famobi. It features popular Disney's characters - Anna and Elsa from Frozen. And yes, Olaf is there too!
 You can play the game here.




JumpTuber


 JumpTuber is game to promote local youtuber Jirka Kral. Jirka is running from left to right in his youtuber den, picking likes and hamburgers. He has to avoid ducks and other items thrown at him by other youtubers. Game was made for Gamee platform and you can play it here.




Brick Stacker


 Last game in this list is Brick Stacker. It is another game made for Gamee. You have to build as high tower of blocks as possible. Test your building skills here (it is the best to play it on some device with portrait screen).