如果问题太难了,可以逐步解决。我想,你需要位置的字段和改造公共
实例化的东西在给定的位置:
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这样的网站解决几十个最简单的任务,不要犯基本错误。我每次学习新的编程语言时都会使用它们
希望这有助于!
你的意思是'Instantiate(new [] {leftPosition,middlePosition,rightPosition} [Random.Range(0,3)])'? – user2136963
你用'位置=新的Vector2 [3];'初始化vector2';'但是你在使用它之前从未给每个位置指定值'leftPosition = positions [Random.Range(0,positions.Length)];'你期望什么将这3个值分配给位置[0],位置[1]和位置[2] ?. – Programmer
他们是公共vector2列在顶部,并且团结要求我添加数字,所以我认为3代表我想要它考虑的3个职位。那么猜错了吗? – Simcha