add Atomes Factory
This commit is contained in:
49
Assets/script/AtomeFactory.cs
Normal file
49
Assets/script/AtomeFactory.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
|
||||
public class AtomeFactory : MonoBehaviour{
|
||||
|
||||
public TextAsset jsonFile;
|
||||
|
||||
private static AtomeFactory instance;
|
||||
|
||||
private AtomesInformation atomeInJson;
|
||||
|
||||
private Dictionary<string, AtomeInformation> AtomeDictionary;
|
||||
|
||||
public AtomeFactory(TextAsset jsonFile){
|
||||
AtomeFactory.instance = this;
|
||||
AtomeDictionary = new Dictionary<string, AtomeInformation>();
|
||||
atomeInJson = JsonUtility.FromJson<AtomesInformation>(jsonFile.text);
|
||||
foreach (var atome in atomeInJson.atomes){
|
||||
AtomeDictionary.Add(atome.symbol, atome);
|
||||
}
|
||||
}
|
||||
|
||||
public static AtomeFactory getInstrance(){
|
||||
if(AtomeFactory.instance == null){
|
||||
Debug.LogError("no Json file");
|
||||
}
|
||||
return AtomeFactory.instance;
|
||||
}
|
||||
|
||||
public static AtomeFactory getInstrance(TextAsset jsonFile){
|
||||
if(AtomeFactory.instance == null){
|
||||
AtomeFactory.instance = new AtomeFactory(jsonFile);
|
||||
}
|
||||
return AtomeFactory.instance;
|
||||
}
|
||||
|
||||
public AtomeInformation createAtome(string symbol){
|
||||
if(!AtomeDictionary.ContainsKey(symbol)){
|
||||
Debug.LogError("Atome does not existe in json");
|
||||
}
|
||||
AtomeInformation atome = AtomeDictionary[symbol];
|
||||
|
||||
return atome;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user