简体   繁体   English

在运行时更改Sprite-Unity

[英]Change Sprite during runtime - Unity

Hi I am using unity and javascript to write my code for a card game. 嗨,我正在使用unity和javascript编写我的纸牌游戏代码。 After dealing the cards I want to change the players card to random cards in the deck. 发完卡后,我想将玩家卡更改为甲板上的随机卡。 I have all the sprites created in my assets folder. 我在我的Assets文件夹中创建了所有sprite。 I was wondering if there was a way to change the sprite by name or something to achieve this. 我想知道是否有一种方法可以通过名称或其他方式来更改精灵。 All the cards are called like cardDiamonds8 or cardClubs4. 所有卡的名称类似于cardDiamonds8或cardClubs4。 I have already set up to get the name: 我已经设置好了名字:

#pragma strict
public var testCard : GameObject;
private var suit;
private var value;
private var number : int;



function Start () {
    number = Random.Range(1,5);
    if (number == 1) {
        suit = "Clubs";
    } else if (number == 2) {
        suit = "Diamonds";
    } else if (number == 3) {
        suit = "Hearts";
    } else if (number == 4) {
        suit = "Spades";
    }
    number = Random.Range(1,14);
    if (number == 1) {
        value = "A";
    } else if (number == 11) {
        value = "J";
    } else if (number == 12) {
        value = "Q";
    } else if (number == 13) {
        value = "K";
    } else {
        value = number;
    }
    testCard.name = "card" + suit + value;
    print(testCard.name);
}

This just prints out the name, but the logic is there. 这只是打印出名称,但是逻辑就在那里。 Any help to change the sprite would be appreciated. 任何改变精灵的帮助将不胜感激。

One method that i would suggest is to create an object that can hold all your sprites. 我建议的一种方法是创建一个可以容纳所有精灵的对象。 Either utilize the inspector to reference the images, or use the Start() function to load them into the object via code. 利用检查器来引用图像,或使用Start()函数通过代码将其加载到对象中。 The reason for this would be to reduce the need to load assets at run time. 这样做的原因是为了减少在运行时加载资产的需求。

Once you have the sprites loaded, you can change them around at run time to you hearts content. 加载精灵后,可以在运行时更改它们以使您满意。

Look into this for loading via code: Resources.Load <Sprite>("YourImageName"); 查看此代码以通过代码进行加载: Resources.Load <Sprite>("YourImageName");

As for changing the sprite via code: you're going to need to get the component on the game objects that are displaying the sprite. 至于通过代码更改Sprite:您将需要在显示Sprite的游戏对象上获取组件。 If you're using a gui objects then you'll need to get the associated image component and change the reference of the image component. 如果您使用的是gui对象,则需要获取关联的图像组件并更改该图像组件的引用。


Update for more clarification, and to improve upon my potentially vague first response. 请进行更新以进一步说明问题,并改善我可能含糊的第一反应。

A suggestion would be the following, which should load your sprites prior to the first execution of the Update() loop. 以下是一个建议,它应该在第一次执行Update()循环之前加载您的Sprite。

public var Sprites: Sprite[];

function Start () {
     var imports : Object[] = Resources.LoadAll("path/" , Sprite);
     Sprites = new Sprite[imports.Length];
     for(var i = 0 ; i < Sprites.Length ; i++){
         Sprites[i] = imports[i];
     }
}

Once loaded, you will be able to reference your sprites by utilizing Sprites[idx] . 加载后,您将可以通过Sprites[idx]引用您的Sprites[idx]

This will let you reference your sprites on the fly by using some of the code that you learned yourself: 这将使您可以使用自己学到的一些代码即时引用自己的精灵:

 GetComponent(SpriteRenderer).sprite = Sprites[idx];

I found a way, but in order to do it you must put all your sprites that you want to use in a folder called 'Resources'. 我找到了一种方法,但是要做到这一点,必须将所有要使用的精灵放置在名为“资源”的文件夹中。 Then just use 然后使用

gameObject.GetComponent.<SpriteRenderer> ().sprite = Resources.Load("name", typeof(Sprite)) as Sprite;;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM