public static AtomBehaviour instantiateAtom(Vector3 location, string type) { MoleculeBehaviour molecule = GameObject.Find("Molecule").GetComponent <MoleculeBehaviour>(); GameObject newAtom = Instantiate((GameObject)Resources.Load("Prefabs/Atom", typeof(GameObject)), location, Quaternion.identity, molecule.transform); AtomBehaviour atomScript = newAtom.GetComponent <AtomBehaviour>(); atomScript.initializeAtom(type); return(atomScript); }
public void recreateMolecule(string encodedSave) { if (encodedSave != null) { Molecule molecule = Molecule.deserialize(UnityWebRequest.UnEscapeURL(encodedSave)); Hashtable newAtoms = new Hashtable(); foreach (Atom atom in molecule.a) { if (atom != null) { GameObject newAtom = Instantiate((GameObject)Resources.Load("Prefabs/Atom", typeof(GameObject)), new Vector3(atom.x, atom.y, atom.z), Quaternion.identity, transform); AtomBehaviour atomScript = newAtom.GetComponent <AtomBehaviour>(); atomScript.initializeAtom(atom.type, atom.charge); newAtoms.Add(atom.id, atomScript); } } foreach (Bond bond in molecule.b) { if (bond != null) { GameObject newBond = null; switch (bond.type) { case (int)BondBehaviour.BondType.Single: newBond = Instantiate((GameObject)Resources.Load("Prefabs/SingleBond", typeof(GameObject)), transform.position, Quaternion.identity, transform); break; case (int)BondBehaviour.BondType.Double: newBond = Instantiate((GameObject)Resources.Load("Prefabs/DoubleBond", typeof(GameObject)), transform.position, Quaternion.identity, transform); break; case (int)BondBehaviour.BondType.Triple: newBond = Instantiate((GameObject)Resources.Load("Prefabs/TripleBond", typeof(GameObject)), transform.position, Quaternion.identity, transform); break; case (int)BondBehaviour.BondType.Electrons: newBond = Instantiate((GameObject)Resources.Load("Prefabs/Electrons", typeof(GameObject)), transform.position, Quaternion.identity, transform); break; } if (newBond != null) { AtomBehaviour anchor1 = (AtomBehaviour)newAtoms[bond.a1]; AtomBehaviour anchor2 = (AtomBehaviour)newAtoms[bond.a2]; BondBehaviour bondScript = newBond.GetComponent <BondBehaviour>(); bondScript.initializeBond(anchor1, anchor2); if (anchor1 != null) { anchor1.addBond(bondScript); } if (anchor2 != null) { anchor2.addBond(bondScript); } } } } } }