/ / Enum gameobjects की ओर इशारा करते हुए? - c #, unity3d, enums, gameobject

Enum gameobjects को इंगित करता है? - सी #, एकता 3 डी, enums, gameobject

तो, मेरे पास गेमबजेक्ट्स की ओर इशारा करते हुए यह पागल विचार है।

यहाँ मैं क्या करना चाहता हूँ:

/* These enums would hold gameobjects instead of ints */
enum exampleEnum{
AddPanel,
ListPanel
}

public class GUIManager : MonoBehaviour {
void Start()
{
EnablePanel(exampleEnum.AddPanel);
}

void EnablePanel(GameObject panel)
{
panel.setActive(true);
}
}

क्या यह काम करने का कोई तरीका है? या एक वर्कअराउंड?

यह एक एनम के अलावा कुछ और के साथ संभव हो सकता है, लेकिन अगर वहाँ है और मैं इस तरह के एक समाधान के लिए वेब के माध्यम से देख रहा हूँ "यह पता नहीं है"।

उत्तर:

उत्तर № 1 के लिए 6

यह आपकी आवश्यकता को पूरा करेगा, enum मानों या पैनलों की किसी भी राशि के लिए काम करता है।

// Add this to each of your panels, the enum field can be integrated into your other behaviours as well
public class EnumPanel : MonoBehaviour
{
// configurable from the Editor, the unity way.
public ExampleEnum Type;
}

// Assign all your panles in the editor (or use FindObjectsByType<EnumPanel> in Start())
public class GUIManager : MonoBehaviour
{
// configurable from the Editor, the unity way.
public EnumPanel[] Panels;

void Start()
{
// Optionally find it at runtime, if assigning it via the editor is too much maintenance.
// Panels = FindObjectsByType<EnumPanel>();
EnablePanel(ExampleEnum.AddPanel);
}

void EnablePanel(ExampleEnum panelType)
{
foreach(var panel in Panels)
{
if(panel.Type == panelType)
EnablePanel(panel.gameObject);
}
}

void EnablePanel(GameObject panel)
{
panel.setActive(true);
}
}

उत्तर № 2 के लिए 1

मुझे पता नहीं है कि क्यों से जवाब: @Paradox फोर्ज गलत था, लेकिन शायद यह आपकी मदद करेगा।

System.Collections.Generic.Dictionary

डिक्शनरी क्लास को समझाने के लिए मेरे पास बहुत समय नहीं है लेकिन आप इसका उपयोग कैसे कर सकते हैं। यह कुछ प्रदर्शन पर खर्च करेगा लेकिन वास्तव में अच्छी पठनीयता है

    public class GUIManager : MonoBehaviour {
public enum exampleEnum{
AddPanel,
ListPanel
}

//For readability you can also add "using System.Collections.Generic;" on the top of your script
private System.Collections.Generic.Dictionary<exampleEnum,GameObject> exampleDictionary = new System.Collections.Generic.Dictionary<exampleEnum, GameObject>();

private GameObject SomeGameObject;
private GameObject SomeOtherGameObject;

void Start()
{
//You have to add all the enums and objects you want to use inside your GUIManager.
exampleDictionary.Add (exampleEnum.AddPanel, SomeGameObject); //Add panel will be linked to SomeGameObject
exampleDictionary.Add (exampleEnum.ListPanel, SomeOtherGameObject); //List Panel will be linked to SomeOtherGameObject

EnablePanel(exampleEnum.AddPanel);
}

void EnablePanel(exampleEnum examplePanel)
{
if (!exampleDictionary.ContainsKey (examplePanel)) //If the given panel does not exist inside the dictionary
return; //Leave the method

GameObject panelToEnable = exampleDictionary [examplePanel]; //Will return the GameObject linked to given panel
panelToEnable.SetActive(true); //Enable the gameobject
}
}

यदि आप शब्दकोश वर्ग के बारे में अधिक जानना चाहते हैं शब्दकोश