Public Unity 中的字典

Public Dictionary in Unity

我如何制作 public 字典,我在 Unity 中的其他脚本和对象中使用什么?我在对象和脚本 A 中有字典,我想使用 script/object B 中的命令向该字典添加值。

*例如A对象中的脚本:*

public Dictionary<string, GameObject> dictio  = new Dictionary<string, GameObject>();

*例如 B 对象中的 B 脚本:*

答:

public Dictionary<string, GameObject> obstacleDictionary;

GameObject gob;

void Start()
{
    obstacleDictionary = new Dictionary<string, GameObject>();
    pos = transform.position;

}

乙:

Vector3 pos;
    public string openSide;
    GameObject player;
    string posString;
void Start ()
{
    player = GameObject.FindGameObjectWithTag("Player");
    pos = transform.position;
    posString = pos.x + "_" + pos.y;


    player.GetComponent<PlayerMove>().obstacleDictionary.Add(posString, gameObject); //warning

    //print(player.GetComponent<PlayerMove>().obstacleDictionary.ContainsKey("3_0"));
}

A.GetComponent<AScript>().dictio.Add("aaa", gameObject);

你能帮帮我吗?谢谢 :)

我不知道什么对你不起作用,因为你没有提到什么有效或无效。这是一个完整的例子:

你的 ScriptA 与 public 词典:

public class ScriptA : MonoBehaviour{
    public Dictionary<string, GameObject> dictio = new Dictionary<string, GameObject>();
}

您可以从 ScriptB 访问它:

public class ScriptB : MonoBehaviour{

    ScriptA scriptInstance = null;  

    void Start()
    {
      GameObject tempObj = GameObject.Find("NameOfGameObjectScriptAIsAttachedTo");
      scriptInstance = tempObj.GetComponent<ScriptA>();

      //Access dictio  variable from ScriptA
      scriptInstance.dictio.Add("aaa", gameObject);
    }
}

确保将 NameOfGameObjectScriptAIsAttachedTo 替换为 ScriptA 附加到的游戏对象的名称。

编辑:

查看了您的项目并发现了问题。 obstacleDictionaryStart() 函数中初始化之前正在从另一个脚本中使用..

解决此问题的三种方法:

在你的 PlayerMove.cs、

1.替换public Dictionary<string, GameObject> obstacleDictionary;

public Dictionary<string, GameObject> obstacleDictionary = new Dictionary<string, GameObject>();

然后从 Start() 函数中删除 obstacleDictionary = new Dictionary<string, GameObject>();

如果您按照我最初的回答去做,但您没有这样做,那应该会奏效。您所要做的就是在 Start() 函数之外创建新实例。

2。您可以将 obstacleDictionary = new Dictionary<string, GameObject>();Start() 函数移动到 Awake() 函数,它仍然有效。

您的新密码:

public class PlayerMove : MonoBehaviour
{

    Vector3 pos;
    public float speed;
    public static bool inside;
    public Dictionary<string, GameObject> obstacleDictionary;

    GameObject gob;

    void Awake()
    {
        obstacleDictionary = new Dictionary<string, GameObject>();
    }

    void Start()
    {
        pos = transform.position;
    }

    void Update()
    {
        gob = GetObjectAt(transform.position);
        if (gob == null) inside = false;
        else inside = true;
        print(inside);


        #region Control
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            GameObject go = GetObjectAt(transform.position - new Vector3(speed, 0, 0));
            if (go == null || go.GetComponent<ObstacleMove>().openSide == "right")
            {
                if (inside && gob.GetComponent<ObstacleMove>().openSide == "left") inside = false;
                /*if(inside && gob.CompareTag("BigRed") && go.CompareTag("BigRed")) { }
                else */
                pos.x -= speed;
            }
        }

        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            GameObject go = GetObjectAt(transform.position + new Vector3(speed, 0, 0));
            if (go == null || go.GetComponent<ObstacleMove>().openSide == "left")
            {
                if (inside && gob.GetComponent<ObstacleMove>().openSide == "right") inside = false;
                pos.x += speed;
            }
        }

        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            GameObject go = GetObjectAt(transform.position - new Vector3(0, speed, 0));
            if (go == null || go.GetComponent<ObstacleMove>().openSide == "up")
            {
                if (inside && gob.GetComponent<ObstacleMove>().openSide == "down") inside = false;
                pos.y -= speed;
            }
        }

        else if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            GameObject go = GetObjectAt(transform.position + new Vector3(0, speed, 0));
            if (go == null || go.GetComponent<ObstacleMove>().openSide == "down")
            {
                if (inside && gob.GetComponent<ObstacleMove>().openSide == "up") inside = false;
                pos.y += speed;
            }
        }
        #endregion

        transform.position = pos;

        if (inside && gob.transform.position != transform.position)
        {
            print("change");
            ChangeObstacleDictionary(gob.transform.position, transform.position);
            gob.transform.position = transform.position;

        }
    }

    public GameObject GetObjectAt(Vector3 position)
    {
        string pos = position.x + "_" + position.y;
        if (obstacleDictionary.ContainsKey(pos) == true)
        {
            print(obstacleDictionary[pos]);
            return obstacleDictionary[pos];
        }

        else return null;
    }

    public void ChangeObstacleDictionary(Vector3 lastPosition, Vector3 newPos)
    {

        string lastPosString = lastPosition.x + "_" + lastPosition.y;
        string newPosString = newPos.x + "_" + newPos.y;

        //print("test" + lastPosString + " " + newPosString);
        if (lastPosString != newPosString)
        {
            obstacleDictionary.Remove(lastPosString);
            obstacleDictionary.Add(newPosString, gob);
        }
    }

}

3。更改脚本的 执行顺序 并使 PlayerMove 在其他脚本之前先执行。

然后