npgdev

ScriptableObjectsAsDatabase

Nov 26th, 2020
496
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4.  
  5. public class CreateItemData
  6. {
  7.     [MenuItem("Assets/Create/Item Data")]
  8.     public static void MakeItemData()
  9.     {
  10.         string name = "ItemData";
  11.         ItemData asset = ScriptableObject.CreateInstance<ItemData>();
  12.         AssetDatabase.CreateAsset(asset, string.Format("Assets/ScriptableObjects/{0}.asset", name));
  13.         AssetDatabase.SaveAssets();
  14.         EditorUtility.FocusProjectWindow();
  15.         Selection.activeObject = asset;
  16.     }
  17. }
  18.  
  19.  
  20. using UnityEngine;
  21.  
  22. [System.Serializable]
  23. public class Item
  24. {
  25.     public string Name;
  26.     public ItemType ItemType;
  27.     public Sprite Sprite;
  28.     public GameObject Prefab;
  29.     public int level;
  30.  
  31.     public float Damage
  32.     {
  33.         get
  34.         {
  35.             return level * 5;
  36.         }
  37.     }
  38. }
  39.  
  40. using System.Collections;
  41. using System.Collections.Generic;
  42. using UnityEngine;
  43.  
  44. public class ItemGenerator : MonoBehaviour
  45. {
  46.     public ItemData itemData;
  47.     public Transform[] positions;
  48.  
  49.     private void Start()
  50.     {
  51.         for (int i = 0; i < positions.Length; i++)
  52.         {
  53.             GameObject item = Instantiate(itemData.ItemOfType((ItemType) i).Prefab, positions[i]);
  54.             item.transform.localPosition = Vector3.zero;
  55.         }
  56.     }
  57. }
  58.  
  59. using System.Collections;
  60. using System.Collections.Generic;
  61. using UnityEngine;
  62. using UnityEngine.UI;
  63.  
  64. public class ItemsUIDisplayController : MonoBehaviour
  65. {
  66.     public ItemData itemData;
  67.     public Image[] itemDisplays;
  68.  
  69.     private void Start()
  70.     {
  71.         for (int i = 0; i < itemDisplays.Length; i++)
  72.         {
  73.             itemDisplays[i].sprite = itemData.ItemOfType((ItemType) i).Sprite;
  74.         }
  75.     }
  76. }
  77.  
  78.  
  79.  
RAW Paste Data