Beispiel #1
0
        public Flora3()
        {
            Debug.Log("flora3 constructor ||||||||||||||||||||||||||||||||");
            //initializeFlora();
            flora2 = new Flora2();

            newPlante("reddik");
        }
Beispiel #2
0
        public Flora()
        {
            Debug.Log("---------------------Flora Model---------------->");

            planter       = new List <Plant>();
            planterHentet = new List <Plant>();


            ///_______________________________________________________________
            ///lager planteobjekt
            ///

            /*
             *          Plant dummyplante = new Plant("UmuligPlante");
             *          Plant hentetplante;
             *
             *          string p_json = JsonUtility.ToJson(dummyplante,true);
             *          Debug.Log("I Flora Constructor,dummyplante. Den serialiserte json:    '" + p_json + "'");
             *
             *          using (System.IO.StreamWriter File2 = new System.IO.StreamWriter(@"D:\Unity\HappyGardenUnityVS\HappyGarden_6\Assets\VS\HappyGardenConsoleVSU 2\HappyGardenConsoleVSU\HGData\floratest.txt"))
             *          File2.WriteLine(p_json);
             *
             *
             *           //////////////////////////////// å konvertere tilbake
             *          // jsontest = JsonUtility.FromJson<JsonTest>(json);
             *          //Debug.Log("jsontest:      " + jsontest.playerName);
             *
             *          hentetplante = JsonUtility.FromJson<Plant>(p_json);
             *          Debug.Log("hentetplante:      " + hentetplante.name_no);
             */



            /*
             *          //Eventually Load the list from file, but it some times have to be created
             *          //this way e.g:  planter.Add(new Plant("fantasiPlante1"));
             *          CreatePlantList();
             *
             *          /// Denne fungerer lagrer listeobjektet i json-streng, lagrer strengen til fil
             *          string plantlist = JsonConvert.SerializeObject(planter, Formatting.Indented);
             *          Debug.Log("plantelisten: > " + plantlist + " <");
             *
             *          using (System.IO.StreamWriter File2 = new System.IO.StreamWriter(@"D:\Unity\HappyGardenUnityVS\HappyGarden_6\Assets\VS\HappyGardenConsoleVSU 2\HappyGardenConsoleVSU\HGData\plantlist.txt"))
             *          File2.WriteLine(plantlist);
             *
             *          Debug.Log("ReadFromJSON: leser objektet fra JSON");
             *
             *          for (int i=0; i<planterHentet.Count; i++)
             *          {
             *              Debug.Log("Hentet fra fil: " + planterHentet[i].name_no);
             *          }
             */



            /*
             *          Debug.Log("---------lager flora2--------------------------------------------------------------------" );
             *          //Lager ny alternativ flora, flora2. Hensikt å finne plantene ved plantenavn
             *          //laget i flora3 slik: public Plant[] planter = { new Plant("fantasiPlante1"),...
             *
             *          Flora2 flora2 = new Flora2(); //lager en liste over Planteobjekter.
             *          Plant[] plantene = flora2.planter;
             *
             *          //serialiserer den nye flora2-listen
             *          string plantlist2 = JsonConvert.SerializeObject(plantene, Formatting.Indented);
             *          Debug.Log("plantelisten2: > " + plantlist2 + " <");
             *
             *          //Debug.Log("flora2['potet'].name_no:   "+               flora2["potet"].name_no);
             *
             *          using (System.IO.StreamWriter File2 = new System.IO.StreamWriter(@"D:\Unity\HappyGardenUnityVS\HappyGarden_6\Assets\VS\HappyGardenConsoleVSU 2\HappyGardenConsoleVSU\HGData\plantlist2.txt"))
             *              File2.WriteLine(plantlist2);
             */



            //  LESE FRA FIL

            string readplantlist = "";


            // using (StreamReader sr = new StreamReader(@"D:\Unity\HappyGardenUnityVS\HappyGarden_7\Assets\VS\HappyGardenConsoleVSU 2\HappyGardenConsoleVSU\HGData\plantlist2.txt"))
            using (StreamReader sr = new StreamReader(@"Assets\VS\HappyGardenConsoleVSU 2\HappyGardenConsoleVSU\HGData\plantlist2.txt"))

            {
                string line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    //Debug.Log("> "+line);
                    readplantlist += line;
                }
            }


            //Her gjøres string 'readplantlist' om til listeobjektet 'planterHentet2'.    List<Plant>()
            planterHentet2 = JsonConvert.DeserializeObject <List <Plant> >(readplantlist);


            Flora2 flora2 = new Flora2();      //lager en liste over Planteobjekter.

            Plant[] plantene = flora2.planter; //dette funker. henter verdier fra lokal tilordning

            //Debug.Log("etter  Plant[] plantene = flora2.planter; før utskrift av flora2['potet']");

            //Debug.Log("flora2['potet'] = " + flora2["potet"].name_no); //denne gav feilmelding fordi verdiene var feil f.x '.potet' i stedet for 'potet'



            //Debug.Log("Plant[] plantene = flora2.planter; Nå blir plantene initiert? plantene[8].name_no = " + plantene[8].name_no);
            //kan vi gå andre veien flora2.planter = planterHentet2

            //Flora2 flora2 = new Flora2(); //lager en liste over Planteobjekter.
            //flora2.Plant = planterHentet2;
            //Plant[] plantene = flora2.planter;



            for (int i = 0; i < planterHentet2.Count; i++)
            {
                //Debug.Log("Lokal-tilordning: " +plantene[i]+ ",  "+ plantene[i].name_no);
                //Debug.Log("Hentet fra fil: " + planterHentet2[i]+",  "+ planterHentet2[i].name_no);

                //HER legger vi inn verdiene fra fil til et tilgjengelig planteobjekt (Flora2[])
                plantene[i] = planterHentet2[i];
                //flora2
            }


            ///Debug.Log("flora2['potet'] = " + flora2["potet"].name_no);

            //foreach (string pnavn in flora2[])
            //    {

            //    }

            for (int i = 0; i < planterHentet2.Count; i++)
            {
                //Debug.Log("Plante"+i+": " + plantene[i].name_no);

                //Plant testplante = Flora2.Plant["kgh"];
            }
        }