Skip to content

Instantly share code, notes, and snippets.

@IJEMIN
Last active July 2, 2020 15:23
Show Gist options
  • Save IJEMIN/ca8425e42d9fbc6d0e910ee2db8851bd to your computer and use it in GitHub Desktop.
Save IJEMIN/ca8425e42d9fbc6d0e910ee2db8851bd to your computer and use it in GitHub Desktop.
목업 데이터를 모아두는 스크립터블 오브젝트 예제
[System.Serializable]
public class ItemSampleData
{
public string DisplayName;
public string Description;
public string Id;
public int Cost;
}
using System;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
using Random = UnityEngine.Random;
#if UNITY_EDITOR
using UnityEditor;
#endif
#if UNITY_EDITOR
[CustomEditor(typeof(ItemSampleDataProvider))]
public class ItemSampleDataProviderEditor : Editor
{
[MenuItem("Assets/UI Mock-up Data...")]
public static void OpenInspector()
{
Selection.activeObject = ItemSampleDataProvider.Instance;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Add Random Data"))
{
(target as ItemSampleDataProvider).AppendNewRandomData();
}
}
}
#endif
public class ItemSampleDataProvider : ScriptableObject
{
#region SINGLETON
private const string SettingFileDirectory = "Assets/Resources";
private const string SettingFilePath = "Assets/Resources/ItemSampleData.asset";
private static ItemSampleDataProvider _instance;
public static ItemSampleDataProvider Instance
{
get
{
if (_instance != null) return _instance;
_instance = Resources.Load<ItemSampleDataProvider>("ItemSampleData");
#if UNITY_EDITOR
if (_instance == null)
{
if (!AssetDatabase.IsValidFolder(SettingFileDirectory))
{
AssetDatabase.CreateFolder("Assets", "Resources");
}
_instance = (ItemSampleDataProvider) AssetDatabase.LoadAssetAtPath(SettingFilePath,
typeof(ItemSampleDataProvider));
if (_instance == null)
{
_instance = CreateInstance<ItemSampleDataProvider>();
AssetDatabase.CreateAsset(_instance, SettingFilePath);
}
}
#endif
return _instance;
}
}
#endregion
[SerializeField] private List<ItemSampleData> _sampleDatas = new List<ItemSampleData>();
public ItemSampleData GetRandomItemSampleData()
{
return _sampleDatas[Random.Range(0, _sampleDatas.Count)];
}
public ItemSampleData GetItemSampleData(string id)
{
return _sampleDatas.FirstOrDefault(item => item.Id == id);
}
public ItemSampleData[] GetAllItemSampleData()
{
return _sampleDatas.ToArray();
}
public void AppendNewRandomData()
{
var newSampleDataTask = GenerateRandomData(10);
newSampleDataTask.ContinueWith(
result => _sampleDatas.AddRange(result));
}
private async UniTask<ItemSampleData[]> GenerateRandomData(int count)
{
var webReq = UnityWebRequest.Get($"http://names.drycodes.com/{count * 2}");
await webReq.SendWebRequest().ToUniTask();
var randomNames = JsonConvert.DeserializeObject<List<string>>(webReq.downloadHandler.text).GetEnumerator();
var itemList = new List<ItemSampleData>();
for (var i = 0; i < count; i++)
{
randomNames.MoveNext();
var title = randomNames.Current;
randomNames.MoveNext();
var description = randomNames.Current;
itemList.Add(
new ItemSampleData
{
DisplayName = title,
Description = description,
Id = Guid.NewGuid().ToString(),
Cost = Random.Range(1000, 3000)
}
);
}
return itemList.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment