【问题标题】:How to Access a Key In a dictionary From another script.如何从另一个脚本访问字典中的键。
【发布时间】:2015-08-17 16:23:38
【问题描述】:

我想知道如何在另一个脚本中访问字典中的键。可能吗。我有两个脚本,我想知道这是否可能。我已经在字典中添加了键,我想知道如何访问它们。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Vuforia
{
public class ZoneDictonary : MonoBehaviour 
{   
    public Vector3 Zone1V;
    public Vector3 Zone2V;
    public Vector3 Zone3V;
    public Vector3 Zone4V;
    public Vector3 Zone5V;

    void Start()
    {
        Dictionary<Vector3, bool> isZoneEmpty = new Dictionary<Vector3, bool> ();
        isZoneEmpty.Add (Zone1V, true);
        isZoneEmpty.Add (Zone2V, true);
        isZoneEmpty.Add (Zone3V, true);
        isZoneEmpty.Add (Zone4V, true);
        isZoneEmpty.Add (Zone5V, true);
    }
}
}

这是另一个脚本。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Vuforia
{
public class Test : MonoBehaviour 
{
    ZoneDictonary Zd;

    void Start()
    {
        GameObject ZD = GameObject.FindGameObjectWithTag ("Zone Manager");
        Zd = ZD.GetComponent<ZoneDictonary> ();


    }
}

}

【问题讨论】:

    标签: c# c#-4.0 dictionary unity3d unityscript


    【解决方案1】:

    您遇到了轻微的范围问题,但这是您访问密钥的方式:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    namespace Vuforia
    {
        public class ZoneDictonary : MonoBehaviour 
        {   
            public Vector3 Zone1V;
            public Vector3 Zone2V;
            public Vector3 Zone3V;
            public Vector3 Zone4V;
            public Vector3 Zone5V;
    
            // Dictionary must be here to access from outside of this script
            public Dictionary<Vector3, bool> isZoneEmpty = new Dictionary<Vector3, bool> ();
    
            void Start()
            {
                isZoneEmpty.Add (Zone1V, true);
                isZoneEmpty.Add (Zone2V, true);
                isZoneEmpty.Add (Zone3V, true);
                isZoneEmpty.Add (Zone4V, true);
                isZoneEmpty.Add (Zone5V, true);
            }
        }
    }
    

    ...

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    namespace Vuforia
    {
        public class Test : MonoBehaviour 
        {
            ZoneDictonary Zd;
    
            void Start()
            {
                GameObject ZD = GameObject.FindGameObjectWithTag ("Zone Manager");
                Zd = ZD.GetComponent<ZoneDictonary> ();
    
                foreach(Vector3 key in Zd.isZoneEmpty.Keys)
                {
                    //do something with keys
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      是的,这是可能的,但您必须使用 public 关键字将您的字典公开为 ZoneDictonary 类的公共成员。

      但是,我建议将其作为属性公开并保持变量私有,而不是仅公开变量。这有很多原因。 This answer on PSE详细列举几个。

      private Dictionary<Vector3, bool> _isZoneEmpty = new Dictionary<Vector3, bool> ();
      
      public Dictionary<Vector3, bool> IsZoneEmpty { 
          get
          {
              return _isZoneEmpty;
          }
          set 
          {
              _isZoneEmpty = value;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-26
        • 2019-09-01
        • 2022-12-20
        • 2020-10-09
        • 1970-01-01
        • 2018-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多