在評論中看到您的示例,並且這是針對Unity的,請觀看this clip from Unite 2016。它討論使用腳本對象而不是字典鍵的枚舉。
你會做什麼是有
public class Program
{
protected Dictionary<ScriptableObject, string> dict = new Dictionary<ScriptableObject, string>();
}
public class ProgramChild1 : Program
{
public void Test()
{
dict.Add(MyEnum1.One.Instance, "One");
dict.Add(MyEnum1.Two.Instance, "Two");
dict.Add(MyEnum1.Three.Instance, "Three");
string result;
dict.TryGetValue(MyEnum1.Two.Instance, out result);
}
}
public class ProgramChild2 : Program
{
public void Test()
{
dict.Add(MyEnum2.Four.Instance, "One");
dict.Add(MyEnum2.Five.Instance, "Two");
dict.Add(MyEnum2.Six.Instance, "Three");
string result;
dict.TryGetValue(MyEnum2.Five.Instance, out result);
}
}
//Each class goes in to its own .cs file, Put them in two folders `MyEnum1` and `MyEnum2`
namespace MyEnum1
{
public class One : ScriptableObject
{
private static One _inst;
public static One Instance
{
get
{
if (!_inst)
_inst = Resources.FindObjectOfType<One>();
if (!_inst)
_inst = CreateInstance<One>();
return _inst;
}
}
}
}
namespace MyEnum1
{
public class Two : ScriptableObject
{
private static Two _inst;
public static Two Instance
{
get
{
if (!_inst)
_inst = Resources.FindObjectOfType<Two>();
if (!_inst)
_inst = CreateInstance<Two>();
return _inst;
}
}
}
}
namespace MyEnum1
{
public class Three : ScriptableObject
{
private static Three _inst;
public static Three Instance
{
get
{
if (!_inst)
_inst = Resources.FindObjectOfType<Three>();
if (!_inst)
_inst = CreateInstance<Three>();
return _inst;
}
}
}
}
namespace MyEnum2
{
public class Four : ScriptableObject
{
private static Four_inst;
public static Four Instance
{
get
{
if (!_inst)
_inst = Resources.FindObjectOfType<Four>();
if (!_inst)
_inst = CreateInstance<Four>();
return _inst;
}
}
}
}
namespace MyEnum2
{
public class Five : ScriptableObject
{
private static Five _inst;
public static Five Instance
{
get
{
if (!_inst)
_inst = Resources.FindObjectOfType<Five>();
if (!_inst)
_inst = CreateInstance<Five>();
return _inst;
}
}
}
}
namespace MyEnum2
{
public class Six : ScriptableObject
{
private static Six _inst;
public static Six Instance
{
get
{
if (!_inst)
_inst = Resources.FindObjectOfType<Six>();
if (!_inst)
_inst = CreateInstance<Six>();
return _inst;
}
}
}
}
請注意,我們從ScriptableObject
繼承的原因是,如果你想一個枚舉暴露設計師,那麼你可以拖動和右鍵的在設計下降枚舉值,你不能這樣做,如果只是基本類的枚舉。
public class ProgramChild2 : Program
{
public ScriptableObject SelectedValue;
public void Test()
{
dict.Add(MyEnum2.Four.Instance, "One");
dict.Add(MyEnum2.Five.Instance, "Two");
dict.Add(MyEnum2.Six.Instance, "Three");
string result;
dict.TryGetValue(SelectedValue, out result);
}
}
您的字典聲明中是否有拼寫錯誤?你的枚舉是'MyEnum',但你的字典是'Dictionary' –
maccettura
爲什麼你的字典類型是''而不是''? –
因爲我的字典存儲在ma基類中,並且Iam每次都使用不同的枚舉將其填充到派生類中 – MrIncognito