简体   繁体   English

将随机 Integer 分配给游戏 Object 以便在 Unity 中引用

[英]Assign A Random Integer To A Game Object For Referencing In Unity

Due to my inept ability to think, I've ran into a bit of a conundrum.由于我笨拙的思考能力,我遇到了一些难题。 When developing a card based system, I'm trying to assign each card an integer which can be used to stack the deck, instead of doing a random card per draw.在开发基于卡片的系统时,我试图为每张卡片分配一个 integer,它可用于堆叠牌组,而不是每次抽取一张随机卡片。 Issue is, I have no clue how to assign a card a random number on every Start.问题是,我不知道如何在每次开始时为卡片分配一个随机数。 I've come up with a...I would say...overcomplicated design, which I've left below.我想出了一个……我会说……过于复杂的设计,我把它留在了下面。 If anyone would be willing to point me in the right direction on how I would go about this, I would appreciate it.如果有人愿意为我指出正确的方向,我将如何处理 go,我将不胜感激。 Apologies for this being a more...complex task, but I've searched everywhere for a solution, and cannot find it.抱歉,这是一项更……复杂的任务,但我到处寻找解决方案,但找不到。 Maybe I'm overthinking it.也许我想多了。 Who knows.谁知道。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DeckOrder : MonoBehaviour
{
    //too many objects, maybe a way to simplify this process
    public GameObject card1;
    public GameObject card2;
    public GameObject card3;
    public GameObject card4;
    public GameObject card5;
    public GameObject card6;
    public GameObject card7;
    public GameObject card8;
    public GameObject card9;
    public GameObject card10;
    public GameObject card11;
    public GameObject card12;
    public GameObject card13;
    public GameObject card14;
    public GameObject card15;

    int randomNumber1;
    int randomNumber2;
    int randomNumber3;
    int randomNumber4;
    int randomNumber5;
    int randomNumber6;
    int randomNumber7;
    int randomNumber8;
    int randomNumber9;
    int randomNumber10;
    int randomNumber11;
    int randomNumber12;
    int randomNumber13;
    int randomNumber14;
    int randomNumber15;

    void Start()
    {
        //set card order
        randomNumber1 = Random.Range(1, 15);
            //beneath each would be a code telling it not to repeat that number it chose for the rest of these
        randomNumber2 = Random.Range(1, 15);
        randomNumber3 = Random.Range(1, 15);
        randomNumber4 = Random.Range(1, 15);
        randomNumber5 = Random.Range(1, 15);
        randomNumber6 = Random.Range(1, 15);
        randomNumber7 = Random.Range(1, 15);
        randomNumber8 = Random.Range(1, 15);
        randomNumber9 = Random.Range(1, 15);
        randomNumber10 = Random.Range(1, 15);
        randomNumber11 = Random.Range(1, 15);
        randomNumber12 = Random.Range(1, 15);
        randomNumber13 = Random.Range(1, 15);
        randomNumber14 = Random.Range(1, 15);
        randomNumber15 = Random.Range(1, 15);

        //this doesnt work, GameObject can't be converted to int or vise versa
        card1 = randomNumber1;
        card2 = randomNumber2;
        card3 = randomNumber3;
        card4 = randomNumber4;
        card5 = randomNumber5;
        card6 = randomNumber6;
        card7 = randomNumber7;
        card8 = randomNumber8;
        card9 = randomNumber9;
        card10 = randomNumber10;
        card11 = randomNumber11;
        card12 = randomNumber12;
        card13 = randomNumber13;
        card14 = randomNumber14;
        card15 = randomNumber15
    }
}

You should really use an array and then you could use Linq to simply shuffle the deck everytime like eg你真的应该使用一个数组,然后你可以使用 Linq 每次都简单地洗牌,比如

using System.Linq;
using UnityEngine;

public class DeckOrder : MonoBehaviour
{
    public GameObject[] availableCards;

    public GameObject[] deck;

    private void Start ()
    {
        deck = availableCards.OrderBy(c => Random.value).ToArray();
    }
}

Now simply drag all your cards onto the name of the array availableCards -> they will all be added to the array in the Inspector.现在只需将所有卡片拖到数组availableCards的名称上 -> 它们将全部添加到检查器中的数组中。

Then on every start the deck will be filled in random order.然后在每次开始时, deck将以随机顺序填充。


For then eg taking only a few of them you can either use Take like eg然后,例如只取其中的一些,您可以使用Take like eg

deck = availableCards.OrderBy(c => Random.value).Take(5).ToArray();

now deck will only have picked the first random 5 cards.现在牌组只会随机挑选前 5 张牌。

Or in general since your deck is probably about drawing cards one by one I would actually suggest to implement it as a Queue so you can simply do或者一般来说,因为你的套牌可能是一张一张地抽牌,我实际上建议将它实现为一个Queue ,这样你就可以简单地做

private Queue<GameObject> deck;

and then然后

deck = new Queue<GameObject>(availableCards.OrderBy(c => Random.value));

and now whenever you want to draw a card do eg现在每当你想抽一张牌时,比如

public bool TryDrawCard(out GameObject card)
{
    if(deck.Count > 0)
    {
        card = deck.Dequeue();
        return true;    
    }

    card = null;
    return false;
}

and then you can simply loop until you have picked your desired amount of cards.然后你可以简单地循环,直到你选择了你想要的卡片数量。

In the beginning, everyone who entered this field was writing codes like you in the beginning and this is not a mistake, but the mistake lies in your staying at this low level一开始进入这个领域的人一开始都是和你一样写代码的,这不是错误,错误在于你停留在这个低水平

As for your question, it's not very clear but I will give you the basics so you can fix your code至于你的问题,不是很清楚,但我会给你一些基础知识,这样你就可以修复你的代码

If you have more than one variable with different values, you should use List Here you will find a toturial to explain how to use the list and where to use it如果你有多个不同值的变量,你应该使用List Here 你会找到一个教程来解释如何使用列表以及在哪里使用它

Here is a simple example of a List这是一个简单的列表示例

[System.Serializable]
public struct Informaiton
{
    public string name;
    public int age;
    public int number;
}
public List<Informaiton> MyInformaiton = new List<Informaiton>();

public void Start()
{
    Informaiton _informaiton_ = new Informaiton();
    _informaiton_.name = "lelouch";
    _informaiton_.age = 17;
    _informaiton_.number = 1245836985;
    MyInformaiton.Add(_informaiton_);
}

I advise you to write this code and try to add things to it and remove things from it to adapt to the list more我建议您编写此代码并尝试向其中添加内容并从中删除内容以适应更多列表

Don't give up on the first try, nothing comes easy不要放弃第一次尝试,没有什么是容易

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

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