- Home /
Figured out an answer
How to Save a UI Image Rotation and Load it When Coming Back from a Different Scene?
Hello,
In my 2D game the player should be able to rotate a UI Image based on a Mouse Drag function, and then press the "Go" button to start the level, and after this the "Back" button on the level should take the player back to the main scene (the one that has the UI Image), thus the image should be displayed in the same last rotation the player has set for it. Everything works fantastically fine for me except for the part of loading the image rotation. I was able to see the correct rotation value in the console, however I see no rotation on the image when coming back from the other scene. I've been trying to do so many attempts to make it work but not getting any good results. It will be so much appreciated if you help me celebrate my birthday tomorrow!
I hope this will explain better what I am trying to do:
This is the first scene where the square is the UI Image and the Go button is under
This is after the user drags the image and thus it gets a new rotation
And then there is a second scene that has the Back button (it takes the user to the main scene and should display the rotated UI image based on the last user's input). The issue is that there is no rotation shown at all on the UI Image.
And here is the code I have used for the UI Image Rotation:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ImgRotation : MonoBehaviour {
public static Quaternion rot;
public void OnMouseDrag(){
Vector3 pos = Camera.main.WorldToScreenPoint (transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = rot;
rot = Quaternion.AngleAxis (angle, Vector3.forward);
}
}
And this is the code assigned to the "Go" Button in the first scene:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GoButtonScript : MonoBehaviour {
public void OnClicked(Button button)
{
PlayerPrefs.SetFloat ("PlayerX", ImgRotation.rot.eulerAngles.x);
PlayerPrefs.SetFloat ("PlayerY", ImgRotation.rot.eulerAngles.y);
PlayerPrefs.SetFloat ("PlayerZ", ImgRotation.rot.eulerAngles.z);
Application.LoadLevel ("2");
}
}
And the code assigned to the "Back" button in the second scene:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BackButtonScript : MonoBehaviour {
public void OnClicked(Button button)
{
Vector3 imgRot;
imgRot = ImgRotation.rot.eulerAngles;
imgRot.x = PlayerPrefs.GetFloat ("PlayerX");
imgRot.y = PlayerPrefs.GetFloat ("PlayerY");
imgRot.z = PlayerPrefs.GetFloat ("PlayerZ");
print (imgRot.z);
Application.LoadLevel ("1");
}
}
Answer by $$anonymous$$ · Nov 11, 2015 at 07:54 PM
The problem is that you assign the rotation values in the second scene and then you switch back to the first one, thus losing those changes.
Simply add this to ImgRotation (I assume it's on an UI Image) to read the values of PlayerPrefs when the scene starts (also remove that code before Application.LoadLevel("1") on scene 2 because it's useless:
using UnityEngine.UI;
public class ImgRotation : MonoBehaviour
{
Image img;
public void Start()
{
img = GetComponent<Image>();
Vector3 imgRot = new Vector3();
imgRot.x = PlayerPrefs.GetFloat("PlayerX",0);
imgRot.y = PlayerPrefs.GetFloat("PlayerY",0);
imgRot.z = PlayerPrefs.GetFloat("PlayerZ",0);
img.rectTransform.rotation = Quaternion.Euler(imgRot);
}
}
Thank you Al3_x for the answer. I have found a solution long time ago and even posted the answer here, and I don't understand why I can't see the answer I posted anymore.. $$anonymous$$any thanks again Al3_x, my answer was a bit similar to this.
Glad you solved it! I was searching for some UI things and saw your question unanwered, I didn't see the date. Care to mark it as solved or accept my answer?
It was very nice from you and I appreciate the help! Do you know how I can mark the question as solved?
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Question Regarding Don'tDestroyOnLoad Object 1 Answer
Object instantiate, Projectile problems 2 Answers