본문 바로가기

프로그래밍/C#27

구글 RealTimeDataBase GetValue 개수와 크기 .GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { // 오류 처리 } else if (task.IsCompleted) { DataSnapshot snapshot = task.Result; // 데이터 처리 var cnt = snapshot.Children.Count(); var size = snapshot.GetRawJsonValue().Length; } }); 이런 방식으로 Child 의 개수와 사이즈를 알 수 있다 2023. 2. 24.
C# Generic return T public T GetGenericReturn(string num) { return (T)Convert.ChangeType(num, typeof(T)); } int n = GetGenericReturn("1"); string n = GetGenericReturn("1"); 한개의 함수로 그때 그때 다른 리절트를 얻는 2022. 10. 2.
버프 시스템 클래스 using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations; public struct BuffData { public Soul owner; public float lifeTime; public Action onEnd; public BuffData(Soul owner, float lifeTime, Action onEnd) { this.owner = owner; this.lifeTime = lifeTime; this.onEnd = onEnd; } } pub.. 2022. 5. 27.
ObscuredString / String 을 숫자로 변환 했을 때의 성능 using System.Diagnostics; using System.Numerics; using CodeStage.AntiCheat.ObscuredTypes; using UnityEngine; using Debug = UnityEngine.Debug; public class Test_Obscured : MonoBehaviour { private BigInteger m_Value1 = 100000; private BigInteger m_Value2 = 1000; private ObscuredString m_ObsValue1 = "100000"; private ObscuredString m_ObsValue2 = "1000"; private string m_StrValue1 = "100000"; privat.. 2022. 3. 4.
제트브레인 라이더(Rider) 코파일럿(Copilot) 사용법 https://github.com/github/copilot-docs/blob/main/docs/jetbrains/gettingstarted.md#getting-started-with-github-copilot-in-jetbrains GitHub - github/copilot-docs: Documentation for GitHub Copilot Documentation for GitHub Copilot. Contribute to github/copilot-docs development by creating an account on GitHub. github.com 2022. 2. 21.
C# 가상함수(Virtual)와 오버라이드(override) public class View_Base : MonoBehaviour { [SerializeField] protected Text m_Text_Title; virtual void Init() { } } public class View_Character : View_Base { override void Init() { m_Text_Title.text = "캐릭터"; } } public class View_Inventory : View_Base { override void Init() { m_Text_Title.text = "인벤토리"; } } 이런식으로 UI 베이스를 만들고 같은 형식의 캐릭창과 인벤창을 만든다 치면 베이스에서 공통된 것 (창 타이틀 이름이라던가 닫기 버 튼 같은 것들) 을 편리하게 관리할.. 2022. 1. 14.
C# Action<> 의 활용 using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class Test_ClassA { public void Run(string value) { Debug.Log($"Test_ClassA.Run : {value}"); } } public class Test_ClassB { public event Action m_Run; public void Run(string value) { m_Run?.Invoke(value); } } public class Test_Callback : MonoBehaviour { private Test_ClassA m_A = new Test_ClassA();.. 2021. 10. 15.
퀘스트 시스템 feat linq lookup using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public class QuestInfo { public int m_Type; } public class QuestData : MonoBehaviour { private Dictionary m_Dic = new Dictionary(); private void Awake() { m_Dic.Add("Kill_Monster", new QuestInfo() { m_Type = 0 }); m_Dic.Add("Kill_Monster_Daily", new QuestInfo() { m_Type = 0 }); m_Dic.A.. 2021. 9. 29.
KeyCode.BackQuote = = ` KeyCode.BackQuote ! 옆에 있는 ` 버튼 키 2021. 9. 27.
BigInteger x ratio or percent using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test_BigInteger : MonoBehaviour { public System.Numerics.BigInteger m_Damage = 1000000000000000; // 10000000000000000000 public float m_Ratio = 1.1f; // Start is called before the first frame update void Start() { CalcR(m_Damage, m_Ratio); CalcR(100, m_Ratio); CalcR(60000000, m_Ratio); CalcR(3422346435, m.. 2021. 9. 23.
.Net 4.0 https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and- https://docs.microsoft.com/ko-kr/dotnet/csharp/whats-new/csharp-7 C# 7.0의 새로운 기능 - C# 가이드 C# 언어 버전 7.0의 새로운 기능을 살펴봅니다. docs.microsoft.com 2021. 9. 14.
Tuple using System; void Start() { Tuple tmp = Tuple.Create("Hello", "World"); Debug.Log($"{tmp.Item1} {tmp.Item2}"); } 가끔 어떤 값들을 뭉쳐서 함수의 변수로 넘겨줘야 할 때가 있다. 근데 이게 자주 바뀔 수 있다거나 픽스된 상황이 아니라고 한다면 스트럭쳐나 클래스 로 묶어서 쓰다가 나중에 바꿔야 수도 있는데 그런 일로 클래스를 늘리는 일이 싫다면 단발성 tuple 을 사용해서 처리하는 것도 편리하다 2021. 9. 7.
무결성 랜덤 (Random) A C# Mersenne Twister class - CodeProject A C# Mersenne Twister class A pseudorandom number generator. www.codeproject.com C++ 에 Boost::Random 이 있다면 C# 은 MersenneTwister 가 있다...? namespace MersenneTwister; void Test() { int rnd = (new MT19937()).RandomRange(0, 10); } 2021. 3. 9.
C# foreach 중 삭제(Remove) feat Linq using System.Linq; Dictionary m_Dic = new Dictionary(); private void Start() { m_Dic.Add("test1", 1.0f); m_Dic.Add("test2", 2.0f); m_Dic.Add("test3", 3.0f); // error foreach (var tmp in m_Dic) if (tmp.Value < 2.0f) m_Dic.Remove(tmp.Key); // success foreach (var key in m_Dic.Keys.ToList()) if (m_Dic[key] < 2.0f) m_Dic.Remove(key); } 2021. 2. 2.
에서 필요한 정식 매개 변수 에 해당하는 제공된 인수가 없습니다 클래스 상속시 'class' 에서 필요한 정식 매개 변수 ~~ 에 해당하는 제공된 인수가 없습니다 경고문 떴을 때 class StageData { public StageData(int id) { } } class StageData_Normal : StageData { public StageData_Normal(int id) : base(id) { } } : base 를 추가해주면 됩니다 2020. 11. 20.
C# 내일까지 남은 초 구하기 public static float NowToTomorrowSec() { return (float)((DateTime.Now - DateTime.Parse("0:00:00 AM").AddDays(1)).TotalSeconds); // 혹은 return (float)DateTime.Today.AddDays(1).Subtract(DateTime.Now).TotalSeconds; } 백문이 불여일견 2020. 11. 13.
List 루프 돌며 삭제 for (int i = 0; i < List.Count;) { if (List[i] == 조건) { List.RemoveAt(i); continue; } else i++; } 2020. 11. 11.
Shuffle Array + Array([]) to List(List<>) public T[] ShuffleArray(T[] array, int seed) { System.Random prng = new System.Random(seed); for (int i = 0; i < array.Length - 1; i++) { int randomIndex = prng.Next(i, array.Length); T tempItem = array[randomIndex]; array[randomIndex] = array[i]; array[i] = tempItem; } return array; } void Test() { List val = new List(); val.Add(1); val.Add(2); val.Add(3); List tmp = new List(ShuffleArray(val.T.. 2020. 10. 7.
DateTime 내일 / 내일까지 남은 시간 // 내일 0시 DateTime.Today.AddDays(1); // 내일 이 시간 DateTime.Now.AddDays(1); // ex 내일 0시까지 남은 시간 DateTime.Today.AddDays(1).Subtract(DateTime.Now).ToString(@"hh\:mm\:ss") 2020. 9. 1.
C# long (Int64) to bits Convert.ToString(someInt64, 2); Convert.ToUInt64("101010", 2); 2016. 12. 5.