Unity中的公共字典

时间:2016-06-18 21:27:25

标签: c# dictionary unity3d unityscript

我如何制作公共字典,我在Unity中的其他脚本和对象中使用了什么?我在对象和脚本 A 中有字典,我希望在脚本/对象 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;

}

B:

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);
你能帮帮我吗?谢谢 :)

1 个答案:

答案 0 :(得分:1)

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

您的ScriptA公共字典:

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替换为附加的GameObject ScriptA的名称。

修改

查看您的项目并发现问题。在obstacleDictionary函数初始化之前,Start()正在使用另一个脚本。

解决此问题的三种方法:

在你的PlayerMove.cs中,

1 。重新定位public Dictionary<string, GameObject> obstacleDictionary;

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

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

如果你按照我最初的答案但它没有,那应该有用。您所要做的就是在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先执行,然后再执行其他操作。 enter image description here

然后

enter image description here