【问题标题】:Randomly select image to display on random button in c# unity随机选择图像以显示在 c# unity中的随机按钮上
【发布时间】:2016-08-01 23:50:33
【问题描述】:

我想显示 4 张随机图像,其中一张是正确的图像。所以我从“correct_caps”文件夹中选择了 1 个随机图像,从另一个文件夹中选择了 3 个。现在,我想选择随机图像变量来显示这些图像,这样我的正确图像总是会改变它的位置。

using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;



 public class NewBehaviourScript : MonoBehaviour
  {
 public Image art1;
 public Image art2;
 public Image art3;
 public Image art4;


 public int x;
 public int x1;
 public int x2;
 public int x3;

 public string s;
 public string path;
 public string path1;
 public string path2;
 public string path3;
 public string path4;

 public int cl;
 public int i;


 void Start()
 {


     //selection of correct letter
     cl = Random.Range(1, 26);

     path1 = "Capital Letters/correct_caps/" + cl;
     path = "Capital Letters/" + cl;
     Debug.Log("Load path is " + path1);


     x1 = Random.Range(1, 5);
     path2 = path + "/" + x1;



     x2 = Random.Range(1, 5);
     path3 = path + "/" + x2;
     i = 0;
     while (i == 0)
     {
         if (x1 == x2)
         {

             x2 = Random.Range(1, 5);
             path3 = path + "/" + x2;
             Debug.Log("Load path3 is " + path3);

         }
         else
         {

             Debug.Log("Load path3 is " + path3);
             i++;
         }
     }




     x3 = Random.Range(1, 5);
     path4 = path + "/" + x3;

     i = 0;
     while (i == 0)
     {
         if (x1 == x3 || x2 == x3)
         {
             x3 = Random.Range(1, 5);
             path4 = path + "/" + x3;
             Debug.Log("Load path4 is " + path4);
         }
         else
         {

             Debug.Log("Load path4 is " + path4);
             i++;
         }
     }

  //here I displayed images . But, I want to select art,art1,art2,art3 randomly.

   art.sprite = Resources.Load<Sprite>(path1) as Sprite; 
    art1.sprite = Resources.Load<Sprite>(path2) as Sprite;
     art2.sprite = Resources.Load<Sprite>(path3) as Sprite;
    art3.sprite = Resources.Load<Sprite>(path4) as Sprite;  
 }    

 }

【问题讨论】:

    标签: c# image button random unity5


    【解决方案1】:

    在 1-4 之间做一个随机数,然后使用 if 语句。

     Random random = new Random();
     int randomNumber = random.Next(0, 5);
     if (randomNumber ==1)
     {
          art.sprite = Resources.Load<Sprite>(path1) as Sprite; 
     }
     if (randomNumber ==2)
     {
          art1.sprite = Resources.Load<Sprite>(path2) as Sprite; 
     }
     if (randomNumber ==3)
     {
          art2.sprite = Resources.Load<Sprite>(path3) as Sprite; 
     }
     if (randomNumber ==4)
     {
          art3.sprite = Resources.Load<Sprite>(path4) as Sprite; 
     }
    

    【讨论】:

    • 这里的 path1 只是我的正确字母。始终在“艺术”本身上显示正确的字母(路径 1)对于游戏来说并不是一个好主意。
    【解决方案2】:

    要通过资源加载图像。加载图像必须在“资源”文件夹中。如果你愿意,你可以把它放在子文件夹中。

    [这里] (http://docs.unity3d.com/ScriptReference/Resources.Load.html) 是文档

    【讨论】:

      猜你喜欢
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 2021-03-10
      • 2018-02-19
      相关资源
      最近更新 更多