c# - 无法将类型“UnityEngine.ScriptableObject”隐式转换为“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_;
}
}
任何人都可以帮助我解决这个问题,因为我无法解决这些错误:
- Assets/Rocket/Archi/Scripts/Editor/UVEditor/UVBaseTool.cs(219,14):错误 CS0029:无法隐式转换类型
Rocket.Archi.BaseToolProperties' to
T' - Assets/Rocket/Archi/Scripts/Editor/UVEditor/UVBaseTool.cs(224,14):错误 CS0029:无法隐式转换类型
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();
}
这是基础工具;
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();
}
}
}
附加的图像显示了我如何在其他类中调用属性,相信我在这部分没有错误,我已经一次又一次地检查了所有上下文,但是这个通用函数正在杀死我。
如果我这样写,错误就会消失,但现在我收到另一个错误,它是针对 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 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 - Modeling Tools/Archi/Scripts/Editor/BaseTool.cs:224) RocketMod.Archi.PropertiesGUI.HasContents () (at Assets/Rocket - Modeling Tools/Archi/ Scripts/Editor/PropertiesGUI.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 - Modeling Tools/Archi/Scripts/Editor/ArchiEditor.cs:
如果要解决这个问题,我会写如下一切都停止工作;
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_;
}
推荐阅读
- command-line - taskkill 命令删除程序
- python - 如何解决“ValueError:`window_shape` 与 `arr_in.shape` 不兼容”?
- arrays - 如何使用打字稿将地图值存储到数组的第一个位置?
- autodesk-forge - Autodesk 锻造/查看器
- neovim - 带有 coc 和 python 格式的 nvim
- android-source - Android 汽车仪表盘模拟器
- amazon-web-services - 尝试导入 swagger.json 文件时,AWS CLI 中出现 BadRequestException
- apache - 如何将长网址重定向到短网址,但将长网址提供给客户端?
- reactjs - 无论我做什么,盖茨比都没有创建盖茨比页面
- django - forms.py 使用 ForeignKey 模型创建表单。商店应该只能将产品添加到自己的商店。(姜戈)