簡體   English   中英

是否可以將 Unity GameObject 轉換為 ScriptableObject 類類型?

[英]Is it possible to cast Unity GameObject to ScriptableObject class type?

我創建了可編寫腳本的對象自定義類 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 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;
}

通過鼠標單擊 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;
}

在程序的其他部分,我有List<Card> ActiveCards名單,我不能夠施展Raycasted游戲物體來個ScriptableObject類

GameObject g = RaycastOpenedCard("CardUIPrefabOpened");
if (g != null)
{
    if (ActiveCards != null && ActiveCards.Count < 5)
    {
        Mplayer.ActiveCards.Add(g.GetComponent<Card>());
    }
}

是否可以將 Unity GameObject 轉換ScriptableObject類類型?

錯誤:

ArgumentException: GetComponent 要求請求的組件“Card”派生自 MonoBehaviour 或 Component 或者是一個接口。 UnityEngine.GameObject.GetComponent[T] () (在 C:/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:28) BoardManager.Update () (在 Assets/Scripts/BoardManager.cs: 202)

簡而言之,不,不可能將GameObjectScriptableObject ,反之亦然。 這是因為GameObject不繼承ScriptableObject而且也沒有ScriptableObjectGameObject 然而,它們都直接從Object繼承。

之所以行GameObject go = Instantiate(CardPrefabTemplate, new Vector3(x_delt, 0, 0), Quaternion.identity) as GameObject; 是因為Instantiate將一個Object作為參數並克隆它,返回一個Object ScriptableObject是一個Object因此可以工作。 你可以將一個Object為一個GameObject Object ,這樣也可以。 但這並沒有達到什么效果,因為這樣做是Object部分,所以在您的示例中, Object.name

現在GetComponent失敗的原因是因為它只能查找MonoBehaviour ,而ScriptableObject (Card) 不是。 順便說一下, MonoBehaviour也從Object繼承了一個點。

所以真正的問題是你一開始想達到什么目的?

暫無
暫無

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

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