示例#1
0
文件: Item.cs 项目: Baensi/Assets
 public Item Create(GameObject gameObject, ItemResource resource, ItemSize size, int maxCount, ItemDescription description)
 {
     this.itemName=resource.files.itemName;
     this.gameObject=gameObject;
     this.resource=resource;
     this.size=size;
     this.maxCount=maxCount;
     this.count=1;
     this.description=description;
     return this;
 }
示例#2
0
        public SortedDictionary<string, Item> readItems(string fileName)
        {
            SortedDictionary<string, Item> result = new SortedDictionary<string, Item>();

                XmlDocument xDocument = new XmlDocument();
                XmlTextReader reader = new XmlTextReader(fileName);
                xDocument.Load(reader);
                XmlNodeList objectsList = xDocument.GetElementsByTagName("item");

                foreach (XmlElement item in objectsList) {

                    XmlElement  property    = (XmlElement)item.GetElementsByTagName("property")[0];
                    XmlElement  description = (XmlElement)item.GetElementsByTagName("description")[0];

                    string     name = item.GetAttribute("name");
                    string     gameObjectPath = item.GetAttribute("gameObject");
                    GameObject gameObject = Resources.Load<GameObject>(gameObjectPath);
                    int        id = Convert.ToInt32(item.GetAttribute("id"));

            #if UNITY_EDITOR
                    if (gameObject == null)
                            Debug.LogError("Не удалось найти префаб для объекта "+ name+", проверьте файл 'Assets/Resources/"+ gameObjectPath + "'!");
            #endif

                    string iconPath = description.GetAttribute("icon");
                    Texture2D  icon = Resources.Load<Texture2D>(iconPath);

            #if UNITY_EDITOR
                    if (icon == null)
                        Debug.LogError("Не удалось найти иконку для объекта " + name + ", проверьте файл 'Assets/Resources/" + iconPath + "'!");
            #endif

                    List<SoundPack> soundList = null;
                    XmlNodeList sounds = description.GetElementsByTagName("sound");
                    string soundPath = null;
                    string soundName = null;

                    List<string> soundsPaths = new List<string>();
                    List<string> soundsNames = new List<string>();
                    if (sounds.Count > 0) {
                        soundList = new List<SoundPack>();
                        foreach (XmlElement sound in sounds) {
                            soundPath = sound.GetAttribute("sound");

            #if UNITY_EDITOR
                            if (icon == null)
                                Debug.LogError("Не удалось найти звуковой файл для объекта " + name + ", проверьте файл 'Assets/Resources/" + soundPath + "'!");
            #endif
                            soundName = sound.GetAttribute("tag");
                            soundsPaths.Add(soundPath);
                            soundsNames.Add(soundName);
                            soundList.Add(new SoundPack(Resources.Load<AudioClip>(soundPath), soundName));
                        }
                    }

                    int width  = Convert.ToInt32(property.GetAttribute("width"));
                    int height = Convert.ToInt32(property.GetAttribute("height"));
                    int count  = Convert.ToInt32(property.GetAttribute("count"));

                    ItemResource resource = new ItemResource(icon, soundList);
                    ItemSize     size = new ItemSize(width,height);

                    resource.files.itemName = name;
                    resource.files.gameObjectPath = gameObjectPath;
                    resource.files.iconPath       = iconPath;
                    resource.files.soundsPaths = soundsPaths;
                    resource.files.soundsNames = soundsNames;

                    string textName    = description.GetAttribute("textName");
                    string textCaption = description.GetAttribute("textCaption");
                    float  costValue   = Convert.ToSingle(description.GetAttribute("costValue"));

                    ItemDescription itemDescription = new ItemDescription().Create(id,textName,textCaption,costValue);

                    result.Add(name, new Item().Create(gameObject, resource, size, count, itemDescription));
                }

                reader.Close();
                reader    = null;
                xDocument = null;

            return result;
        }