Documentation for the Unity C# Library
Loading...
Searching...
No Matches
ItemIDsSettings.cs
Go to the documentation of this file.
1#if UNITY_EDITOR
2
3using System;
4using System.Collections.Generic;
5using PixoVR.Event;
6using UnityEditor;
7using UnityEngine;
8
9// Create a new type of Settings Asset.
10class ItemIDsSettings : ScriptableObject
11{
12 [Serializable]
13 public struct ItemID
14 {
15 public string name;
16 public int id;
17 }
18
19 public const string settingsPath = "Assets/Resources/ItemIDsSettings.asset";
20 public List<ItemID> itemIds;
21
22 internal static ItemIDsSettings GetOrCreateSettings()
23 {
24 var settings = AssetDatabase.LoadAssetAtPath<ItemIDsSettings>(settingsPath);
25
26 if (settings == null)
27 {
28 settings = CreateInstance<ItemIDsSettings>();
29 settings.itemIds = new List<ItemID>();
30 //TODO: remote itemID enum
31 var names = Enum.GetNames(typeof(ItemID));
32
33 for (int i = 0; i < names.Length; i++)
34 {
35 settings.itemIds.Add(new ItemID() {name = names[i], id = (int) Enum.Parse<int>(names[i])});
36 }
37
38 CheckOrCreateAssetsPath(settingsPath);
39 AssetDatabase.CreateAsset(settings, settingsPath);
40 AssetDatabase.SaveAssets();
41 }
42
43 return settings;
44 }
45
46 private static void CheckOrCreateAssetsPath(string path)
47 {
48 var splitedArray = path.Split('/');
49 string currentPath = splitedArray[0];
50
51 for (int i = 1; i < splitedArray.Length; i++)
52 {
53 if (!splitedArray[i].Contains("."))
54 {
55 if (!AssetDatabase.IsValidFolder(currentPath + "/" + splitedArray[i]))
56 {
57 AssetDatabase.CreateFolder(currentPath, splitedArray[i]);
58 }
59
60 currentPath += "/" + splitedArray[i];
61 }
62 }
63 }
64
65 internal static SerializedObject GetSerializedSettings()
66 {
67 return new SerializedObject(GetOrCreateSettings());
68 }
69}
70#endif