游戏上下文菜单中的Unity3D-奇怪的行为

游戏上下文菜单中的Unity3D-奇怪的行为,unity3d,Unity3d,从另一个对象向字典添加操作时会发生什么 首先,我正在尝试设计一些像样的游戏内上下文菜单。我的目标是动态生成每个项目。每个项都是从存储操作的字典中加载的。字典可以从每个游戏对象的最多3个组件访问,并附带一个游戏部件组件 首先,有一个字典,其中动作是每种类型游戏的组成部分: public class GamePiece : MonoBehaviour { protected bool rightClickable = true; protected StatManager sta

从另一个对象向字典添加操作时会发生什么

首先,我正在尝试设计一些像样的游戏内上下文菜单。我的目标是动态生成每个项目。每个项都是从存储操作的字典中加载的。字典可以从每个游戏对象的最多3个组件访问,并附带一个游戏部件组件

首先,有一个字典,其中动作是每种类型游戏的组成部分:

public class GamePiece : MonoBehaviour {

    protected bool rightClickable = true;

    protected StatManager statManager;
    Transform ui;
    CanvasManager canvas;
    SpriteRenderer sprite;
    Color spriteColor;

    public Dictionary<string, Action> actions;

    void Awake(){
        statManager = GameObject.Find("StatPanel").GetComponent<StatManager>();
        actions = new Dictionary<string, Action>();
        actions.Add("Deconstruct", Deconstruct);
    }

因此,当调用此函数时,它会将对自身的引用传递给CanvasManager,然后从ContextMenu中退出。

在调用之前将操作存储在本地,这样就可以开始了

在上下文菜单中:

Action newAction =kVp.Value;
menuItem.GetComponent<Button>().onClick.AddListener(() => {newAction.Invoke();});
Action newAction=kVp.Value;
menuItem.GetComponent().onClick.AddListener(()=>{newAction.Invoke();});

就是这样。谢谢
public class ContextMenu : MonoBehaviour {
    public Transform menuItem;

    //Takes a ref from the calling gameObject, t. And is called from CanvasManager.cs
    public void PopulateContextMenu(GameObject t)
    {
        Transform selections = transform.FindChild("Selections").transform; //Parent for items.

        //gamePiece holds the dictionary.
        GamePiece gamePiece = t.GetComponent<GamePiece>();
        foreach (KeyValuePair<string, Action> kVp in gamePiece.actions)
        {
            GameObject menuItem =
             (GameObject)Instantiate(Resources.Load("MenuItem"));
            menuItem.name = kVp.Key;
            menuItem.GetComponent<Text>().text = kVp.Key;

            //Adding functuionality.
            menuItem.GetComponent<Button>().onClick.AddListener
              (() => { kVp.Value.Invoke(); });

            menuItem.GetComponent<Button>().onClick.AddListener
              (() => { CloseContextMenu(); });

            menuItem.transform.SetParent(selections, false);
        }
    }

    public void CloseContextMenu()
    {
        Destroy(this.gameObject);
    }
}
public class CanvasManager : MonoBehaviour {

    public void ToggleContextMenu(GameObject t) {
        GameObject newMenu = (GameObject)Resources.Load("ContextMenu");
            newMenu = Instantiate(newMenu) as GameObject;
            //Passing gameObject t into PopulatContextMenu
            newMenu.GetComponent<ContextMenu>().PopulateContextMenu(t);
    }

}
public class GamePiece : MonoBehaviour {

    void OnMouseOver(){
        if (Input.GetMouseButtonDown(1) && rightClickable) {
            canvas.ToggleContextMenu(this.gameObject);
        }
    }
}
Action newAction =kVp.Value;
menuItem.GetComponent<Button>().onClick.AddListener(() => {newAction.Invoke();});