我正在创建一个数字棋盘游戏,它由多个棋盘组成,游戏中的棋子由玩家移动。
游戏板的脚本需要一个2D的位置数组来知道当一个游戏被接收到时,它应该在哪里移动。目前,为了在游戏板上标记位置,我将占位符游戏对象添加到预置文件中,并将它们命名为"spawnpoint\d".In --我使用Transform.Find()搜索这些游戏对象的Awake()方法。然后,在我保存了他们的位置后,我叫他们Destroy(),这样他们就不会出现在游戏中。
我看到两个问题:
Transform.Find()是社区专家们讨论的重点。我希望将spawnpoint位置存储在静态数组中,因此所有实例都引用相同的数据。此外,我希望通过视觉帮助轻松地修改编辑器中的这些职位。
我尝试过序列化静态成员,但这些成员在编辑器中无法修改。
[SerializeField]
public static int TestNumber;TLDR:如何使静态成员从统一编辑器?中可视地变化
发布于 2021-04-03 10:05:44
tl;dr 您不能,static字段没有序列化。
你可以这样做。
[SerializeField] private Transform[] spawnPoints;
public static Transform[] SpawnPoints;
private void Awake ()
{
SpawnPoints = spawnPoints;
}总的来说,我建议使用这样的方法:
// Simply attach this class to each GameObject that shall be a spawn point
// MAKE SURE IT IS ACTIVE AND ENABLED BY DEFAULT
public class SpawnPoint : MonoBehaviour
{
// Each SpawnPoint (un)registers itself here
private static readonly HasSet<SpawnPoint> _instances = new HashSet<SpawnPoint>();
// For the public return a new HashSet to make sure nobody can modify the
// original _instances from the outside
public static HashSet<SpawnPoint>() Instances => new HashSet<SpawnPoint>(_instancea);
private void Awake()
{
// Register yourself to the existing instances
_instances.Add(this);
// Optional: make sure this object is not destroyed when a new scene is loaded
DontDestroyOnLoad (gameObject);
// simply hide the entire gameObject
gameObject.SetActive(false);
}
private void Destroy ()
{
// Unregister yourself from the instances
_instances.Remove(this);
}
}这边请
DontDestroyOnLoad,则它们将被销毁并自动从实例中删除)Find或FindObjectsOfType等昂贵的东西,而只需通过该属性即可。var availableSpawnPoints =availableSpawnPoints
https://stackoverflow.com/questions/66928567
复制相似问题