Exemplo n.º 1
0
    //Saves all Human NPC's
    private void saveNPCData()
    {
        BinaryFormatter bf   = new BinaryFormatter();
        FileStream      file = File.Create(Application.persistentDataPath + "/npcData.dat");

        //Gets all Humans with Human class component in scene
        npcList = FindObjectsOfType(typeof(Human)) as Human[];
        //Data container for all humans
        npcSaveStruct toSave = new npcSaveStruct();

        //Saves each human and adds to toSave container
        foreach (Human npc in npcList)
        {
            HumanSave save = npc.saveHumanData();
            toSave.allNPCInGame.Add(save);
            toSave.numberOfNPCS++;
        }

        bf.Serialize(file, toSave);
        file.Close();
    }
Exemplo n.º 2
0
    //Loads all human NPC's
    private void loadNPCData()
    {
        if (File.Exists(Application.persistentDataPath + "/npcData.dat"))
        {
            BinaryFormatter bf   = new BinaryFormatter();
            FileStream      file = File.Open(Application.persistentDataPath + "/npcData.dat", FileMode.Open);

            //Container for all data to load, gets contents from file
            npcSaveStruct toLoad = new npcSaveStruct();
            try
            {
                toLoad = bf.Deserialize(file) as npcSaveStruct;
            }
            catch (EndOfStreamException e)
            {
                Debug.Log("Failed Load");
                return;
            }
            file.Close();

            //npcList = new Human[toLoad.numberOfNPCS];
            //Loads each Human
            foreach (HumanSave npc in toLoad.allNPCInGame)
            {
                //Gets prefab from resources folder and instantiates it with this gameObject as its parent
                GameObject defaultNPC = Resources.Load("prefabs/defaultNPC") as GameObject;
                GameObject newHuman   = Instantiate(defaultNPC, gameObject.transform);

                //NEXT LINE MIGHT BE REDUNDANT, double checks parent is this gameobject
                newHuman.transform.parent = gameObject.transform;
                //Adds a human script to the gameobject and creates a reference which can be used to load the HUMAN
                Human human = newHuman.AddComponent <Human>();
                human.loadHuman(npc);
            }
        }
    }