Ejemplo n.º 1
0
 /// <summary>
 /// Constructs an object pool of specified type, and allows adjusting the flexibility of the pool
 /// </summary>
 /// <param name="size">A number of slots to preallocate.</param>
 /// <param name="grow">Whether or not the pool should dynamically add new instances.</param>
 /// <returns>An ObjectPool of specified type.</returns>
 public ObjectPool(int size, bool grow = false)
 {
     max            = grow ? -1 : size;
     actives        = new List <T>(size);
     deactives      = new List <T>(size);
     OnInstantiate  = () => (T)Activator.CreateInstance(typeof(T));
     OnInstantiated = t => { };
     OnSpawned      = t => { };
     OnWillSpawn    = t => { };
     OnDespawned    = t => { };
     OnWillDespawn  = t => { };
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Constructs an object pool of specified type, and allows adjusting the flexibility of the pool
 /// </summary>
 /// <param name="size">A number of slots to preallocate.</param>
 /// <param name="grow">Whether or not the pool should dynamically add new instances.</param>
 /// <returns>An ObjectPool of specified type.</returns>
 public GameObjectPool(Transform parent, GameObject prefab, int size, bool grow = false)
 {
     this.parent    = parent;
     this.prefab    = prefab;
     max            = grow ? -1 : size;
     actives        = new List <T>(size);
     deactives      = new List <T>(size);
     OnInstantiated = t => { };
     OnSpawned      = t => { };
     OnWillSpawn    = t => { };
     OnDespawned    = t => { };
     OnWillDespawn  = t => { };
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            // 基本
            SampleDelegate a = new SampleDelegate(AddOne);

            // 暗黙の型変換も可能
            SampleDelegate s = SubtractOne;

            // インスタンスメソッドも代入可能
            var sampleClass    = new SampleClass();
            InstanceDelegate i = sampleClass.Show;

            // 複数のメソッドを代入可能(マルチキャストデリゲート)
            InstanceDelegate m = sampleClass.Show;

            m += sampleClass.Show;


            // 匿名関数を代入可能
            AnonymousDelegate an = delegate(int param1, int param2)
            {
                Console.WriteLine("Called Anonymouse Function!!!");
                Console.WriteLine($"result: {param1 + param2}");
                Console.WriteLine();
            };

            // ラムダ式でも書ける
            an += (int param1, int param2) =>
            {
                Console.WriteLine("Called Lambda Expression!!!");
                Console.WriteLine($"result: {param1 + param2}");
                Console.WriteLine();
            };

            // もっと簡単なラムダ式
            an += (param1, param2) =>
            {
                Console.WriteLine("Called Simple Lambda Expression!!!");
                Console.WriteLine($"result: {param1 + param2}");
                Console.WriteLine();
            };

            // ラムダ式の中身が一行ならさらに簡単に書ける
            an += (param1, param2) => Console.WriteLine("Called More Simple Lambda Expression!!!");


            // デリゲートを介してメソッドを呼び出す
            a(123);
            s(100);
            i("test");
            m("multi");
            an(5, 6);


            /* デリゲートにはすでに用意してある型がある(汎用デリゲート)
             * Actionは、任意個の引数を取り、かつ、戻り値のないメソッドへのDelegate
             * Funcは、任意個の引数を取り、かつ、戻り値のあるメソッドへのDelegate
             * Predicateは、任意個の引数を取り、かつ、戻り値の型がBooleanであるようなメソッドへのDelegate
             * ほかにもConvertorやComparisonがある
             */


            // 引数なし、戻り値あり
            Func <int> func_01 = () =>
            {
                Console.WriteLine("Called Func<TResult> !!!");
                Console.WriteLine();
                return(0);
            };

            // 引数1つ、戻り値あり
            Func <int, string> func_02 = tt =>
            {
                var cal = tt + 2;
                Console.WriteLine("Called Func<T, TResult> !!!");
                Console.WriteLine($"result: {cal}");
                Console.WriteLine();
                return(cal.ToString());
            };

            // 引数2つ、戻り値あり
            Func <int, int, string> func_03 = (aa, bb) =>
            {
                Console.WriteLine("Called Func<T1, T2, TResult> !!!");
                Console.WriteLine($"result: {aa + bb}");
                Console.WriteLine();
                return((aa + bb).ToString());
            };

            // 引数なし、戻り値なし
            Action action_01 = () => Console.WriteLine("Called Action !!!");

            // 引数1つ、戻り値なし
            Action <string> action_02 = str =>
            {
                Console.WriteLine("Called Action<T> !!!");
                Console.WriteLine($"param: {str}");
                Console.WriteLine();
            };

            // 引数2つ、戻り値なし
            Action <string, string> action_03 = (param1, param2) =>
            {
                Console.WriteLine("Called Action<T1, T2> !!!");
                Console.WriteLine($"param: {param1}, {param2}");
                Console.WriteLine();
            };

            // 引数1つ、戻り値Bool
            Predicate <int> predicate_01 = param =>
            {
                Console.WriteLine("Called Predicate<T> !!!");
                Console.WriteLine($"result: {param > 5}");
                return(param > 5);
            };


            // 汎用メソッドを呼び出す
            func_01();
            func_02(8);
            func_03(56, 44);

            action_01();
            action_02("test");
            action_03("test01", "test02");

            predicate_01(9);


            // Linqをいろいろな呼び方をする
            var list = new List <string> {
                "aaa", "bbb", "ccc", "ddd", "eee", "fff"
            };

            var whereResult = list.Where(w => w == "ddd");

            whereResult = list.Where((w) => { return(w == "ccc"); });
            whereResult = list.Where(sampleClass.LinqWhereFunction).ToList();

            var findResult = list.FindAll(x => x == "fff");

            findResult = list.FindAll(sampleClass.LinqFindFunction);

            // イベントの登録で使用する
            var eventClass = new EventClass((sender, eventArgs) => Console.WriteLine(eventArgs.Message));

            // 冗長な書き方
            //var eventClass = new EventClass(new EventHandler<MyEventArgs>((sender, eventArgs) => Console.WriteLine(eventArgs.Message)));

            // 非同期実行
            Task.Run(() => eventClass.Start()).Wait();

            // ラムダ式でかきたくない場合はキャストすれば使用可能
            //Task.Run((Action)eventClass.Start).Wait();

            Console.WriteLine("キーを押して終了。");
            Console.ReadKey();
        }