是否可以将 Unity GameObject 转换为 ScriptableObject class 类型?
Is it possible to cast Unity GameObject to ScriptableObject class type?
我已经创建了 scriptable object custom class 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;
}
我已经通过 Unity 编辑器添加了大量此类对象,并在 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;
}
通过鼠标点击 Raycast 后我得到当前点击的卡片 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;
}
在程序的其他部分,我有 List<Card> ActiveCards
列表,但我无法 cast Raycasted GameObject 到 scriptableobject class 卡片:
GameObject g = RaycastOpenedCard("CardUIPrefabOpened");
if (g != null)
{
if (ActiveCards != null && ActiveCards.Count < 5)
{
Mplayer.ActiveCards.Add(g.GetComponent<Card>());
}
}
是否可以将 Unity GameObject 转换为 ScriptableObject class 类型?
错误:
ArgumentException: GetComponent requires that the requested component 'Card' derives from MonoBehaviour or Component or is an interface.
UnityEngine.GameObject.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:28)
BoardManager.Update () (at Assets/Scripts/BoardManager.cs:202)
简单地说,不,不可能将 GameObject
转换为 ScriptableObject
,在这方面也不可能相反。这是因为 GameObject
没有继承自 ScriptableObject
,ScriptableObject
也没有继承自 GameObject
。然而,它们都直接继承自 Object
。
行 GameObject go = Instantiate(CardPrefabTemplate, new Vector3(x_delt, 0, 0), Quaternion.identity) as GameObject;
的原因是因为 Instantiate
将 Object
作为参数并克隆它,返回 Object
。 ScriptableObject
是 Object
,这样就可以了。您可以将 Object
转换为 GameObject
这样也可以。但这并没有取得太大的成就,因为你唯一要做的就是 Object
部分,所以在你的例子中,Object.name
.
现在 GetComponent
失败的原因是因为它只能寻找 MonoBehaviour
,而 ScriptableObject
(Card) 不是。 MonoBehaviour
也继承自 Object
顺便说一句。
所以真正的问题是您最初想要实现的目标是什么?
我已经创建了 scriptable object custom class 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;
}
我已经通过 Unity 编辑器添加了大量此类对象,并在 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;
}
通过鼠标点击 Raycast 后我得到当前点击的卡片 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;
}
在程序的其他部分,我有 List<Card> ActiveCards
列表,但我无法 cast Raycasted GameObject 到 scriptableobject class 卡片:
GameObject g = RaycastOpenedCard("CardUIPrefabOpened");
if (g != null)
{
if (ActiveCards != null && ActiveCards.Count < 5)
{
Mplayer.ActiveCards.Add(g.GetComponent<Card>());
}
}
是否可以将 Unity GameObject 转换为 ScriptableObject class 类型?
错误:
ArgumentException: GetComponent requires that the requested component 'Card' derives from MonoBehaviour or Component or is an interface. UnityEngine.GameObject.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:28) BoardManager.Update () (at Assets/Scripts/BoardManager.cs:202)
简单地说,不,不可能将 GameObject
转换为 ScriptableObject
,在这方面也不可能相反。这是因为 GameObject
没有继承自 ScriptableObject
,ScriptableObject
也没有继承自 GameObject
。然而,它们都直接继承自 Object
。
行 GameObject go = Instantiate(CardPrefabTemplate, new Vector3(x_delt, 0, 0), Quaternion.identity) as GameObject;
的原因是因为 Instantiate
将 Object
作为参数并克隆它,返回 Object
。 ScriptableObject
是 Object
,这样就可以了。您可以将 Object
转换为 GameObject
这样也可以。但这并没有取得太大的成就,因为你唯一要做的就是 Object
部分,所以在你的例子中,Object.name
.
现在 GetComponent
失败的原因是因为它只能寻找 MonoBehaviour
,而 ScriptableObject
(Card) 不是。 MonoBehaviour
也继承自 Object
顺便说一句。
所以真正的问题是您最初想要实现的目标是什么?