【问题标题】:how chage an image of a gameobject and display it on unity如何更改游戏对象的图像并统一显示
【发布时间】:2020-07-22 12:14:06
【问题描述】:

我对统一很陌生,我正在尝试制作纸牌游戏。 我在一个列表中创建了 52 个对象,每个对象都有设置和获取选项。 现在我正在尝试获取对象的图像并通过 VS 将其显示在游戏中。 我的问题是我不知道如何在 VisualStudio 中更改游戏对象的图像。

这是我做的代码:

// The purpose of the function is to display the card image in canvas when play 
 public void Deal () {   
    deck = GenerateDeck();// generate a deck of 52 cards
    Card card = deck[15]; // refer to a spesific card in deck
    Sprite image = card.GetCardImage();
    imagecards.sprite = image;
    card1.AddComponent<Image>();
    card1.sprite=imagecards; <- this line dosent work
    GameObject playercard = Instantiate(card1, new Vector3(0, 0, 0), Quaternion.identity);
    playercard.transform.SetParent(PlayerArea.transform, false);        
 }

【问题讨论】:

    标签: image unity3d gameobject


    【解决方案1】:

    您需要通过获取现在添加的图像组件。 GetComponent&lt;Image&gt;() 访问精灵并更改它。

    示例:

    card1.GetComponent<Image>().sprite = imagecards;
    

    【讨论】:

    • 如果有帮助,请提前将其标记为解决方案。
    • 点击答案左侧的灰色复选标记即可完成
    猜你喜欢
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多