[英]Is it possible to cast Unity GameObject to ScriptableObject class type?
I have created scriptable object custom class Card:我创建了可编写脚本的对象自定义类 Card:
[CreateAssetMenu(fileName = "Card", menuName = "Card")]
public class Card : ScriptableObject
{
public new string name;
public string Description;
public Sprite HeroImage;
public int Attack;
public int Health;
public int XCoord;
public int YCoord;
}
I have added plenty of such objects through Unity Editor, and instanced such objects as GameObjects on 2d Canvas:我通过 Unity Editor 添加了大量这样的对象,并在 2d Canvas 上实例化了诸如GameObjects 之类的对象:
for (int i = 0; i < 5; i++)
{
GameObject go = Instantiate(CardPrefabTemplate, new Vector3(x_delt, 0, 0), Quaternion.identity) as GameObject;
go.transform.SetParent(MPlayerOpenedCards.transform, false);
go.gameObject.name = "CardUIPrefabOpened" + i;
x_delt += (int)(CardPrefabTemplBackgrRect.rect.width * CardPrefabTemplateBackground.localScale.x) + x_card_margin;
}
After Raycast by mouse click I get current clicked Card GameObject :通过鼠标单击 Raycast 后,我得到当前单击的 Card GameObject :
GameObject RaycastOpenedCard(string prefabName)
{
//Set up the new Pointer Event
m_PointerEventData = new PointerEventData(m_EventSystem);
//Set the Pointer Event Position to that of the mouse position
m_PointerEventData.position = Input.mousePosition;
//Create a list of Raycast Results
List<RaycastResult> results = new List<RaycastResult>();
//Raycast using the Graphics Raycaster and mouse click position
m_Raycaster.Raycast(m_PointerEventData, results);
if (Input.GetMouseButtonDown(0))
{
foreach (RaycastResult result in results)
{
if (result.gameObject.transform.parent != null && Regex.IsMatch(result.gameObject.transform.parent.name, prefabName))
{
Debug.Log("Hit " + result.gameObject.transform.parent.name);
return result.gameObject.transform.parent.gameObject;
}
}
}
return null;
}
In other part of program, I have List<Card> ActiveCards
list and I am not able to cast Raycasted GameObject to scriptableobject class Card :在程序的其他部分,我有List<Card> ActiveCards
名单,我不能够施展Raycasted游戏物体来个ScriptableObject类卡:
GameObject g = RaycastOpenedCard("CardUIPrefabOpened");
if (g != null)
{
if (ActiveCards != null && ActiveCards.Count < 5)
{
Mplayer.ActiveCards.Add(g.GetComponent<Card>());
}
}
Is it possible to cast Unity GameObject to ScriptableObject class type?是否可以将 Unity GameObject 转换为ScriptableObject类类型?
Error:错误:
ArgumentException: GetComponent requires that the requested component 'Card' derives from MonoBehaviour or Component or is an interface. ArgumentException: GetComponent 要求请求的组件“Card”派生自 MonoBehaviour 或 Component 或者是一个接口。 UnityEngine.GameObject.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:28) BoardManager.Update () (at Assets/Scripts/BoardManager.cs:202) UnityEngine.GameObject.GetComponent[T] () (在 C:/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:28) BoardManager.Update () (在 Assets/Scripts/BoardManager.cs: 202)
Simply put, no, it's not possible to cast a GameObject
to ScriptableObject
, nor the opposite for that matter.简而言之,不,不可能将GameObject
为ScriptableObject
,反之亦然。 This is because GameObject
doesn't inherit from ScriptableObject
and neither does ScriptableObject
from GameObject
.这是因为GameObject
不继承ScriptableObject
而且也没有ScriptableObject
从GameObject
。 Both of them directly inherit from Object
however.然而,它们都直接从Object
继承。
The reason the line GameObject go = Instantiate(CardPrefabTemplate, new Vector3(x_delt, 0, 0), Quaternion.identity) as GameObject;
之所以行GameObject go = Instantiate(CardPrefabTemplate, new Vector3(x_delt, 0, 0), Quaternion.identity) as GameObject;
is because Instantiate
that takes an Object
as argument and that clones it, returning an Object
.是因为Instantiate
将一个Object
作为参数并克隆它,返回一个Object
。 A ScriptableObject
is an Object
so that works. ScriptableObject
是一个Object
因此可以工作。 And you can cast an Object
to an GameObject
so that works too.你可以将一个Object
为一个GameObject
Object
,这样也可以。 But that doesn't achieve much as the only thing you'll keep by doing that is the Object
part, so in your example, Object.name
.但这并没有达到什么效果,因为这样做是Object
部分,所以在您的示例中, Object.name
。
Now the reason GetComponent
fails is because it can only look for a MonoBehaviour
, which a ScriptableObject
(Card) isn't.现在GetComponent
失败的原因是因为它只能查找MonoBehaviour
,而ScriptableObject
(Card) 不是。 MonoBehaviour
also inherit from Object
a some point by the way.顺便说一下, MonoBehaviour
也从Object
继承了一个点。
So the real question is what were you trying to achieve in the first place ?所以真正的问题是你一开始想达到什么目的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.