Example #1
0
    static public SimulationObjectState createObjectFromJSON(string stateStr, GameObject[] gameObjectTemplates, bool active)
    {
        if (gameObjectTemplates != null)
        {
            var state = JsonUtility.FromJson <SimulationObjectState>(stateStr);

            GameObject refObject = gameObjectTemplates[state.templateIndex];
            GameObject obj       = Object.Instantiate(refObject);

            SimulationMaterial mat = new SimulationMaterial((SimulationMaterial.TYPE)state.material);
            obj.GetComponent <Rigidbody>().SetDensity(mat.GetDensity());

            SimulationColor col    = new SimulationColor((SimulationColor.TYPE)state.color);
            Material        newMat = Object.Instantiate(refObject.GetComponent <Renderer>().material);
            newMat.SetColor("_BaseColor", col.GetColor(mat.type));
            obj.GetComponent <Renderer>().material = newMat;

            obj.transform.position = state.position;
            obj.transform.rotation = state.rotation;
            //obj.GetComponent<Rigidbody>().velocity = state.velocity;
            //obj.GetComponent<Rigidbody>().inertiaTensor = state.inertiaTensor;
            //obj.GetComponent<Rigidbody>().inertiaTensorRotation = state.inertiaTensorRotation;
            //obj.GetComponent<Rigidbody>().angularVelocity = state.angularVelocity;

            if (active && state.active == 1)
            {
                obj.SetActive(true);
            }

            state.SetGameObject(obj);
            return(state);
        }
        return(null);
    }
    static public string toJSON(List <SimulationObjectState> objStates)
    {
        if (objStates != null)
        {
            SimulationSceneState sceneState = new SimulationSceneState();
            foreach (SimulationObjectState obj in objStates)
            {
                SimulationColor    col = new SimulationColor((SimulationColor.TYPE)obj.color);
                SimulationMaterial mat = new SimulationMaterial((SimulationMaterial.TYPE)obj.material);
                sceneState.objectStates.Add(SimulationObjectState.createJSONFromObject(obj.GetGameObject(), mat, col, obj.size, obj.templateIndex));
            }

            return(JsonUtility.ToJson(sceneState));
        }

        return("");
    }
Example #3
0
    static public string createJSONFromObject(GameObject obj, SimulationMaterial mat, SimulationColor col, int size, int templateIndex)
    {
        if (obj != null)
        {
            SimulationObjectState state = new SimulationObjectState();
            state.position              = obj.transform.position;
            state.rotation              = obj.transform.rotation;
            state.velocity              = obj.GetComponent <Rigidbody>().velocity;
            state.inertiaTensor         = obj.GetComponent <Rigidbody>().inertiaTensor;
            state.inertiaTensorRotation = obj.GetComponent <Rigidbody>().inertiaTensorRotation;
            state.angularVelocity       = obj.GetComponent <Rigidbody>().angularVelocity;
            state.material              = (int)mat.type;
            state.color = (int)col.type;
            state.size  = size;
            //state.sceneIndex = sceneIndex;
            state.templateIndex = templateIndex;
            state.active        = obj.activeSelf ? 1 : 0;

            return(JsonUtility.ToJson(state));
        }

        return("");
    }