簡體   English   中英

無法將類型'UnityEngine.ScriptableObject'隱式轉換為'T'

[英]Cannot implicitly convert type `UnityEngine.ScriptableObject' to `T'

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. 資產/火箭/ Archi /腳本/編輯器/UVEditor/UVBaseTool.cs(219,14):錯誤CS0029:無法將Rocket.Archi.BaseToolProperties' to類型隱式轉換Rocket.Archi.BaseToolProperties' to T'
  2. 資產/火箭/存檔/腳本/編輯器/UVEditor/UVBaseTool.cs(224,14):錯誤CS0029:無法將類型UnityEngine.ScriptableObject' to隱式轉換UnityEngine.ScriptableObject' to T'

我嘗試通過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();
}

這是baseTool;

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();
    }
  }
}

所附的圖像顯示了我如何在其他類中調用Properties,並相信我在該部分中沒有錯誤,我已經一遍又一遍地檢查了所有上下文,但是這個通用函數使我喪命。

如果我這樣寫,錯誤消失了,但是現在我又遇到了另一個錯誤,該錯誤是針對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 conversionType,System.IFormatProvider提供程序)(在0處)System.Convert.ChangeType(System.Object值,System.Type conversionType)(在0處)RocketMod.Archi .BaseTool.Properties [T]()(在Assets / Rocket-建模工具/Archi/Scripts/Editor/BaseTool.cs:224處)RocketMod.Archi.PropertiesGUI.HasContents()(在Assets / Rocket-建模工具/ Archi /腳本/編輯器/屬性GUI.cs:54)RocketMod.Archi.PropertiesGUI.OnGUI()(在Assets / Rocket-建模工具/Archi/Scripts/Editor/PropertiesGUI.cs:17)RocketMod.Archi.ToolSelectionGUI.OnGUI()(在Assets / Rocket-建模工具/Archi/Scripts/Editor/ToolSelectionGUI.cs:164處)RocketMod.Archi.ArchiEditor.OnInspectorGUI()(在Assets / Rocket-建模工具/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));
    }

任何幫助都會被認可的人!

您在返回特定類型屬性的方法中使用通用類型<T>

用以下內容替換方法的簽名:

//     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_;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM