Exemple #1
0
    /**
     * Handler for whenever the custom inspector is enabled
     */
    protected void OnEnable()
    {
        controller  = (WaterGridController)target;
        vectorField = controller.vectorField;

        vectorField.LoadFromFile(controller.tilemap);
    }
    /**
     * Load vector field data from a file
     */
    public void LoadFromFile(Tilemap tilemap)
    {
#if UNITY_EDITOR
        // make sure that the file to load from actually exists
        if (File.Exists(GetEditorDataFilePath()))
        {
            // read in the data from the file
            string dataAsJson = File.ReadAllText(GetEditorDataFilePath());

            // make a temporary object to take in the data from the file
            WaterGridVectorField tempVF = ScriptableObject.CreateInstance <WaterGridVectorField>();
            JsonUtility.FromJsonOverwrite(dataAsJson, tempVF);

            // make sure that the tilemap size matches the saved data
            if (tilemap.size.x * tilemap.size.y == tempVF.vectors.Length)
            {
                // if the data matches, replace current vector field data with data from file
                this.vectors = tempVF.vectors;
            }
            else
            {
                // if the data does not match, reset the vector field based on the tilemap
                ResetVectorField(tilemap);
            }
        }
        else
        {
            // if the file does not exist, reset the vector field based on the tilemap
            ResetVectorField(tilemap);
        }
#elif UNITY_STANDALONE || UNITY_WEBGL
        TextAsset textAsset = (TextAsset)Resources.Load("VectorField/" + this.name, typeof(TextAsset));

        // make a temporary object to take in the data from the file
        WaterGridVectorField tempVF = ScriptableObject.CreateInstance <WaterGridVectorField>();
        JsonUtility.FromJsonOverwrite(textAsset.text, tempVF);

        // make sure that the tilemap size matches the saved data
        if (tilemap.size.x * tilemap.size.y == tempVF.vectors.Length)
        {
            // if the data matches, replace current vector field data with data from file
            this.vectors = tempVF.vectors;
        }
        else
        {
            // if the data does not match, reset the vector field based on the tilemap
            ResetVectorField(tilemap);
        }
#endif
    }