示例#1
0
    // Converts object to string!
    public string GetValueString(object value, DebugTunable tunable = null)
    {
        string dataType = "" + type;
        string ret      = "";

        switch (dataType)
        {
        case DataType.COLOR:
            ret = ((Color)value).ToString();
            break;

        case DataType.BOOLEAN:
            ret = (bool)value ? TRUE : FALSE;
            break;

        case DataType.RANGE:
            DebugNumberTunable numTunable = tunable as DebugNumberTunable;

            if (value.GetType() == typeof(int))
            {
                if (numTunable != null)
                {
                    ret = numTunable.MapRange((int)value, RANGE_MIN, RANGE_MAX).ToString();
                }
                else
                {
                    ret = ((int)value).ToString();
                }
            }
            else if (value.GetType() == typeof(float))
            {
                if (numTunable != null)
                {
                    ret = numTunable.MapRange((float)value, RANGE_MIN, RANGE_MAX).ToString();
                }
                else
                {
                    ret = ((float)value).ToString();
                }
            }
            break;

        case DataType.STRING:
            ret = (string)value;
            break;

        default:
        {
            if (dataType.StartsWith("enum_"))
            {
                ret = value.ToString();
            }
            else
            {
                // Unsupported type?  Can still format to string
                ret = value.ToString();
            }
        }
        break;
        }

        return(ret);
    }
示例#2
0
    public void AddTunable(DebugTunable attTunable, UnityEngine.Object inst)
    {
        if (attTunable == null)
        {
            Debug.Log("AddTunable:  null attTunable, what?");
            return;
        }

        int urid = -1;

        if (attTunable.IsField())
        {
            // Initial value
            object value     = attTunable.fieldInfo.GetValue(inst);
            string debugName = string.IsNullOrEmpty(attTunable.ItemName) ? attTunable.fieldInfo.Name : attTunable.ItemName;

            // Add the item
            if (attTunable is DebugBoolTunable)
            {
                urid = DebugMenu.Instance.AddBoolItem(debugName, (bool newVal) => { attTunable.fieldInfo.SetValue(inst, newVal); }, () => { return((bool)attTunable.fieldInfo.GetValue(inst)); }, (bool)value, this.gameObject);
            }
            else if (attTunable is DebugNumberTunable)
            {
                DebugNumberTunable numTuneable = attTunable as DebugNumberTunable;
                if (attTunable.fieldInfo.FieldType == typeof(float))
                {
                    urid = DebugMenu.Instance.AddFloatItem(debugName, (float newVal) => { attTunable.fieldInfo.SetValue(inst, newVal); }, () => { return((float)attTunable.fieldInfo.GetValue(inst)); }, (float)value, numTuneable.Fmin, numTuneable.Fmax, this.gameObject, (int)numTuneable.FStep);
                }
                else if (attTunable.fieldInfo.FieldType == typeof(int))
                {
                    urid = DebugMenu.Instance.AddIntItem(debugName, (int newVal) => { attTunable.fieldInfo.SetValue(inst, newVal); }, () => { return((int)attTunable.fieldInfo.GetValue(inst)); }, (int)value, (int)numTuneable.Fmin, (int)numTuneable.Fmax, this.gameObject, (int)Mathf.Max(1, (int)numTuneable.FStep));
                }
            }
            else if (attTunable is DebugVector3Tunable)
            {
                DebugVector3Tunable vec3Tuneable = attTunable as DebugVector3Tunable;
                urid = DebugMenu.Instance.AddVector3Item(debugName, (Vector3 newVal) => { attTunable.fieldInfo.SetValue(inst, newVal); }, () => { return((Vector3)attTunable.fieldInfo.GetValue(inst)); }, (Vector3)value, vec3Tuneable.VecStep, this.gameObject);
            }
            else if (CustomAttributeBase.IsEnum(attTunable.fieldInfo.FieldType) && attTunable is DebugEnumTunable)
            {
                DebugEnumTunable enumTunable = attTunable as DebugEnumTunable;
                // Get range from enum values
                Type fieldType = attTunable.fieldInfo.FieldType;

                int minv = enumTunable.MinEnumVal;
                int maxv = enumTunable.MaxEnumVal;

                if (minv == -1 && maxv == -1)
                {
                    Array arr = Enum.GetValues(fieldType);
                    if (arr != null && arr.Length > 0)
                    {
                        minv = (int)arr.GetValue(0);
                        maxv = (int)arr.GetValue(arr.Length - 1);
                    }
                }

                urid = DebugMenu.Instance.AddEnumItem(debugName, attTunable.fieldInfo.FieldType, (int newVal) => { attTunable.fieldInfo.SetValue(inst, newVal); }, () => { return((int)attTunable.fieldInfo.GetValue(inst)); }, (int)value, minv, maxv, this.gameObject);
            }
        }
        else if (attTunable.IsMethod())
        {
            string debugName = string.IsNullOrEmpty(attTunable.ItemName) ? attTunable.methodInfo.Name : attTunable.ItemName;

            if (attTunable is DebugButtonAttribute)
            {
                DebugButtonAttribute buttonTunable = attTunable as DebugButtonAttribute;

                urid = DebugMenu.Instance.AddButtonItem(debugName, buttonTunable.ButtonName, () => { buttonTunable.methodInfo.Invoke(inst, null); }, this.gameObject);
            }
        }

        // Add the item urid to the list of items associated with the object instance
        if (urid != -1)
        {
            if (!m_urids.ContainsKey(inst))
            {
                m_urids[inst] = new List <int>();
            }
            m_urids[inst].Add(urid);
        }
    }