文章目录
物品管理器
-  
物品数据 Item.json(json部分)(Resources/Data/Item)
[{ "id": 1, "name": "新手剑", "des": "这是一把宝剑", "price": 200, "icon": "", "attack": 10, "hp": 0 }, { "id": 2, "name": "新手药剂", "des":"这是新手药剂", "price": 100, "icon": "", "attack" : 0, "hp": 10 }]-  
数据类
public class Item { public int id; public string name; public string des; public int price; public string icon; public int attack; public int hp; } 
 -  
 -  
物品管理类
using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; public class ItemManager { private static ItemManager instance; public static ItemManager Instance{ get { if(instance == null) { instance = new ItemManager(); } return instance; } } //所有的物品信息 private Item[] items; public ItemManager(){ TextAsset json = Resources.Load<TextAsset>("Data/Item"); //解析json items = LitJson.JsonMapper.ToObject<Item[]>(json.text); } //通过物品id获取物品 public Item GetItem(int id){ foreach (Item item in items){ if(item.id == id) { return item; } } return null; } } 
背包管理器
-  
InventoryManager
using System.Collections; using System.Collections.Generic; //背包格子 public class InventoryItem{ //物品id public int ItemId; //个数 public int Count = 1; } public class InventoryManager { //单例模式 private static InventoryManager instance; public static InventoryManager Instance{ get{ if(instance == null){ instance = new InventoryManager(); return instance; } return instance; } } //背包集合 public List<InventoryItem> Inventory = new List<InventoryItem>(); //添加物品 public void AddItem(int itemId, int count = 1){ //查看背包中是否已经存在该物品 foreach (InventoryItem tmpItem in Inventory){ if (tmpItem.ItemId == itemId){ //存在,就数量自增 tmpItem.Count += count; return; } } //背包中不存在该物品 InventoryItem item = new InventoryItem(); item.ItemId = itemId; item.Count = count; Inventory.Add(item); } //移除物品 public void Removeltem(int itemId, int count = 1){ //遍历背包 for (int i = 0; i < Inventory.Count; i++){ InventoryItem item = Inventory[i]; //如果存在该物品 if (item.ItemId == itemId && item.Count > 0){ //删除 item.Count -= count; if (item.Count <= 0){ Inventory.Remove(item); } } } } //获取物品 public InventoryItem GetItem(int itemId){ foreach (InventoryItem tmpItem in Inventory){ if (tmpItem.ItemId == itemId){ return tmpItem; } } return null; } //清空背包 public void RemoveAllItem() { Inventory.Clear(); } } 
角色管理器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//角色基本信息
public class Bai_Character{
    //姓名
    public string Name ="张三";
    //等级
    public int Level = 1;
    //经验
    public int CurrentExp = 0;
    public int Hp = 0;
    //最大血量
    public int MaxHp{
        get{
            return Level * 10;
            }   
    }
    //升到下一级需要的经验
    public int GetNextLevelExp(){
            return Level * 50;
    }
    //装备 -1代表无
    public int WeaponId = -1;
    public int ClothesId = -1;
    public int ShoesId = -1;
    //技能
    public List<int> SkillList = new List<int>();
}
public class CharacterManager 
{
    private static CharacterManager instance;
    public static CharacterManager Instance{
        get{
            if(instance == null){
                instance = new CharacterManager();
                return instance;
            }
            return instance;
        }
    }
    //玩家
    public Bai_Character character = new Bai_Character();
    //金钱
    public int Money = 0;
    //当前角色是否可控
    public bool canControl = true;
    //增加/减少金钱
    public void ChangeMoney(int money){
        Money += money;
        if (Money < 0) Money = 0;
    }
    //增加经验
    public void AddExp(int exp){
        character.CurrentExp += exp;
        //判断经验是否升级
        if (character.CurrentExp >= character.GetNextLevelExp()){
            //升级
            character.Level++;
            character.CurrentExp = 0;
        }
    }
    //改变血量
    public void ChangeHp(int hp){
        character.Hp += hp;
        //限制character.Hp的范围[0, character.MaxHp]
        character.Hp = Mathf.Clamp(character.Hp, 0, character.MaxHp);
    }
#region 任务技能
    //添加技能
    public bool AddSkill(int skillld){
        if(character.SkillList.Contains(skillld)){
            return false;
        }
        character.SkillList.Add(skillld);
        return true;
    }
    //是否拥有此技能
    public bool HasSkill(int skillld){
        return character.SkillList.Contains(skillld);
    }
    //移除技能
    public void RemoveSkill(int skillld){
        if (character.SkillList.Contains(skillld)){
            character.SkillList.Remove(skillld);
        }
    }
    //所有技能
    public int[] GetSkills(){
        return character.SkillList.ToArray();
    }
#endregion
#region 人物装备
    //装备武器
    public void AddWeapon(int id){
        //如果背包有这个武器,允许装备
        InventoryItem item = InventoryManager.Instance.GetItem(id);
        if (item!= null)
        {
            InventoryManager.Instance.Removeltem(id,1);
        }
        //如果当前有武器,武器卸载下来放入背包
        if (character.WeaponId > -1){
            InventoryManager.Instance.AddItem(character.WeaponId, 1);
        }
        character.WeaponId = id;
    }
    
    //获取当前武器
    public int GetWeapon(){
        return character.WeaponId;
    }
    //移除当前武器
    public void RemoveWeapon(){
        //如果当前有武器,武器卸载下来放入背包if (character.Weaponld > -1)
        InventoryManager.Instance.AddItem(character.WeaponId,1);
    }
#endregion
}








