限制令牌移动到 Unity3D 中的邻近区域
Restrict token movements to neighboring areas in Unity3D
我在我的(TEG/RISK 崇拜者)2D 游戏中的地图上有一个标记,我将地图划分为 16 个不同的区域,每个区域都有一个碰撞器,这样我就可以进行点击操作他们并将令牌移向它。我能够进行基本操作:令牌位置 = 区域位置,并且效果很好。当我尝试将算法限制到令牌的邻近区域时,问题就来了,我遇到了很多错误,当我得到 none 时,它不起作用。
脚本有 200~ 行长(是的,很确定是 -100% 优化的)所以我要缩短一些部分。它应该附加到 16 个区域中的每一个区域。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Movimiento : MonoBehaviour {
//Prefabs of each zone
public Transform OBJ_ZONA_1;
public Transform OBJ_ZONA_2;
.
.
.
public Transform OBJ_ZONA_16;
//List of zones prefabs to optimize searchs
public List<Transform> LISTA_OBJ_ZONAS;
//List of each zone's neighbors
public List<int> LISTA_VECINOS_ZONA_1;
public List<int> LISTA_VECINOS_ZONA_2;
.
.
.
public List<int> LISTA_VECINOS_ZONA_16;
//List of lists of each zone's neighbors to optimize searchs
public List<List<int>> LISTA_LISTAS_VECINOS;
//The token object
public Transform FICHA;
public void MoverFicha(){
Vector3 TP = FICHA.transform.position;
TP.x = this.transform.position.x;
TP.y = this.transform.position.y;
FICHA.transform.position = TP;
}
//Function to clarify the neighbors of the token at the momment
public List<int> Vecinos(){
for (int i=0; i<15; i++) {
if (FICHA.transform.position.x == LISTA_OBJ_ZONAS[i].transform.position.x && FICHA.transform.position.y == LISTA_OBJ_ZONAS[i].transform.position.y)
return LISTA_LISTAS_VECINOS[i];
}
return LISTA_VECINOS_ZONA_1;
}
//Function to define the zone that got triggered by the click
public int DefinirZonaClick(){
for (int i=0; i<15; i++) {
if (this.transform.position.x == LISTA_OBJ_ZONAS[i].transform.position.x && this.transform.position.y == LISTA_OBJ_ZONAS[i].transform.position.y)
return i+1;
}
return 1;
}
void Awake(){
//Fill the list of zone objects
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_1);
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_2);
.
.
.
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_16);
//Clarify the neighbors of each zone
LISTA_VECINOS_ZONA_1.Add (2);
LISTA_VECINOS_ZONA_2.Add (1);
.
.
.
LISTA_VECINOS_ZONA_16.Add (15);
}
// Use this for initialization
void Start () {
//Fill the list of lists of neighbors of each zone
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_1);
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_2);
.
.
.
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_16);
}
// Update is called once per frame
void Update () {
//Define the list of neighbors for the token at the momment of trigger
List<int> VECINOS = Vecinos ();
//Define the clicked zone
int INT_ZONA_CLICK = DefinirZonaClick ();
//If the triggered zone is in the token's neighbors list then it moves towards that zone
if (VECINOS.Contains (INT_ZONA_CLICK))
MoverFicha ();
}
}
这是我的设置:
这些是我目前遇到的错误:
您正在声明 LISTA_VECINOS_ZONA_X
等,但您实际上并没有给他们一个 value/creating 对象的实例(或者这是我可以从提供的代码中推断出来的)。因此,当您将它们添加到 LISTA_LISTAS_VECINOS
时,您会收到 NullReference 错误。
先创建 LISTA_VECINOS_ZONA_X
的实例,然后再将它们添加到方法 Start()
的列表中。
编辑:
您的列表列表已在您的代码中声明,但我没有看到它实际被实例化。这应该是声明的位置。
public List<List<int>> LISTA_LISTAS_VECINOS = new List<List<int>>();
我在我的(TEG/RISK 崇拜者)2D 游戏中的地图上有一个标记,我将地图划分为 16 个不同的区域,每个区域都有一个碰撞器,这样我就可以进行点击操作他们并将令牌移向它。我能够进行基本操作:令牌位置 = 区域位置,并且效果很好。当我尝试将算法限制到令牌的邻近区域时,问题就来了,我遇到了很多错误,当我得到 none 时,它不起作用。
脚本有 200~ 行长(是的,很确定是 -100% 优化的)所以我要缩短一些部分。它应该附加到 16 个区域中的每一个区域。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Movimiento : MonoBehaviour {
//Prefabs of each zone
public Transform OBJ_ZONA_1;
public Transform OBJ_ZONA_2;
.
.
.
public Transform OBJ_ZONA_16;
//List of zones prefabs to optimize searchs
public List<Transform> LISTA_OBJ_ZONAS;
//List of each zone's neighbors
public List<int> LISTA_VECINOS_ZONA_1;
public List<int> LISTA_VECINOS_ZONA_2;
.
.
.
public List<int> LISTA_VECINOS_ZONA_16;
//List of lists of each zone's neighbors to optimize searchs
public List<List<int>> LISTA_LISTAS_VECINOS;
//The token object
public Transform FICHA;
public void MoverFicha(){
Vector3 TP = FICHA.transform.position;
TP.x = this.transform.position.x;
TP.y = this.transform.position.y;
FICHA.transform.position = TP;
}
//Function to clarify the neighbors of the token at the momment
public List<int> Vecinos(){
for (int i=0; i<15; i++) {
if (FICHA.transform.position.x == LISTA_OBJ_ZONAS[i].transform.position.x && FICHA.transform.position.y == LISTA_OBJ_ZONAS[i].transform.position.y)
return LISTA_LISTAS_VECINOS[i];
}
return LISTA_VECINOS_ZONA_1;
}
//Function to define the zone that got triggered by the click
public int DefinirZonaClick(){
for (int i=0; i<15; i++) {
if (this.transform.position.x == LISTA_OBJ_ZONAS[i].transform.position.x && this.transform.position.y == LISTA_OBJ_ZONAS[i].transform.position.y)
return i+1;
}
return 1;
}
void Awake(){
//Fill the list of zone objects
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_1);
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_2);
.
.
.
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_16);
//Clarify the neighbors of each zone
LISTA_VECINOS_ZONA_1.Add (2);
LISTA_VECINOS_ZONA_2.Add (1);
.
.
.
LISTA_VECINOS_ZONA_16.Add (15);
}
// Use this for initialization
void Start () {
//Fill the list of lists of neighbors of each zone
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_1);
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_2);
.
.
.
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_16);
}
// Update is called once per frame
void Update () {
//Define the list of neighbors for the token at the momment of trigger
List<int> VECINOS = Vecinos ();
//Define the clicked zone
int INT_ZONA_CLICK = DefinirZonaClick ();
//If the triggered zone is in the token's neighbors list then it moves towards that zone
if (VECINOS.Contains (INT_ZONA_CLICK))
MoverFicha ();
}
}
这是我的设置:
这些是我目前遇到的错误:
您正在声明 LISTA_VECINOS_ZONA_X
等,但您实际上并没有给他们一个 value/creating 对象的实例(或者这是我可以从提供的代码中推断出来的)。因此,当您将它们添加到 LISTA_LISTAS_VECINOS
时,您会收到 NullReference 错误。
先创建 LISTA_VECINOS_ZONA_X
的实例,然后再将它们添加到方法 Start()
的列表中。
编辑:
您的列表列表已在您的代码中声明,但我没有看到它实际被实例化。这应该是声明的位置。
public List<List<int>> LISTA_LISTAS_VECINOS = new List<List<int>>();