Exemplo n.º 1
0
    void SetupGenerator()
    {
        noGeneratorFound = false;
        SwitchToOutputMode();

        parameterList = new List <GeneratorParameter>();

        foreach (FieldInfo field in generator.GetType().GetFields())
        {
            foreach (Attribute _attr in field.GetCustomAttributes(false))
            {
                if (_attr is TunableAttribute)
                {
                    TunableAttribute attr = (TunableAttribute)_attr;
                    parameterList.Add(new GeneratorParameter(attr.Name, field.GetValue(generator), attr.MinValue, attr.MaxValue, field, generator));
                }
            }
        }

        foreach (MethodInfo method in generator.GetType().GetMethods())
        {
            foreach (Attribute attr in method.GetCustomAttributes(false))
            {
                if (attr is Generator)
                {
                    // Debug.Log(generator.name + "." + method.Name);
                    generateMapMethod = method;
                }
                if (attr is Visualiser)
                {
                    contentVisualiser = method;
                }
            }
        }

        if (generateMapMethod == null)
        {
            noGeneratorFound = true;
        }
        else
        {
            GenerateMap();
        }
    }
Exemplo n.º 2
0
    public void SetupTuningUI(MonoBehaviour generator)
    {
        this.generator = generator;

        TunableParameterInputList = new List <TunableParameter>();

        //Find all the fields with tunable attributes and add them to the UI
        foreach (FieldInfo field in generator.GetType().GetFields())
        {
            foreach (Attribute attr in field.GetCustomAttributes(false))
            {
                if (attr is TunableAttribute)
                {
                    TunableAttribute t = (TunableAttribute)attr;

                    TunableParameter tpi;
                    if (t.MinValue is bool)
                    {
                        GameObject tpp = Instantiate(TunableBoolUIPrefab);
                        tpp.transform.parent = TunableParameterPanel.transform;
                        tpi = tpp.GetComponent <TunableParameterToggleField>();
                    }
                    else
                    {
                        GameObject tpp = Instantiate(TunableParameterUIPrefab);
                        tpp.transform.parent = TunableParameterPanel.transform;
                        tpi = tpp.GetComponent <TunableParameterInput>();
                    }

                    tpi.Setup(this);
                    //Set the field's name up
                    tpi.targetParameter = field;
                    tpi.tuneMin         = t.MinValue;
                    tpi.tuneMax         = t.MaxValue;
                    tpi.SetValue(field.GetValue(generator));
                    if (t.Name == "")
                    {
                        tpi.label.text = field.Name;
                    }
                    else
                    {
                        tpi.label.text = t.Name;
                    }
                    //Add the TPIs to the corresponding list
                    TunableParameterInputList.Add(tpi);

                    //Also add them to the ERAnalyser as parameter setting objects
                    ParSetting p = new ParSetting(field, generator, t.MinValue, t.MaxValue);
                    tuner.AddParameter(p);
                }
            }
        }

        MetricReportTexts    = new List <Text>();
        MetricDelegateList   = new List <TargetSetting.MetricDelegate>();
        MetricInputFieldList = new List <InputField>();

        List <string> metricNames = new List <string>();

        foreach (MonoBehaviour b in generator.GetComponents <MonoBehaviour>())
        {
            foreach (MethodInfo method in b.GetType().GetMethods())
            {
                foreach (Attribute attr in method.GetCustomAttributes(false))
                {
                    if (attr is Metric)
                    {
                        GameObject metricPanel = Instantiate(TargetMetricPanelUIPrefab);
                        metricPanel.transform.parent = TargetMetricPanel.transform;
                        metricPanel.transform.Find("TargetLabel").GetComponent <Text>().text = "Calculate " + ((Metric)attr).Name;

                        //This line creates a MetricDelegate from the MethodInfo object.
                        //MetricDelegates take a Tile[,] and return a float.
                        //Note that we pass in the MonoBehaviour because it needs an object to invoke the delegate on.
                        MetricDelegateList.Add((TargetSetting.MetricDelegate)Delegate.CreateDelegate(typeof(TargetSetting.MetricDelegate), b, method));
                        MetricInputFieldList.Add(metricPanel.transform.Find("TargetInput").GetComponent <InputField>());

                        //We also create a report in the top left
                        GameObject metricReport = Instantiate(MetricReportUIPrefab);
                        metricReport.transform.parent = MetricReportPanel.transform;
                        metricReport.name             = "MetricReport" + ((Metric)attr).Name;
                        metricReport.transform.Find("MetricName").GetComponent <Text>().text = "" + ((Metric)attr).Name;
                        MetricReportTexts.Add(metricReport.transform.Find("MetricValue").GetComponent <Text>());

                        metricNames.Add(method.Name);
                    }
                }
            }
        }

        if (MetricInputFieldList.Count == 0 || DAN.Instance.AddDefaultMetrics)
        {
            LevelAnalyser la = DAN.Instance.analyser;
            foreach (MethodInfo method in la.GetType().GetMethods())
            {
                foreach (Attribute attr in method.GetCustomAttributes(false))
                {
                    if (attr is Metric)
                    {
                        GameObject metricPanel = Instantiate(TargetMetricPanelUIPrefab);
                        metricPanel.transform.parent = TargetMetricPanel.transform;
                        metricPanel.transform.Find("TargetLabel").GetComponent <Text>().text = ((Metric)attr).Name;

                        //This line creates a MetricDelegate from the MethodInfo object.
                        //MetricDelegates take a Tile[,] and return a float.
                        //Note that we pass in the MonoBehaviour because it needs an object to invoke the delegate on.
                        MetricDelegateList.Add((TargetSetting.MetricDelegate)Delegate.CreateDelegate(typeof(TargetSetting.MetricDelegate), la, method));
                        MetricInputFieldList.Add(metricPanel.transform.Find("TargetInput").GetComponent <InputField>());

                        //We also create a report in the top left
                        GameObject metricReport = Instantiate(MetricReportUIPrefab);
                        metricReport.transform.parent = MetricReportPanel.transform;
                        metricReport.name             = "MetricReport" + ((Metric)attr).Name;
                        metricReport.transform.Find("MetricName").GetComponent <Text>().text = "" + ((Metric)attr).Name;
                        MetricReportTexts.Add(metricReport.transform.Find("MetricValue").GetComponent <Text>());

                        metricNames.Add(method.Name);
                    }
                }
            }
        }

        //Update the ERA Buttons with the first two metrics
        GameObject.Find("MetricName1").GetComponent <InputField>().text = metricNames[0];
        GameObject.Find("MetricName2").GetComponent <InputField>().text = metricNames[1];
    }