我根本不认为我理解词典。我理解如何设置它们并从同一个脚本/对象访问它们但是当从不同的对象/脚本访问它时,我不知道我在做什么。这是我持有词典的游戏对象的脚本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class HeroStats : MonoBehaviour {
//Dictionary Structure <Key, value>
Dictionary<string,int> HeroStat = new Dictionary<string,int>();
Dictionary<string,string>HeroName = new Dictionary<string,string>();
Dictionary<string,string>StatDef = new Dictionary<string,string>();
//Initialization:
void Start(){
//===NAME==========================
HeroName.Add("Name","Insert Name");
//===STATS=========================
HeroStat.Add("Constitution", 3);
HeroStat.Add("Dexterity", 3);
HeroStat.Add("Intelligence", 3);
HeroStat.Add("Strength", 3);
HeroStat.Add("Wisdom", 3);
//===STAT DEFINITION==============
StatDef.Add("Constitution", "Your Overall Healthiness");
print (HeroName["Name"]);
print (HeroStat["Strength"]);
}
}
这是我试图用来访问字典的脚本:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
public class heroName : MonoBehaviour {
public HeroStats heroStats;
public Text text;
public string lookUp;
public string what;
// Use this for initialization
void Start(){
heroStats = FindObjectOfType<HeroStats>();
what = heroStats.GetType().GetField("Name").GetValue(this).ToString();
text = gameObject.GetComponent<Text>();
lookUp = this.gameObject.name;
}
void Update(){
}
}
我只是不知道我现在在做什么,或者知道要google什么。我见过的每个视频/教程都会显示他们从同一个脚本/对象访问字典。
答案 0 :(得分:0)
字典只是一种存储数据的方式。就像一个int或string。您需要在外部脚本中访问您的词典。
你需要在HeroStats中公开你的词典,然后在heroName中你只需要
heroStats.HeroStat["Constitution"]