【问题标题】:Random Shuffle Listing in Unity 3DUnity 3D 中的随机随机播放列表
【发布时间】:2014-08-29 22:58:42
【问题描述】:

我有点吃不消了。我有一个显示项目列表的数组脚本。现在的问题是我只希望这个列表显示十个项目中的五个并且还洗牌,所以你不能每次开始一个新游戏时都有相同的列表。我在想是否应该实施 Random.Range 但我不知道在哪里。请帮助和感谢。这是脚本:`

public class RayCasting : MonoBehaviour
{
    public float pickupDistance;
    public List<Item> items;


    #region Unity
    void Start ()
    {
        Screen.lockCursor = true;
    }
    void Update ()
    {
        RaycastHit hit;
        Ray ray = new Ray(transform.position, transform.forward);
        if (Physics.Raycast(ray, out hit, pickupDistance))
        {
            foreach(Item item in items)
            {

                if(Input.GetMouseButtonDown(0)) {
                    if (item.gameObject.Equals(hit.collider.gameObject))
                {
                    numItemsCollected++;
                    item.Collect();
                    break;
                        }
                }
            }
        }
    }

    void OnGUI()
    {

        GUILayout.BeginArea(new Rect(130,400,100,100));
        {
            GUILayout.BeginVertical();
            {
        if (numItemsCollected < items.Count)
        {
            foreach (Item item in items)

                        GUILayout.Label(string.Format("[{0}] {1}", item.Collected ? "X" : " ", item.name));
        }
        else
        {
            GUILayout.Label("You Win!");
        }
            }
            GUILayout.EndVertical();
    }
        GUILayout.EndArea();
    }
    #endregion

    #region Private
    private int numItemsCollected;
    #endregion
}

[System.Serializable]
public class Item
{
    public string name;
    public GameObject gameObject;

    public bool Collected { get; private set; }

    public void Collect()
    {
        Collected = true;
        gameObject.SetActive(false);
    }
}

`

【问题讨论】:

  • 如果你想真正洗牌,按照Fisher-Yates算法,你可以在Wiki上找到。如果您只是想要一些随机性,请使用 OrderByRandom.Nextnew Guid。同样的问题也有大约 15 个重复项,请选择一个。
  • 我还是个新手,还在学习中,所以如果你能详细说明一下,我搜索了很多,我没有找到任何带有数组和列表在数组中被打乱。

标签: c# list random shuffle


【解决方案1】:

要从 10 项列表中随机获取 5 项,您可以使用:

List<Items> AllItems = new List<Items>();

List<Items> RandomItems = new List<Items>();

Random random = new Random();

for(int i = 0; i < 5; i++)
{
    RandomItems.Add(AllItems[random.Next(0, AllItems.Count + 1)]);
}

AllItems 列表包含您的 10 项。

循环之后,您将在 RandomItems 列表中拥有 5 个随机项目。

【讨论】:

  • 所以我应该替换我的公共 List 项目;与您的 List AllItems = new List();
  • 您的项目列表可以保持不变。你只需要用你想要的 5 个随机项目来填充它。因此,在我的示例中,您将 RandomItems 替换为您的项目列表。
  • random.Next...Next 不符合,它说它不是变量。
  • 您必须在系统中创建一个可用的 Random 类的实例;图书馆。 Unity3D 内置的 Random 类只包含静态方法,不包含 Next 方法。因此,在使用 UnityEngine 的代码之上;添加行:使用系统;添加后替换我的代码行: Random random = new Random(); with System.Random random = new System.Random();
  • 使用系统一次输入;它取消了 Random 并且没有改变 Next...我想只是把 uning Systems;还是还有更多?
【解决方案2】:

刚刚想通了,这是我洗牌任何类型列表的解决方案

public class Ext : MonoBehaviour
{
    public static List<T> Shuffle<T>(List<T> _list)
    {
        for (int i = 0; i < _list.Count; i++)
        {
            T temp = _list[i];
            int randomIndex = Random.Range(i, _list.Count);
            _list[i] = _list[randomIndex];
            _list[randomIndex] = temp;
        }

        return _list;
    }
}

你的例子应该是这样的:

AllItems = Ext.Shuffle<Items>(AllItems);
Debug.Log(AllItems[0]); // Will be always random Item

如果你需要5个随机物品,你可以打电话

Debug.Log(AllItems[0]); // Random Item
Debug.Log(AllItems[1]); // Random Item
Debug.Log(AllItems[2]); // Random Item
Debug.Log(AllItems[3]); // Random Item
Debug.Log(AllItems[4]); // Random Item

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-23
    • 2014-04-02
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多