| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEngine; |
| | 3 | | using Type = DCL.NotificationModel.Type; |
| | 4 | |
|
| | 5 | | public class NotificationFactory : ScriptableObject |
| | 6 | | { |
| | 7 | | [System.Serializable] |
| | 8 | | public class Item |
| | 9 | | { |
| | 10 | | public Type type; |
| | 11 | | public Notification prefab; |
| | 12 | | } |
| | 13 | |
|
| | 14 | | public Item[] factoryList; |
| | 15 | |
|
| | 16 | | private Dictionary<Type, Item> factoryDict; |
| | 17 | |
|
| | 18 | | public void EnsueFactoryDictionary() |
| | 19 | | { |
| 4 | 20 | | if (factoryDict == null) |
| | 21 | | { |
| 1 | 22 | | factoryDict = new Dictionary<Type, Item>(); |
| | 23 | |
|
| 20 | 24 | | for (int i = 0; i < factoryList.Length; i++) |
| | 25 | | { |
| 9 | 26 | | Item item = factoryList[i]; |
| | 27 | |
|
| 9 | 28 | | if (!factoryDict.ContainsKey(item.type)) |
| | 29 | | { |
| 9 | 30 | | factoryDict.Add(item.type, item); |
| | 31 | | } |
| | 32 | | } |
| | 33 | | } |
| 4 | 34 | | } |
| | 35 | |
|
| | 36 | | public Notification CreateNotificationFromType(Type type, Transform parent = null) |
| | 37 | | { |
| 4 | 38 | | EnsueFactoryDictionary(); |
| | 39 | |
|
| 4 | 40 | | if (!factoryDict.ContainsKey(type)) |
| | 41 | | { |
| | 42 | | #if UNITY_EDITOR |
| 0 | 43 | | Debug.LogError("Notification of type " + type + " can't be instantiated because it does not exist in the fac |
| | 44 | | #endif |
| 0 | 45 | | return null; |
| | 46 | | } |
| | 47 | |
|
| 4 | 48 | | if (factoryDict[type].prefab == null) |
| | 49 | | { |
| 0 | 50 | | Debug.LogError("Prefab for type " + type + " is null!"); |
| 0 | 51 | | return null; |
| | 52 | | } |
| | 53 | |
|
| 4 | 54 | | return parent == null ? Instantiate(factoryDict[type].prefab) : Instantiate(factoryDict[type].prefab, parent); |
| | 55 | | } |
| | 56 | | } |