C# 如何保存UI图像旋转并在从Unity3D中的不同场景返回时加载它?
在我的2D游戏中,玩家应该能够基于鼠标拖动功能旋转UI图像,然后按下“Go”按钮开始关卡,然后关卡上的“Back”按钮将玩家带回主场景(具有UI图像的场景),因此,图像应该以玩家为其设置的最后一个旋转显示。对我来说,除了加载图像旋转部分,一切都非常好。我能够在控制台中看到正确的旋转值,但是当从另一个场景返回时,我在图像上看不到旋转。我已经尝试了很多次了,但是没有得到任何好的结果。如果你明天帮我庆祝生日,我将不胜感激 我希望这将更好地解释我正在尝试做的事情:C# 如何保存UI图像旋转并在从Unity3D中的不同场景返回时加载它?,c#,unity3d,save,image-rotation,C#,Unity3d,Save,Image Rotation,在我的2D游戏中,玩家应该能够基于鼠标拖动功能旋转UI图像,然后按下“Go”按钮开始关卡,然后关卡上的“Back”按钮将玩家带回主场景(具有UI图像的场景),因此,图像应该以玩家为其设置的最后一个旋转显示。对我来说,除了加载图像旋转部分,一切都非常好。我能够在控制台中看到正确的旋转值,但是当从另一个场景返回时,我在图像上看不到旋转。我已经尝试了很多次了,但是没有得到任何好的结果。如果你明天帮我庆祝生日,我将不胜感激 我希望这将更好地解释我正在尝试做的事情: 第一个场景,正方形是UI图像,Go按钮
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);
}
这是分配给第一个场景中“Go”按钮的代码:
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");
}
以及分配给第二个场景中“后退”按钮的代码:
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");
}
之所以看不到旋转,是因为从未在实际图像对象上设置旋转。不能从“后退”按钮执行此操作,因为该场景中不存在图像对象 只需在唤醒时加载设置即可:
void Awake()
{
Vector3 imgRot;
imgRot.x = PlayerPrefs.GetFloat ("PlayerX");
imgRot.y = PlayerPrefs.GetFloat ("PlayerY");
imgRot.z = PlayerPrefs.GetFloat ("PlayerZ");
transform.rotation = Quaternion.Euler(imgRot);
}
请记住,此设置将在游戏关闭和重新启动之间保持。如果这不是您想要的行为,请参阅下面的示例,了解如何在场景之间保持这些设置
而不是使用PravePrEs来存储场景之间的信息,考虑使用DoTunDebug下载来保存数据的对象。
例如,场景中可能有一个类似以下内容的对象:public class RotationStorage : public MonoBehaviour
{
private Quaternion m_imageRotation = Quaternion.identity;
void Awake()
{
DontDestroyOnLoad(gameObject);
}
void SetRotation(Quaternion rotation)
{
m_imageRotation = rotation;
}
void OnLevelWasLoaded(int level)
{
if (Application.loadedLevelName == "Your starting scene")
{
GameObject image = GameObject.Find("name of your object");
//If the rotation hasn't been set yet don't set the image' rotation.
if(image != null && m_rotation != Quaternion.identity)
{
image.transform.rotation = m_imageRotation;
}
}
}
}
然后,在正在旋转的图像上的脚本中,保留对RotationStorage游戏对象的引用,并在调用OnMouseDrag时将当前旋转存储在其中:
private RotationStorage m_storage = null;
void Awake()
{
m_storage = FindObjectOfType<RotationStorage>();
}
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);
m_storage.SetRotation(rot);
}
private RotationStorage m_storage=null;
无效唤醒()
{
m_storage=FindObjectOfType();
}
void OnMouseDrag()
{
Vector3位置=Camera.main.WorldToScreenPoint(变换位置);
Vector3 dir=Input.mousePosition-pos;
浮动角度=Mathf.Atan2(方向y,方向x)*Mathf.Rad2Deg;
旋转=旋转;
rot=四元数。角度轴(角度,矢量3。向前);
m_存储。设置旋转(rot);
}
为了确保不会获得RotationStorage对象的重复副本,您需要将其设置在只加载一次的场景中,即启动时的启动屏幕。因此,我终于解决了问题!我在Back Button脚本中添加了一个布尔静态变量,因此每当按下按钮并将玩家带到第一个场景时,布尔值将变为true。在imgRotation脚本的另一端,我添加了一个Start()函数,因此每当场景开始时,它都会根据最后一个用户的输入旋转图像 这就是BackButtonScript:
public static Vector3 imgRot;
[HideInInspector] public static bool loadRot=false;
public void OnClicked(Button button)
{
imgRot = ImgRotation.rot.eulerAngles;
imgRot.x = PlayerPrefs.GetFloat ("PlayerX");
imgRot.y = PlayerPrefs.GetFloat ("PlayerY");
imgRot.z = PlayerPrefs.GetFloat ("PlayerZ");
loadRot= true;
Application.LoadLevel ("1");
}
这是我的演讲稿:
public static Quaternion rot;
void Start(){
if (BackButtonScript.loadRot == true) {
transform.Rotate(BackButtonScript.imgRot);
}
}
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);
}
希望有帮助!:) 您没有看到图像旋转,因为您没有旋转它。您有Vector3,但您没有在脚本中为“后退”按钮将其分配(仅打印)给游戏对象。@buxter我曾尝试在BackButtonScript中分配它,但每次都收到以下错误消息:NullReferenceException:对象引用未设置为对象的实例。我使用了这个脚本:ImgRotation temp=this.GetComponent();矢量3图像传输;imgTrans=temp.img.transform.rotation.euleranges;imgTrans.z=imgRot.z;你能把整个剧本都贴上去吗?同样,ImgRotation->这是您的脚本,而不是图像。@buxter当ImgRotation附加到UI图像时,我试图从该脚本获取引用的图像。@buxter这是我的ImgRotation脚本:使用UnityEngine;使用系统集合;使用System.Collections.Generic;使用UnityEngine.UI;公共类ImgRotation:monobhavior{public static Quaternion rot;public Image img;public void OnMouseDrag(){Vector3 pos=Camera.main.WorldToScreenPoint(transform.position);Vector3 dir=Input.mousePosition-position;float angle=Mathf.Atan2(dir.y,dir.x)*Mathf.Rad2Deg;transform.rotation=rot;rot=Quaternion.AngleAxis(angle,Vector3.forward);}}非常感谢您提供的帮助!我真的很感激。在看到你的答案之前,我尝试过另一种解决方案,它对我很有效。我还尝试了您的解决方案,并在这一行得到了一个NullReferenceException:m_storage.SetRotation(rot);。我现在就发布我的答案!非常感谢你!