2016-05-29 54 views
0

我一直在努力想弄清楚如何为3个可能的位置之一指定一个正确的预制件(当前是一个按钮),另外2个位置是从一个不正确的预制件(也包括按钮)中选择的一个不正确的预制阵列。如何将随机预制分配给3个随机公共vector2之一?

由于原型是基于UI的,因此我只使用Canvas。

将不胜感激任何帮助,因为我无法想象如何这应该工作,尽管多次尝试。

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

public class Testing : MonoBehaviour { 


    //3 locations where the options are to appear 
    Vector2[] positions; 
    public Vector2 leftPosition; 
    public Vector2 middlePosition; 
    public Vector2 rightPosition; 

    //list the possible options (iconPrefab) to choose from in an array 
    public List<Transform> optionPrefab = new List<Transform>(); 
    int optionPrefab_num; 

    //state which option is correct for this scene 
    public Transform correctOption; 



void start() 
{ 
    correctOption = Random.Range (0, 2); 

    positions = new Vector2[3]; 
    leftPosition = positions [Random.Range (0, positions.Length)]; 


    middlePosition = positions [Random.Range (0, positions.Length)]; 
    rightPosition = positions [Random.Range (0, positions.Length)]; 

    Instantiate (correctOption [0], //....to one of the 3 positions listed above); 

} 
+0

你的意思是'Instantiate(new [] {leftPosition,middlePosition,rightPosition} [Random.Range(0,3)])'? – user2136963

+0

你用'位置=新的Vector2 [3];'初始化vector2';'但是你在使用它之前从未给每个位置指定值'leftPosition = positions [Random.Range(0,positions.Length)];'你期望什么将这3个值分配给位置[0],位置[1]和位置[2] ?. – Programmer

+0

他们是公共vector2列在顶部,并且团结要求我添加数字,所以我认为3代表我想要它考虑的3个职位。那么猜错了吗? – Simcha

回答

1

如果问题太难了,可以逐步解决。我想,你需要位置的字段和改造公共

实例化的东西在给定的位置:

using UnityEngine; 
using System.Collections; 

public class Spawn : MonoBehaviour 
{ 
    public Vector3 Position; 
    [SerializeField] 
    GameObject ThingToSpawn; 
    void Start() 
    { 
     if (ThingToSpawn == null) 
     { 
      Debug.LogError ("ThingToSpawn is null for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     Instantiate (ThingToSpawn,Position,Quaternion.identity); 
    } 
} 

实例化的东西在随机位置之一:

using UnityEngine; 
using System.Collections; 

public class Spawn : MonoBehaviour 
{ 
    public Vector3[] Positions; 
    [SerializeField] 
    GameObject ThingToSpawn; 
    void Start() 
    { 
     if (ThingToSpawn == null) 
     { 
      Debug.LogError ("ThingToSpawn is null for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     Instantiate (ThingToSpawn,Positions[Random.Range(0,3)],Quaternion.identity); 
    } 
} 

随机选择三个位置和实例随机一个东西。我不确定拥有一个公开的转换列表是一个很好的解决方案,但是Unity对于构造函数并不适用。 (如果你只在检查assing它,使它私人和使用[SerializeField]

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 

public class Spawn : MonoBehaviour 
{ 
    public ReadOnlyCollection<Vector3> Positions; 
    public List<Transform> Transforms=new List<Transform>(); 
    [SerializeField] 
    GameObject ThingToSpawn; 
    void Start() 
    { 
     if (ThingToSpawn == null) 
     { 
      Debug.LogError ("ThingToSpawn is null for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     if (Transforms == null) 
     { 
      Debug.LogError ("Transforms variable is null for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     if (Transforms.Count == 0) 
     { 
      Debug.LogError ("No transforms provided for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     var positionsList = 
      Enumerable.Range (0, 3) 
       .Select (i => Transforms [Random.Range (0, Transforms.Count)].position) 
       .ToList(); 
     Positions = new ReadOnlyCollection<Vector3> (positionsList); 

     Instantiate (ThingToSpawn,Positions[0],Quaternion.identity); 
    } 
} 

选择三个位置随机。在其中一个实例化任意东西。检查哪些是正确的,哪些是中间的,哪些是剩余的,但不关心重叠。

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 

public class Spawn : MonoBehaviour 
{ 
    public List<Transform> Transforms=new List<Transform>(); 
    public Vector3 LeftPosition{ get; private set;} 
    public Vector3 MiddlePosition{ get; private set;} 
    public Vector3 RightPosition{ get; private set;} 
    [SerializeField] 
    GameObject ThingToSpawn; 
    void Start() 
    { 
     if (ThingToSpawn == null) 
     { 
      Debug.LogError ("ThingToSpawn is null for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     if (Transforms == null) 
     { 
      Debug.LogError ("Transforms variable is null for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     if (Transforms.Count == 0) 
     { 
      Debug.LogError ("No transforms provided for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     var Positions = 
      Enumerable.Range (0, 3) 
       .Select (i => Transforms [Random.Range(0,Transforms.Count)].position) 
       .OrderBy (i => i.x) 
       .ToList(); 
     LeftPosition = Positions [0]; 
     MiddlePosition = Positions [1]; 
     RightPosition = Positions [2]; 
     Instantiate (ThingToSpawn,new[]{LeftPosition,MiddlePosition,RightPosition}[Random.Range(0,3)],Quaternion.identity); 
    } 
} 

和上面一样,但是要注意重叠。分为两个文件

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 

public class Spawn : MonoBehaviour 
{ 
    public List<Transform> Transforms=new List<Transform>(); 
    public Vector3 LeftPosition{ get; private set;} 
    public Vector3 MiddlePosition{ get; private set;} 
    public Vector3 RightPosition{ get; private set;} 
    [SerializeField] 
    GameObject ThingToSpawn; 
    void Start() 
    { 
     if (ThingToSpawn == null) 
     { 
      Debug.LogError ("ThingToSpawn is null for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     if (Transforms == null) 
     { 
      Debug.LogError ("Transforms variable is null for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     if (Transforms.Count <= 3) 
     { 
      Debug.LogError ("Less than 3 transforms provided for instance of Spawn.cs at "+transform.name); 
      return; 
     } 
     var Positions = 
      Enumerable.Range (0, 3) 
       .RandomShuffle() 
       .Take(3) 
       .Select (i => Transforms [i].position) 
       .OrderBy (i => i.x) 
       .ToList(); 
     LeftPosition = Positions [0]; 
     MiddlePosition = Positions [1]; 
     RightPosition = Positions [2]; 
     Instantiate (ThingToSpawn,new[]{LeftPosition,MiddlePosition,RightPosition}[Random.Range(0,3)],Quaternion.identity); 
    } 
} 

RNGUtils.cs。使用Fisher-Yates算法

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 

public static class RNGUtils 
{ 
    static void SwapListElements<T>(IList<T> list, int firstIndex, int secondIndex) 
    { 
     T tmp = list[firstIndex]; 
     list[firstIndex]=list[secondIndex]; 
     list[secondIndex]=tmp; 
    } 
    public static IList<T> RandomShuffle<T>(this IEnumerable<T> enumerable) 
    { 
     List<T> res = enumerable.ToList(); 
     for (int i = 0; i < res.Count; ++i) 
     { 
      SwapListElements (res,i,Random.Range(i,res.Count)); 
     } 
     return res; 
    } 
} 

正如你可以看到,上述两种使用语法new[]{a,b,c}[]最后的解决方案。 It is valid.您提到的错误很可能是编译器错误。他们通常可以通过搜索他们的名字和错误代码来逐个修复。

在您的代码中,您将int指定为Transform,并发现了一些可以通过查看编译器错误来解决的其他错误。我建议你从像spoj/project euler这样的网站解决几十个最简单的任务,不要犯基本错误。我每次学习新的编程语言时都会使用它们

希望这有助于!