using System.Collections.Generic; public enum ItemType { HealthPotion, AmmoPack, Shield, } public enum AmmoType { PistolAmmo, ShotgunShells, RifleAmmo, } public class GameManager : MonoBehaviour { // アイテムのデータベース private Dictionary<ItemType, int> itemInventory = new (); // 弾薬のデータベース private Dictionary<AmmoType, int> ammoInventory = new (); private void Start() { // 初期アイテムと弾薬の設定 itemInventory[ItemType.HealthPotion] = 3; itemInventory[ItemType.AmmoPack] = 5; itemInventory[ItemType.Shield] = 1; ammoInventory[AmmoType.PistolAmmo] = 50; ammoInventory[AmmoType.ShotgunShells] = 20; ammoInventory[AmmoType.RifleAmmo] = 100; // アイテムの使用例 UseItem(ItemType.HealthPotion); UseItem(ItemType.AmmoPack); // 弾薬の使用例 UseAmmo(AmmoType.PistolAmmo, 10); UseAmmo(AmmoType.ShotgunShells, 5); } // アイテムを使用するメソッド private void UseItem(ItemType itemType) { if (itemInventory.ContainsKey(itemType) && itemInventory[itemType] > 0) { // アイテムを使用する処理をここに追加 Debug.Log($"アイテムを使用 : { itemType }"); itemInventory[itemType]--; } else { Debug.Log($"アイテムが不足しています : { itemType }"); } } // 弾薬を使用するメソッド private void UseAmmo(AmmoType ammoType, int amount) { if (ammoInventory.ContainsKey(ammoType) && ammoInventory[ammoType] >= amount) { // 弾薬を使用する処理をここに追加 Debug.Log($"弾薬を使用 : { ammoType } (数量 : { amount })"); ammoInventory[ammoType] -= amount; } else { Debug.Log($"弾薬が不足しています : { ammoType }"); } } }
using System.Collections.Generic; public class ScoreManager : MonoBehaviour { private Dictionary<string, int> playerScores = new (); private void Start() { // プレイヤーのスコアの初期化 playerScores["Player1"] = 0; playerScores["Player2"] = 0; } // スコアを増やすメソッド public void IncreaseScore(string playerName, int points) { if (playerScores.ContainsKey(playerName)) { playerScores[playerName] += points; } else { Debug.Log($"プレイヤーが存在しません : { playerName }"); } } }
using System.Collections.Generic; public enum ItemType { HealthPotion, AmmoPack, Shield, } public class EnemyDropManager : MonoBehaviour { private Dictionary<ItemType, float> dropTable = new (); private void Start() { // アイテムのドロップ確率の設定 dropTable[ItemType.HealthPotion] = 0.3f; dropTable[ItemType.AmmoPack] = 0.2f; dropTable[ItemType.Shield] = 0.1f; } // 敵がアイテムをドロップするメソッド public ItemType DropItem() { float randomValue = Random.value; foreach (var kvp in dropTable) { if (randomValue < kvp.Value) { return kvp.Key; } randomValue -= kvp.Value; } // ドロップアイテムがない場合 return ItemType.None; } }
using System.Collections.Generic; public enum QuestStatus { InProgress, Completed, Failed, } public class QuestManager : MonoBehaviour { private Dictionary<int, QuestStatus> questStatus = new (); private void Start() { // クエストの初期状態を設定 questStatus[1] = QuestStatus.InProgress; questStatus[2] = QuestStatus.InProgress; } // クエストの進行状況を更新するメソッド public void UpdateQuestStatus(int questID, QuestStatus status) { if (questStatus.ContainsKey(questID)) { questStatus[questID] = status; } else { Debug.Log($"クエストが存在しません : { questID }"); } } }
{ "players": { "player1": { "name": "Alice", "score": 100 }, "player2": { "name": "Bob", "score": 200 }, "player3": { "name": "Charlie", "score": 150 } } }
using System.Collections.Generic; using UnityEngine; public class PlayerInfo { public string name; public int score; } public class ServerDataReceiver : MonoBehaviour { // サーバーから送信されたデータを格納するDictionary private Dictionary<string, PlayerInfo> playerData = new (); // サーバーからのデータを受信する関数 public void ReceiveServerData(string jsonString) { // JSONデータをデシリアライズしてDictionaryに格納 Dictionary<string, Dictionary<string, object>> serverData = MiniJSON.Json.Deserialize(jsonString) as Dictionary<string, Dictionary<string, object>>; if (serverData != null && serverData.ContainsKey("players")) { Dictionary<string, object> players = serverData["players"]; foreach (var player in players) { string playerId = player.Key; Dictionary<string, object> playerInfoDict = player.Value as Dictionary<string, object>; // PlayerInfoオブジェクトを作成してDictionaryに追加 PlayerInfo playerInfo = new PlayerInfo { name = playerInfoDict["name"].ToString(), score = int.Parse(playerInfoDict["score"].ToString()) }; playerData[playerId] = playerInfo; } } } // クライアントでデータを利用する例 public void UsePlayerData() { if (playerData.ContainsKey("player1")) { PlayerInfo player1Info = playerData["player1"]; Debug.Log("Player1 Name: " + player1Info.name); Debug.Log("Player1 Score: " + player1Info.score); } } }
[SerializeField] private List<GameObject> gameObjectsList; public List<GameObject> gameObjectsList;
[System.Serializable] public class SerializableDictionary<TKey, TValue> { public List<TKey> keys; public List<TValue> values; }