I have saved the local position and rotation of hundreds of gameobjects in my hierarchy, and I’d like to be able to load the position and rotation values back into my gameobject’s transforms within the Editor itself when I press a button. That is, not at runtime or in play mode. Only when I’m not in playmode.
I have the saving and loading part down – my only issue is how to do it in the Editor, instead of at playtime.
If I load the position and rotation values during play mode/runtime then the values are not saved when I exit playmode. I need the values to be saved – like normally if you manually change something on a gameobject in the hierarchy before hitting play, it saves that setting even after you exit playmode (it reverts back to what it was before playmode).
So, I say all of that to ask: Lets say I attach a script to all of the gameobjects I want to modify in the hierarchy. How do I get the script to load the values I want in the Editor while NOT in playmode?
Here is an example of what I’m trying right now:
This script is attached to all of the gameobjects in my hierarchy that I want to modify.
using UnityEngine;
using UnityEditor;
using System.Collections;
[ExecuteInEditMode]
public class HouseDestruction1 : MonoBehaviour {
public Vector3 houseLocalPositionVector1;
public Vector3 houseLocalEulerAngleVector1;
public int testInteger1 = 0;
public Vector3 currentEulerAngle;
// Use this for initialization
void Start () {
// *** This was created as an Editor/game making tool really, and shouldn't need to happen in a real build of the game (currently).
// I wanted to add driveways to every house in the game, but didn't want to manually have to reposition each house after I modified
// the prefab of each house (modifying the prefab causes the houses to take the position and rotation of the prefab itself). So
// to try to make life easier, I wanted to automatically save each houses position and rotation to a save file. And then load
// them back in once the prefab was modified.
// Save this houses transform position to a vector for loading later
//***ES2.Save(transform.localPosition, "HouseInformation.stormef1?tag=transformLocalPositionHouse" + gameObject.GetInstanceID());
//Also save the local rotation's Euler Angles of the fence or post
//***ES2.Save(transform.localEulerAngles, "HouseInformation.stormef1?tag=transformLocalEulerAngleHouse" + gameObject.GetInstanceID());
currentEulerAngle = transform.localEulerAngles;
}
// Update is called once per frame
void Update () {
#if UNITY_EDITOR
if (Application.isEditor && Application.isPlaying == false)
{
if (Input.GetKeyUp(KeyCode.L) && testInteger1 == 1)
{
houseLocalPositionVector1 = ES2.Load<Vector3>("HouseInformation.stormef1?tag=transformLocalPositionHouse" + gameObject.GetInstanceID());
houseLocalEulerAngleVector1 = ES2.Load<Vector3>("HouseInformation.stormef1?tag=transformLocalEulerAngleHouse" + gameObject.GetInstanceID());
transform.localPosition = houseLocalPositionVector1;
transform.localEulerAngles = houseLocalEulerAngleVector1;
}
}
#endif
}
Currently this code does not work. I’m not sure how to structure it so that it does work in the Editor.