Store/restore 属性 值
Store/restore property values
我正在使用 Unity,我正在尝试创建一个 class 可以存储特定组件状态并在稍后阶段恢复它们。然而,这与 Unity 无关,应该可以在任何类型的 属性 的任何地方使用。然而,这是我将使用的示例,因为它很直观,并且该示例的解决方案应该给出完整的答案。
我有一个 GameObject
,它附加了一个 Transform
类型的对象,它存储对象的位置、旋转和缩放。这是我想要存储并能够在以后恢复的内容。
存储状态
对象的状态通过创建 Transform
的深层副本来保存,然后保存在字典中,类型作为键,副本作为值。深拷贝可以通过 Serializable
class or an Object
extension 来实现。请注意,使用 IClonable
是不可能的,因为我不拥有 class Transform
的代码。使用 Object
扩展代码很容易。在 class:
里面
// The object that should have its state (re)stored
public GameObject _parent;
// Dictionary to hold the object state and its underlying type
public Dictionary<Type, object> _states;
// Store a deep copy of the component of type T
public void StoreState<T>() {
_states.Add(typeof(T), _parent.GetComponent<T>().Copy());
}
// Alternatively
public void StoreState(Type t) {
_states.Add(t, _parent.GetComponent(t).Copy());
}
正在恢复状态
这是我卡住的部分。我不能简单地创建 Transform
的新实例并将其分配给 GameObject
,因为它不可访问。如何从字典值中复制所有值?
public void RestoreInitialState() {
foreach (var pair in _states) {
// Copy to original?
_parent.GetComponent(pair.Key) = pair.Value.Copy();
// Error: The left-hand side of an assignment must be a variable, property or indexer
// This expression cannot be used as an assignment target
}
}
复制所有 public 字段 and/or 属性可能就足够了吗?
我一直在使用 XML 序列化来实现类似的目的。感觉有点像 hack,但到目前为止它对我来说效果很好。
这里有几个用于序列化和反序列化对象的辅助方法,这显然是您的 "Transform" 对象:
/// <summary>
/// Serializes an object to an xml string
/// </summary>
public static String Serialize(Object obj)
{
if (obj == null)
{
return "";
}
string xml = string.Empty;
try
{
// Remove xml declaration.
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using (StringWriter sw = new StringWriter())
{
using (XmlWriter xw = XmlWriter.Create(sw, settings))
{
XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
xmlSerializer.Serialize(xw, obj);
}
xml = sw.ToString();
}
}
catch (Exception) { }
return xml;
}
/// <summary>
/// Deserializes a xml string to an object
/// </summary>
public static Object Deserialize(String xml, Type type)
{
Object obj = null;
try
{
if (xml != string.Empty)
{
using (StringReader sr = new StringReader(xml))
{
XmlSerializer x = new XmlSerializer(type);
obj = x.Deserialize(sr);
}
}
}
catch (Exception e)
{
throw new PageException("XmlDeserialization failed", e);
}
return obj;
}
正如我所说,我不确定它是否 "good",但它确实有效。 =) 感谢指点!
在 Unity 论坛上发布了 Serialize Helper,完整且完整(或多或少)。但我会保存并恢复这些值。我不喜欢操纵运行时对象有几个原因(脆弱,如果出现任何问题我不知道是什么,为什么以及在哪里,平台构建可能是不可能的,等等)
使用新的 unity json 序列化程序 http://docs.unity3d.com/Manual/JSONSerialization.html,它比我使用过 unity 的任何 json 库更简单、更简单、更快,它甚至无需任何 problem.Create a class 并保存它的对象及其所有变量并恢复它们只需使用 jsontoObject 并且所有你的值都返回并恢复,如果那是你要求的。
我正在使用 Unity,我正在尝试创建一个 class 可以存储特定组件状态并在稍后阶段恢复它们。然而,这与 Unity 无关,应该可以在任何类型的 属性 的任何地方使用。然而,这是我将使用的示例,因为它很直观,并且该示例的解决方案应该给出完整的答案。
我有一个 GameObject
,它附加了一个 Transform
类型的对象,它存储对象的位置、旋转和缩放。这是我想要存储并能够在以后恢复的内容。
存储状态
对象的状态通过创建 Transform
的深层副本来保存,然后保存在字典中,类型作为键,副本作为值。深拷贝可以通过 Serializable
class or an Object
extension 来实现。请注意,使用 IClonable
是不可能的,因为我不拥有 class Transform
的代码。使用 Object
扩展代码很容易。在 class:
// The object that should have its state (re)stored
public GameObject _parent;
// Dictionary to hold the object state and its underlying type
public Dictionary<Type, object> _states;
// Store a deep copy of the component of type T
public void StoreState<T>() {
_states.Add(typeof(T), _parent.GetComponent<T>().Copy());
}
// Alternatively
public void StoreState(Type t) {
_states.Add(t, _parent.GetComponent(t).Copy());
}
正在恢复状态
这是我卡住的部分。我不能简单地创建 Transform
的新实例并将其分配给 GameObject
,因为它不可访问。如何从字典值中复制所有值?
public void RestoreInitialState() {
foreach (var pair in _states) {
// Copy to original?
_parent.GetComponent(pair.Key) = pair.Value.Copy();
// Error: The left-hand side of an assignment must be a variable, property or indexer
// This expression cannot be used as an assignment target
}
}
复制所有 public 字段 and/or 属性可能就足够了吗?
我一直在使用 XML 序列化来实现类似的目的。感觉有点像 hack,但到目前为止它对我来说效果很好。
这里有几个用于序列化和反序列化对象的辅助方法,这显然是您的 "Transform" 对象:
/// <summary>
/// Serializes an object to an xml string
/// </summary>
public static String Serialize(Object obj)
{
if (obj == null)
{
return "";
}
string xml = string.Empty;
try
{
// Remove xml declaration.
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using (StringWriter sw = new StringWriter())
{
using (XmlWriter xw = XmlWriter.Create(sw, settings))
{
XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
xmlSerializer.Serialize(xw, obj);
}
xml = sw.ToString();
}
}
catch (Exception) { }
return xml;
}
/// <summary>
/// Deserializes a xml string to an object
/// </summary>
public static Object Deserialize(String xml, Type type)
{
Object obj = null;
try
{
if (xml != string.Empty)
{
using (StringReader sr = new StringReader(xml))
{
XmlSerializer x = new XmlSerializer(type);
obj = x.Deserialize(sr);
}
}
}
catch (Exception e)
{
throw new PageException("XmlDeserialization failed", e);
}
return obj;
}
正如我所说,我不确定它是否 "good",但它确实有效。 =) 感谢指点!
在 Unity 论坛上发布了 Serialize Helper,完整且完整(或多或少)。但我会保存并恢复这些值。我不喜欢操纵运行时对象有几个原因(脆弱,如果出现任何问题我不知道是什么,为什么以及在哪里,平台构建可能是不可能的,等等)
使用新的 unity json 序列化程序 http://docs.unity3d.com/Manual/JSONSerialization.html,它比我使用过 unity 的任何 json 库更简单、更简单、更快,它甚至无需任何 problem.Create a class 并保存它的对象及其所有变量并恢复它们只需使用 jsontoObject 并且所有你的值都返回并恢复,如果那是你要求的。