【问题标题】:Why won't my sprite change during runtime?为什么我的精灵在运行时不会改变?
【发布时间】:2015-11-17 07:04:46
【问题描述】:

在统一的场景视图中,我设置了三个游戏对象,每个对象都有各自的精灵渲染器和精灵。三个中的两个在尺寸上比第三个小。

两个较小的精灵显示的图像不同。我想做的是能够通过单击其他两个来更改第三个的精灵。单击其中一个较小的精灵将使第三个较大的精灵与之前单击的小精灵匹配。

这是分配给两个较小精灵的相关脚本。

第一个处理小精灵上显示的精灵:

public class CardModel : MonoBehaviour
{
    SpriteRenderer spriteRenderer;  // renders sprite

    public int cardIndex;       // indicates what the card is
    public bool show;           // shows or hides card

    public Sprite[] cardFronts; // collection of cards' front sides
    public Sprite cardBack;     // backside of all cards

    // if card is turned up, show front of card, show back otherwise
    public void DisplaySprite()
    {
        if (show)
            spriteRenderer.sprite = cardFronts[cardIndex];
        else
            spriteRenderer.sprite = cardBack;
    }

    void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();    // retrieve our sprite renderer
        DisplaySprite();                                         // show card's sprite
    }
}

第二个使较小的精灵可点击:

public class ClickCard : MonoBehaviour
{
    CardModel model; // used to retrieve card index and bool to assign to larger sprite

    public GameObject largeSprite; // third large sprite

    void OnMouseDown()
    {
        CardModel cModel = GetComponent<CardModel>(); // retrieve card model from smaller sprite
        LargeCardModel largeCardModel = largeSprite.GetComponent<LargeCardModel>(); // retrieve card model for larger sprite

        largeCardModel.matchSprite(cModel.show, cModel.cardIndex); // match large sprite with small sprite clicked
    }
}

最后,附在第三个大精灵上的脚本:

public class LargeCardModel : MonoBehaviour
{
    public int largeIndex; // determines which sprite to show

    public Sprite[] largeCardFaces; // collection of large sprites
    public Sprite largeCardBack; // large card back

    SpriteRenderer largeRenderer; // renders the image upon the large sprite

    void Awake()
    {
        largeRenderer = GetComponent<SpriteRenderer>(); // retrieve renderer
    }

    // method to allow sprite to be changed
    public void matchSprite(bool showFace, int cIndex)
    {
        if (largeRenderer == null)
            largeRenderer = GetComponent<SpriteRenderer>(); // for some reason, I need to include this line or else I get an exception

        if (showFace)
            largeRenderer.sprite = largeCardFaces[cIndex];
        else
            largeRenderer.sprite = largeCardBack;
    }
}

无论出于何种原因,我无法在运行时将图像更改为较大的精灵,但是当我停止运行程序时,大精灵会变为我单击的最后一个较小精灵的图像。

【问题讨论】:

  • 如果我理解正确的话,你基本上有 3 个精灵,比如说 1、2、3。你的意思是,如果点击数字 1 或 2,数字 3 应该变成放大版您刚刚点击的那个?这是你的意思吗?
  • 是的。有两组图像。除了大小不同之外,这两组都是相同的。
  • 我认为你所拥有的代码有点矫枉过正,你是否尝试过实例化而不是尝试手动更改第三个精灵属性?那会更简单,更容易实现吗?如果您还没有尝试过,我可以回答一个想法:)
  • 更改精灵比重复实例化更费力吗?按照我的计划,大精灵的形象会发生多次变化。
  • 你说得有道理。如果你有很多东西要改变,实例化可能会更费力。我会考虑一下并回复你:)

标签: c# unity3d sprite


【解决方案1】:

好的,我还没有在 Unity 中测试过。

我认为您将尝试做的事情复杂化了。你想要的基本上是当精灵 1 或 2 被点击时,精灵 3 变成你刚刚点击的东西。

这是一个尝试的想法:

小卡片游戏对象:

public int cardIndex;       // indicates what the card is
public bool show;           // shows or hides card

public Sprite[] cardFronts; // collection of cards' front sides
public Sprite cardBack;     // backside of all cards

GameObject largeSprite;     //the large sprite that we will be changing!

void Start()
{
    largeSprite = GameObject.Find("Name of the large sprite"); //find our large sprite!
}


// if card is turned up, show front of card, show back otherwise
public void DisplaySprite()
{
    if (show)
        gameObject.sprite = cardFronts[cardIndex]; //or it could be gameObject.renderer.sprite, I havent tested!
    else
        gameObject.sprite = cardBack;
}

void Awake()
{
    DisplaySprite(); // show card's sprite
}

void OnMouseDown()
{
    largeSprite.sprite = gameObject.sprite; //make the large one have the same as the small one!
}

解释:

假设这可行,我已经从大游戏对象中完全删除了脚本,并将 2 个脚本合并到一个小游戏对象中。

Start() 中,我们使用GameObject.Find() 找到大型纸牌游戏对象,这样Unity 将使用您提供的名称找到您的对象并从那里开始。

OnMouseDown() 我们基本上说:

  • 如果你点击了我

  • 让大卡跟我一样!我还没有测试它,所以这条线可能有点偏离,但想法就在那里。

总而言之,这是一个附加到您的小卡片对象的脚本。就像我说的我还没有测试过,但我认为这个想法是存在的。

希望这会有所帮助! :)

【讨论】:

  • 下次有机会我会试一试,然后告诉你进展如何。时间不早了,我该准备睡觉了。感谢您的帮助!
  • 呃,我正在以某种方式破坏我的脚本或其他东西。没关系,我要试试别的。感谢汤姆的帮助。 :)
猜你喜欢
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2016-09-02
  • 2016-01-24
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多