【问题标题】:Unity Player throws excpetion: System.ArgumentException: Get Method not found for 'hideFlags'Unity Player 抛出异常:System.ArgumentException: Get Method not found for 'hideFlags'
【发布时间】:2019-09-04 03:42:36
【问题描述】:

所以我有一个 ComponenetCopier 脚本,复制脚本真是太棒了。

问题是当我构建我的游戏时它不起作用,它只是丢弃以下错误并中止该过程,但以下异常:

System.ArgumentException:在 System.Reflection.MonoProperty 中找不到“hideFlags”的获取方法

它死亡的代码在这里:

PropertyInfo[] properties = type.GetProperties();
Debug.Log("Do i die here?");
foreach (PropertyInfo property in properties)
{
     property.SetValue(myNew_Component, property.GetValue(original, null), null);
}

当它在编辑器模式下工作时,我不知道它为什么会失败,但这让我困扰了好几天。 非常感谢您的帮助...

Ps:我正在使用反射复制我的组件。

【问题讨论】:

  • 我实例化一个游戏对象,然后当不再需要该 GamObject 时,我将它的一些组件复制到一个新对象。
  • 嗯,这听起来应该有更好的设计来实现你想要实现的目标..

标签: c# unity3d reflection


【解决方案1】:

Object.hideFlags

对象应该隐藏、与场景一起保存还是由用户修改?

HideFlags

在检查器中控制对象销毁、保存和可见性的位掩码。

由于在构建中没有要修改、保存和隐藏的场景或检查器,因此该属性似乎仅在编辑器中才有意义。

所以你应该使用CanReadCanWrite 来添加支票

PropertyInfo[] properties = type.GetProperties();
Debug.Log("Do i die here?");
foreach (PropertyInfo property in properties)
{
    if(!property.CanRead || !property.CanWrite) continue;

    property.SetValue(myNew_Component, property.GetValue(original, null), null);
}

此外,您可以尝试使用GetProperties(BindingFlags) 的重载并传入相应的BindingFlags

如果没有传入它是

等于BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public


here 是一些进一步的例子。特别是还使用检查GetGetMethodGetsetMethod,例如

if(property.GetGetMethod(true) == null || property.GetSetMethod(true) == null) continue;

【讨论】:

  • 谢谢,我会检查这个!
【解决方案2】:

我认为 hideFlags 指的是这个 => UnityEngine.HideFlags Doc

PropertyInfo[] properties = type.GetProperties();
    foreach (PropertyInfo property in properties)
    {
        // Add this or else u might run into problems later
        if (!property.CanWrite)
        {
            continue;
        }
        // Hide flags is an Enumeration. the default value for it is HideFlags.None 
        // i assume u dont chnage this value, so for your use case this will be Ok
        if (property.PropertyType.IsEnum && property.ToString() == "UnityEngine.HideFlags hideFlags")
        {
            property.SetValue(my_Component, HideFlags.None);
            continue;
        }
        property.SetValue(my_Component, property.GetValue(original));
    }

上面的代码应该可以解决问题

【讨论】:

  • @Willer .. 基本上我不也是这样提议的吗...?
  • @derHugo 你的看起来好多了,我不明白为什么它不起作用,除非出现新的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
相关资源
最近更新 更多