简体   繁体   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_;
    }
}

Can Any one help me out with this as i am not able to resolve these errors: 任何人都可以帮我解决这个问题,因为我无法解决以下错误:

  1. Assets/Rocket/Archi/Scripts/Editor/UVEditor/UVBaseTool.cs(219,14): error CS0029: Cannot implicitly convert type Rocket.Archi.BaseToolProperties' to T' 资产/火箭/ Archi /脚本/编辑器/UVEditor/UVBaseTool.cs(219,14):错误CS0029:无法将Rocket.Archi.BaseToolProperties' to类型隐式转换Rocket.Archi.BaseToolProperties' to T'
  2. Assets/Rocket/Archi/Scripts/Editor/UVEditor/UVBaseTool.cs(224,14): error CS0029: Cannot implicitly convert type UnityEngine.ScriptableObject' to T' 资产/火箭/存档/脚本/编辑器/UVEditor/UVBaseTool.cs(224,14):错误CS0029:无法将类型UnityEngine.ScriptableObject' to隐式转换UnityEngine.ScriptableObject' to T'

I tried going through the online context for the CS0029 but still i cant seem to convert through IConvertable. 我尝试通过CS0029的在线上下文,但仍然似乎无法通过IConvertable进行转换。 Please let me know if there is a solution. 请让我知道是否有解决方案。

This is how i am calling to 这就是我打电话给的方式

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

And this is the baseTool; 这是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();
    }
  }
}

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. 所附的图像显示了我如何在其他类中调用Properties,并相信我在该部分中没有错误,我已经一遍又一遍地检查了所有上下文,但是这个通用函数使我丧命。

The error Goes away if i write it like this but now i am getting another error which is for IConvertable. 如果我这样写,错误消失了,但是现在我又遇到了另一个错误,该错误是针对IConvertable的。 When ever i try to make an object in scene view i get this recurring error. 每当我尝试在场景视图中制作对象时,都会出现此重复错误。 See Below; 见下文;

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

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

InvalidCastException: Object must implement IConvertible. InvalidCastException:对象必须实现IConvertible。 System.Convert.ChangeType (System.Object value, System.Type conversionType, System.IFormatProvider provider) (at :0) System.Convert.ChangeType (System.Object value, System.Type conversionType) (at :0) RocketMod.Archi.BaseTool.Properties[T] () (at Assets/Rocket - Modelling Tools/Archi/Scripts/Editor/BaseTool.cs:224) RocketMod.Archi.PropertiesGUI.HasContents () (at Assets/Rocket - Modelling Tools/Archi/Scripts/Editor/PropertiesGUI.cs:54) RocketMod.Archi.PropertiesGUI.OnGUI () (at Assets/Rocket - Modelling Tools/Archi/Scripts/Editor/PropertiesGUI.cs:17) RocketMod.Archi.ToolSelectionGUI.OnGUI () (at Assets/Rocket - Modelling Tools/Archi/Scripts/Editor/ToolSelectionGUI.cs:164) RocketMod.Archi.ArchiEditor.OnInspectorGUI () (at Assets/Rocket - Modelling Tools/Archi/Scripts/Editor/ArchiEditor.cs:299) UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor[] editors, System.Int32 editorIndex, System.Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect 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) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1253) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) 和importedObjectBarRect)(在C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1253)UnityEngine.GUIUtility:ProcessEvent(Int32,IntPtr)

if to solve this i write as below everything stops working; 如果要解决这个问题,我写如下

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

ANY HELP WOULD BE MUCH APPRECIATED GUYS !!! 任何帮助都会被认可的人!

You are using generic type <T> in your method that returns a specific typed property. 您在返回特定类型属性的方法中使用通用类型<T>

Replace the signature of your methods by theses : 用以下内容替换方法的签名:

//     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.

相关问题 无法将类型“UnityEngine.AudioClip”隐式转换为“UnityEngine.AudioSource” - Cannot implicitly convert type 'UnityEngine.AudioClip' to 'UnityEngine.AudioSource' 无法隐式转换类型'UnityEngine.Vector2?'到'UnityEngine.Vector2' - Cannot implicitly convert type 'UnityEngine.Vector2?' to 'UnityEngine.Vector2' 无法将类型字符串隐式转换为“UnityEngine.UI.Text” - Cannot implicitly convert type string to `UnityEngine.UI.Text' 无法将类型`string&#39;隐式转换为`UnityEngine.GUIText&#39; - Cannot implicitly convert type `string' to `UnityEngine.GUIText' 无法将类型int&#39;隐式转换为unityengine.vector3&#39; - cannot implicitly convert type int' to unityengine.vector3' 如何修复“无法将类型'void'隐式转换为'UnityEngine.AsyncOperation”? - How to fix "Cannot implicitly convert type 'void' to 'UnityEngine.AsyncOperation"? 无法将类型“UnityEngine.GameObject”隐式转换为“GameObject” - Cannot implicitly convert type 'UnityEngine.GameObject' to 'GameObject' 无法将类型“float”隐式转换为“UnityEngine.Quaternion” - Cannot implicitly convert type 'float' to 'UnityEngine.Quaternion' c#无法将类型“布尔”隐式转换为“ UnityEngine.CursorLockMode” - c# Cannot implicitly convert type 'bool' to 'UnityEngine.CursorLockMode' 无法将类型&#39;bool&#39;隐式转换为&#39;UnityEngine.RaycastHit&#39; - Cannot implicitly convert type `bool' to `UnityEngine.RaycastHit'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM