유니티3D 프로그래밍
C# 3주차 3일 수업 내용 : 이벤트 및 File (21.03.24) 본문
대리자, 람다, 이벤트, LinQ 복습
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Study01
{
public delegate void GoHome();
public delegate void Say(string message);
class CarDriver
{
public static void GoLeft()
{
Console.WriteLine("좌회전");
}
public static void GoRight()
{
Console.WriteLine("우회전");
}
public static void GoForward()
{
Console.WriteLine("직진");
}
}
public class Mic
{
public int Id { get; set; }
//생성자
public Mic(int Id)
{
this.Id = Id;
}
public void Speak(string message)
{
string str1 = message.Substring(0, 2);
string str2 = message.Substring(2);
if (this.Id > 50)
{
Console.WriteLine(message);
}
else
{
Console.WriteLine("{0}...{1}..", str1, str2); ;
}
}
}
public class App
{
public App()
{
Console.WriteLine("App");
GoHome go = new GoHome(CarDriver.GoLeft);
go += new GoHome(CarDriver.GoRight);
go += new GoHome(CarDriver.GoForward);
go += new GoHome(CarDriver.GoRight);
go();
Random rand = new Random();
int num = rand.Next(1, 101);
int num2 = rand.Next(1, 101);
Mic mic1 = new Mic(num);
Mic mic2 = new Mic(num2);
Say say = new Say(mic1.Speak);
say += new Say(mic2.Speak);
say("안녕하세요");
}
}
}
로고 출력
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Study01
{
//대리자 선언
public delegate void DelPrint(string msg);
public delegate string DelPrintWithLogo(string logo, string msg);
//프린터기
public class Printer
{
//생성자
public Printer()
{
}
public void Print(string msg)
{
Console.WriteLine(msg);
}
public string PrintWithLogo(string logo, string message)
{
return logo + "\n" + message;
}
}
public class App
{
public App()
{
Console.WriteLine("App");
string message = "동해물과 백두산이...";
//대리자 초기화
Printer printer = new Printer();
DelPrint delPrint1 = new DelPrint(printer.Print);
delPrint1(message);
//무명 메서드
DelPrint delPrint2 = delegate (string msg)
{
Console.WriteLine("**** 무명 메서드 ****");
Console.WriteLine(msg);
};
//대리자 호출
delPrint2(message);
//람다식
DelPrint delPrint3 = (msg) =>
{
Console.WriteLine("**** 람다식 ****");
Console.WriteLine(msg);
};
delPrint3(message);
DelPrintWithLogo delPrint4 = new DelPrintWithLogo(printer.PrintWithLogo);
string paper = delPrint4("@logo", "쥬라기 공원에 오신걸 환영합니다.");
Console.WriteLine(paper);
//람다식
DelPrintWithLogo delPrint5 = (logo, msg) => string.Format("{0}\n{1}", logo, msg);
string paper2 = delPrint5(@"MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNXKXWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWXko:'..:KMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNkc. ,KMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNx, cNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMW0: .OMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWNx. .xWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM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MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM", "쥬라기 공원 입니다.");
Console.WriteLine(paper2);
}
}
}
RCCar 이동
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Study01
{
//RCCar
public class RCCar
{
public enum eDirection
{
FORWARD, BACKWARD, LEFT, RIGHT
}
public void Move(eDirection dir)
{
Console.WriteLine("{0}로 이동합니다.", dir);
}
}
public class RemoteController
{
public delegate void DelMove(RCCar.eDirection dir);
private RCCar car;
public DelMove delMove;
public RemoteController()
{
}
public void Connect(RCCar car)
{
this.car = car;
this.delMove = new DelMove(this.car.Move);
Console.WriteLine("{0}와 연결되었습니다.", this.car);
}
public void Controll(RCCar.eDirection eDir)
{
this.delMove(eDir);
}
}
public class App
{
public App()
{
Console.WriteLine("App");
RCCar car = new RCCar();
RemoteController controller = new RemoteController();
controller.Connect(car);
controller.Controll(RCCar.eDirection.FORWARD);
controller.Controll(RCCar.eDirection.LEFT);
controller.Controll(RCCar.eDirection.FORWARD);
controller.Controll(RCCar.eDirection.RIGHT);
controller.Controll(RCCar.eDirection.FORWARD);
controller.Controll(RCCar.eDirection.BACKWARD);
}
}
}
Drone 이동
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Study01
{
//RCCar
public class Drone
{
public enum eDirection
{
FLY, FORWARD, BACKWARD, LEFT, RIGHT, DOWN
}
public void Move(eDirection dir)
{
if(dir == eDirection.FLY)
{
Console.WriteLine("공중에 띄웁니다.");
}
else if(dir == eDirection.DOWN)
{
Console.WriteLine("지상으로 내려옵니다.");
}
else
{
Console.WriteLine("{0}로 이동합니다.", dir);
}
}
}
public class RemoteController
{
public delegate void DelMove(Drone.eDirection dir);
private Drone drone;
public DelMove delMove;
public RemoteController()
{
}
public void Connect(Drone drone)
{
this.drone = drone;
Console.WriteLine("{0}와 연결되었습니다.", this.drone);
this.delMove = new DelMove(this.drone.Move);
}
public void Controll(Drone.eDirection eDir)
{
if(eDir == Drone.eDirection.FLY)
{
Fly();
}
else if(eDir == Drone.eDirection.DOWN)
{
Down(eDir);
}
else
{
this.delMove(eDir);
}
}
public void Fly()
{
this.delMove(Drone.eDirection.FLY);
Console.WriteLine("비행을 시작합니다.");
}
public void Down(Drone.eDirection eDir)
{
this.delMove(Drone.eDirection.DOWN);
Console.WriteLine("비행을 종료합니다.");
}
}
public class App
{
public App()
{
Console.WriteLine("App");
Drone drone = new Drone();
RemoteController controller = new RemoteController();
controller.Connect(drone);
controller.Controll(Drone.eDirection.FLY);
controller.Controll(Drone.eDirection.FORWARD);
controller.Controll(Drone.eDirection.LEFT);
controller.Controll(Drone.eDirection.FORWARD);
controller.Controll(Drone.eDirection.RIGHT);
controller.Controll(Drone.eDirection.FORWARD);
controller.Controll(Drone.eDirection.BACKWARD);
controller.Controll(Drone.eDirection.DOWN);
}
}
}
CallBack
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Study01
{
public class Unit
{
public enum eUnitType
{
VALTURE,
TANK,
GOLIATH
}
public Unit(eUnitType type)
{
Console.WriteLine("{0}이 생성되었습니다.", type);
}
}
public class Factory
{
public Factory()
{
Console.WriteLine("팩토리가 생성되었습니다.");
}
public void CreateUnit(Unit.eUnitType type,Action<Unit> callback)
{
callback(new Unit(type));
}
}
public class App
{
public App()
{
Console.WriteLine("App");
Factory f = new Factory();
f.CreateUnit(Unit.eUnitType.VALTURE, (unit) =>
{
Console.WriteLine(unit); //Marine
});
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Study01
{
public class Player
{
Backpack backpack = new Backpack(0);
//생성자
public Player()
{
}
public void Init(Backpack backpack)
{
this.backpack = backpack;
Console.WriteLine("플레이어가 가방을 획득합니다.");
}
public void InitItem(Item item)
{
Console.WriteLine("플레이어가 아이템을 획득합니다.");
backpack.ItemGet(item);
}
public void ItemInfo()
{
backpack.ItemDataInfo();
}
}
public class Item
{
public ItemData Data { get; set; }
//생성자
public Item(ItemData Data)
{
this.Data = Data;
}
}
public class ItemData
{
public int id;
public string name;
//생성자
public ItemData(int id, string name)
{
this.id = id;
this.name = name;
}
}
public class Backpack
{
private List<Item> itemList = new List<Item>();
private int id;
//생성자
public Backpack(int id)
{
this.id = id;
}
public void ItemGet(Item item)
{
itemList.Add(item);
}
public void ItemDataInfo()
{
foreach(Item item in itemList)
{
Console.WriteLine("아이템 목록 : {0} {1}", item.Data.id, item.Data.name);
}
}
}
public class App
{
public App()
{
Console.WriteLine("App");
Func<int, Backpack> createBackpack = (id) =>
{
return new Backpack(id);
};
var backpack = createBackpack(100);
Console.WriteLine(backpack);
Player player = new Player();
player.Init(backpack);
ItemData data = new ItemData(100, "SCAR-L");
Item item = new Item(data);
player.InitItem(item);
Func<Item, ItemData> lookItem = (Data) => {
return item.Data;
};
ItemData itemData = lookItem(item);
player.ItemInfo();
}
}
}
Func 연습
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Study01
{
public class App
{
public App()
{
Console.WriteLine("App");
this.Print(this.GetLength, "안녕하세요");
this.Print(this.GetLength, "Hello World");
}
private void Print(Func<string, int> func, string msg)
{
//대리자 호출
int result = func(msg);
Console.WriteLine(result);
}
private int GetLength(string str)
{
return str.Length;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Study01
{
public class App
{
public App()
{
Console.WriteLine("App");
var result = this.Calc(3, 5, (x, y) => 3 + 4);
Console.WriteLine(result);
var result1 = this.Calc(3, 5, (x, y) => 3 * 4);
Console.WriteLine(result1);
}
public int Calc(int x, int y, Func<int, int, int> calc)
{
return calc(x, y);
}
}
}
Button Event
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Study01
{
public class Button
{
//이벤트 변수 선언
public event EventHandler onClick;
public void OnMouseDown()
{
//대리자 호출
this.onClick(this, EventArgs.Empty);
}
}
public class App
{
public App()
{
Console.WriteLine("App");
Button btn = new Button();
btn.onClick += OnClickEventHandler;
btn.onClick += (sender, e) =>
{
Console.WriteLine("[람다] Click : {0}, {1}", sender, e);
};
btn.OnMouseDown();
}
private void OnClickEventHandler(object sender, EventArgs e)
{
Console.WriteLine("Click : {0}, {1}", sender, e);
}
}
}
Drone 업그레이드
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Study01
{
public class Drone
{
public enum eDirection
{
FORWARD, BACK, LEFT, RIGHT, UP, DOWN
}
public int Id { get; private set; }
private DroneController controller;
public Drone(int id)
{
this.Id = id;
}
public void Init(DroneController controller)
{
this.controller = controller;
this.controller.onMove += OnMoveEventHandler;
}
private void OnMoveEventHandler(object sender, DroneEventArgs e)
{
if (e.Id == this.Id || e.Id == 999)
{
this.Move(e.Dir);
}
else
{
Console.WriteLine("no signal..., id: {0}", this.Id);
}
}
public void Move(eDirection dir)
{
Console.WriteLine("[MOVE] id: {0}, dir:{1}", this.Id, dir);
}
}
public class DroneEventArgs : EventArgs
{
public Drone.eDirection Dir { get; private set; }
public int Id { get; private set; }
public DroneEventArgs(int id, Drone.eDirection dir)
{
this.Id = id;
this.Dir = dir;
}
}
public class DroneController
{
public event EventHandler<DroneEventArgs> onMove;
//생성자
public DroneController()
{
}
public void Control(int id, Drone.eDirection dir)
{
DroneEventArgs args = new DroneEventArgs(id, dir);
this.onMove(this, args);
}
}
public class App
{
public App()
{
Console.WriteLine("App");
DroneController controller = new DroneController();
Drone drone1 = new Drone(100);
Drone drone2 = new Drone(200);
drone1.Init(controller);
drone2.Init(controller);
controller.Control(999, Drone.eDirection.UP);
}
}
}
File 불러오기
using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Study01
{
public class App
{
public App()
{
Console.WriteLine("App");
string txt = File.ReadAllText("./weapon_data.json");
Console.WriteLine(txt);
//역직렬화
WeaponData[] weaponDatas = JsonConvert.DeserializeObject<WeaponData[]>(txt);
Dictionary<int, WeaponData> dicWeaponData;
dicWeaponData = weaponDatas.ToDictionary(x => x.id);
Console.WriteLine("-----------------------------------");
foreach(KeyValuePair<int, WeaponData> pair in dicWeaponData)
{
WeaponData data = pair.Value;
Console.WriteLine("{0} {1} {2}", data.id, data.name, data.damage);
}
}
}
}
using System;
namespace Study01
{
class WeaponData
{
public int id;
public string name;
public int damage;
}
}
'C# > 수업내용' 카테고리의 다른 글
C# 3주차 4일 수업 내용 : JSON (21.03.25) (0) | 2021.03.25 |
---|---|
Json 파일 로딩 (0) | 2021.03.24 |
C# 3주차 2일 수업 내용 : LINQ 및 람다, EVENT(21.03.23) (0) | 2021.03.23 |
C# 3주차 1일 수업 내용 : Delegate, Callback (21.03.22) (0) | 2021.03.22 |
C# 2주차 4일 수업 내용 (21.03.18) (0) | 2021.03.18 |