【问题标题】:Cannot implicitly convert type `UnityEngine.ScriptableObject' to `T'无法将类型“UnityEngine.ScriptableObject”隐式转换为“T”
【发布时间】:2019-02-06 15:33:32
【问题描述】:
public class UVBaseTool : ScriptableObject, IPropertiesChanged
{
    private BaseToolProperties properties_;
    private ScriptableObject propertiesNoValidate_;

    // Some code ...

    protected void SetProperties(System.Type propertiesType, System.Type properties1Type)
    {
        properties_ = propertiesType == null ? null : CreateInstance(propertiesType) as BaseToolProperties;
        if (properties_ != null)
        {
            properties_.tool = (this);
        }

        propertiesNoValidate_ = properties1Type == null ? null : CreateInstance(properties1Type);
    }

    public T Properties<T>()
    {
        return properties_;
    }

    public T PropertiesNoValidate<T>()
    {
        return propertiesNoValidate_;
    }
}

谁能帮我解决这个问题,因为我无法解决这些错误:

  1. Assets/Rocket/Archi/Scripts/Editor/UVEditor/UVBaseTool.cs(219,14):错误 CS0029:无法隐式转换类型 Rocket.Archi.BaseToolProperties' toT'
  2. 资产/Rocket/Archi/Scripts/Editor/UVEditor/UVBaseTool.cs(224,14): 错误 CS0029:无法隐式转换类型 UnityEngine.ScriptableObject' toT'

我尝试浏览 CS0029 的在线上下文,但我似乎仍然无法通过 IConvertable 进行转换。如果有解决办法,请告诉我。

这就是我打电话的方式

[SerializeField]
private BaseToolProperties properties_;
[SerializeField]
private ScriptableObject propertiesNoValidate_;

public virtual void Start()
{
      EditorUtil.HideGlobalGizmo();
      ArchiEx.selectedElements.ClearVertexBoxes();
      ArchiEx.currentCamera = (Camera) null;
      GizmoHandler.Destroy();
      if (properties_ != null)
        EditorUtil.LoadObject(properties_, null);
      if (propertiesNoValidate_ != null)
        EditorUtil.LoadObject(propertiesNoValidate_, null);
      Ruler.Add(this, RulerType.Local);
      ArchiSettings.GatherArchi();
}

这是基础工具;

using System;
using UnityEngine;

namespace Rocket.Archi
{
  [Serializable]
  public class BaseToolProperties : ScriptableObject
  {
    [NonSerialized]
    public IPropertiesChanged tool;

    public virtual void OnValidate()
    {
      if (tool == null)
        return;
      tool.OnPropertiesChanged();
    }
  }
}

The Image Attached shows how i am calling Properties in other Classes and believe me there is no error in that part i have checked all the context again and again, but this generic function is killing me.

如果我这样写,错误就会消失,但现在我收到另一个错误,它是针对 IConvertable 的。每当我尝试在场景视图中制作对象时,都会出现这个反复出现的错误。见下文;

public T Properties<T>()
    {
        return (T)Convert.ChangeType(properties_, typeof(T));
    }

    public T PropertiesNoValidate<T>()
    {
        return (T)Convert.ChangeType(propertiesNoValidate_, typeof(T));
    }

InvalidCastException:对象必须实现 IConvertible。 System.Convert.ChangeType(System.Object 值,System.Type 转换类型,System.IFormatProvider 提供程序)(在:0) System.Convert.ChangeType(System.Object 值,System.Type 转换类型)(在:0) RocketMod.Archi.BaseTool.Properties[T] () (在 Assets/Rocket - Modeling Tools/Archi/Scripts/Editor/BaseTool.cs:224) RocketMod.Archi.PropertiesGUI.HasContents () (在 Assets/Rocket - Modeling Tools/Archi/Scripts/Editor/PropertiesGUI.cs:54) RocketMod.Archi.PropertiesGUI.OnGUI () (在 Assets/Rocket - Modeling Tools/Archi/Scripts/Editor/PropertiesGUI.cs:17) RocketMod.Archi.ToolSelectionGUI.OnGUI () (在 Assets/Rocket - Modeling Tools/Archi/Scripts/Editor/ToolSelectionGUI.cs:164) RocketMod.Archi.ArchiEditor.OnInspectorGUI () (在 Assets/Rocket - Modeling Tools/Archi/Scripts/Editor/ArchiEditor.cs:299) UnityEditor.InspectorWindow.DrawEditor(UnityEditor.Editor[] 编辑器,System.Int32 editorIndex,System.Boolean rebuildOptimizedGUIBlock,System.Boolean& showImportedObjectBarNext,UnityEngine.Rect&importedObjectBarRect)(在 C:/buildslave/unity/build/Editor/Mono/Inspector/ InspectorWindow.cs:1253) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

如果要解决这个问题,我会写如下一切都停止工作;

public T Properties<T>() where T : IConvertible
    {
        return (T)Convert.ChangeType(properties_, typeof(T));
    }

    public T PropertiesNoValidate<T>() where T : IConvertible
    {
        return (T)Convert.ChangeType(propertiesNoValidate_, typeof(T));
    }

任何帮助都会非常感激的人!!!

【问题讨论】:

  • 欢迎来到 Stack Overflow。请阅读the help pages,获取the SO tour,了解how to ask good questions,以及this question checklist。最后在选择标签时请注意。您选择了 C 语言标记,而不是 C#。您还应该添加了unit3d 标签。
  • 你的问题中有很多的代码,你能把与问题无关的部分去掉吗?我怀疑有人会阅读你的整个代码来猜测你的错误在哪里
  • 我只在这些行上遇到这些错误
  • T 是一个泛型类型。 BaseToolProperties 的类型为 ScriptableObject。您在其他课程中如何称呼Properties()?想象一下使用Properties&lt;int&gt;(),您将如何将_properties 转换为int 以返回它?
  • 例如颜色,我称它为颜色 color = Properties().color;

标签: c# unity3d


【解决方案1】:

您在返回特定类型属性的方法中使用了泛型类型&lt;T&gt;

用论文替换方法的签名:

//     v----------------v--------------The type of properties_
public BaseToolProperties Properties()
{   //                              ^----------------the <T> was removed
    return properties_;
}

//     v--------------v----------------The type of propertiesNoValidate_
public ScriptableObject PropertiesNoValidate()
{   //                                      ^--------the <T> was removed
    return propertiesNoValidate_;
}

【讨论】:

  • 这解决了手头的错误,但在我尝试统一创建网格编辑工具时会产生问题。可以这么说,这些 properties_ 和 propertiesNoValidate_ 作为包中的可序列化字段从 BaseMeshTool 反映到其他组件。所以现在他们都停止了工作。
  • 我想在您的问题帖子中看到您如何称呼Properties&lt;T&gt;()PropertiesNoValidate&lt;T&gt;()
  • 所有工具都出现此错误错误 CS0308:非泛型方法 `Rocket.Archi.BaseTool.Properties()' 不能与类型参数一起使用
猜你喜欢
  • 2019-12-02
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多