Example #1
0
    void init()
    {
        //PROPERTIES
        oscProperties = new List <KeyValuePair <string, FieldInfo> >();

        Type t = GetType();

        FieldInfo[] objectFields = t.GetFields(BindingFlags.Instance | BindingFlags.Public);
        for (int i = 0; i < objectFields.Length; i++)
        {
            FieldInfo   info      = objectFields[i];
            OSCProperty attribute = Attribute.GetCustomAttribute(info, typeof(OSCProperty)) as OSCProperty;
            if (attribute != null)
            {
                oscProperties.Add(new KeyValuePair <string, FieldInfo>(attribute.address, info));
            }
        }

        //METHODS

        oscMethods = new List <KeyValuePair <string, MethodInfo> >();

        MethodInfo[] methodFields = t.GetMethods(BindingFlags.Instance | BindingFlags.Public);

        for (int i = 0; i < methodFields.Length; i++)
        {
            MethodInfo info      = methodFields[i];
            OSCMethod  attribute = Attribute.GetCustomAttribute(info, typeof(OSCMethod)) as OSCMethod;
            if (attribute != null)
            {
                oscMethods.Add(new KeyValuePair <string, MethodInfo>(attribute.address, info));
            }
        }
    }
Example #2
0
    public object getData()
    {
        ControllableData data = new ControllableData();

        data.dataID = id;

        foreach (FieldInfo p in Properties.Values)
        {
            OSCProperty attribute = Attribute.GetCustomAttribute(p, typeof(OSCProperty)) as OSCProperty;
            if (attribute.IncludeInPresets)
            {
                Debug.Log("Attribute : " + p.Name + " of type " + p.FieldType + " is saved.");
                data.nameList.Add(p.Name);

                //Because a simple "toString" doesn't give the full value
                if (p.FieldType.ToString() == "UnityEngine.Vector3")
                {
                    data.valueList.Add(((Vector3)p.GetValue(this)).ToString("F8"));
                }
                else if (p.FieldType.ToString() == "System.Single")
                {
                    data.valueList.Add(((float)p.GetValue(this)).ToString("F8"));
                }
                else
                {
                    data.valueList.Add(p.GetValue(this).ToString());
                }
            }
        }

        return(data);
    }
Example #3
0
    public object getData()
    {
        ControllableData data = new ControllableData();

        data.dataID = id;

        foreach (MemberInfo p in Properties.Values)
        {
            OSCProperty attribute = Attribute.GetCustomAttribute(p, typeof(OSCProperty)) as OSCProperty;
            if (attribute.IncludeInPresets)
            {
                string typeString = getInfoTypeString(p);
                if (debug)
                {
                    Debug.Log("Attribute : " + p.Name + " of type " + typeString + " is saved.");
                }

                data.nameList.Add(p.Name);

                //Because a simple "toString" doesn't give the full value
                object val = getInfoValue(p);
                if (typeString == "UnityEngine.Vector3")
                {
                    data.valueList.Add(((Vector3)val).ToString("F8"));
                }
                else if (typeString == "System.Single")
                {
                    data.valueList.Add(((float)val).ToString("F8"));
                }
                else
                {
                    data.valueList.Add(val.ToString());
                }
            }
        }

        return(data);
    }
    public virtual void Awake()
    {
        if (TargetScript == null)
        {
            Debug.LogError("TargetScript of " + this.GetType().ToString() + " is not set ! Aborting initialization.");
        }

        this.scriptValueChanged += OnScriptValueChanged;
        this.uiValueChanged     += OnUiValueChanged;

        //FIELDS
        Fields               = new Dictionary <string, FieldInfo>();
        TargetFields         = new Dictionary <string, ClassAttributInfo>();
        PreviousFieldsValues = new List <object>();

        Type t = GetType();

        FieldInfo[]    objectFields     = t.GetFields(BindingFlags.Instance | BindingFlags.Public);
        FieldInfo[]    scriptFields     = objectFields;
        PropertyInfo[] scriptProperties = null;
        if (TargetScript != null)
        {
            scriptFields     = TargetScript.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
            scriptProperties = TargetScript.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
        }

        for (int i = 0; i < objectFields.Length; i++)
        {
            FieldInfo info = objectFields[i];

            OSCProperty attribute = Attribute.GetCustomAttribute(info, typeof(OSCProperty)) as OSCProperty;
            if (attribute != null)
            {
                if (info.Name == "currentPreset" && !usePresets)
                {
                    continue;
                }

                Fields.Add(info.Name, info);

                var fieldAdded = false;
                for (int j = 0; j < scriptFields.Length; j++)
                {
                    if (scriptFields[j].Name == info.Name)
                    {
                        var newClassAttributInfo = new ClassAttributInfo();
                        newClassAttributInfo.Field = scriptFields[j];

                        TargetFields.Add(scriptFields[j].Name, newClassAttributInfo);
                        fieldAdded = true;
                        break;
                    }
                }

                if (!fieldAdded)
                {
                    for (int j = 0; j < scriptProperties.Length; j++)
                    {
                        if (scriptProperties[j].Name == info.Name)
                        {
                            var newClassAttributInfo = new ClassAttributInfo();
                            newClassAttributInfo.Property = scriptProperties[j];

                            TargetFields.Add(scriptProperties[j].Name, newClassAttributInfo);
                            break;
                        }
                    }
                }

                PreviousFieldsValues.Add(info.GetValue(this));
            }
        }


        //METHODS
        Methods = new Dictionary <string, MethodInfo>();

        MethodInfo[] methodFields = t.GetMethods(BindingFlags.Instance | BindingFlags.Public);

        for (int i = 0; i < methodFields.Length; i++)
        {
            MethodInfo info      = methodFields[i];
            OSCMethod  attribute = Attribute.GetCustomAttribute(info, typeof(OSCMethod)) as OSCMethod;
            if (attribute != null)
            {
                if ((info.Name == "Save" || info.Name == "SaveAs" || info.Name == "Load" || info.Name == "Show") && !usePresets)
                {
                    continue;
                }
                Methods.Add(info.Name, info);
            }
        }

        if (string.IsNullOrEmpty(id))
        {
            id = TargetScript.GetType().Name;
        }

        id          = id.Replace(' ', '_');
        sourceScene = SceneManager.GetActiveScene().name;
    }
Example #5
0
    public void CreateUI(Controllable newControllable)
    {
        if (showDebug)
        {
            Debug.Log("Adding " + newControllable.id + ", use panel : " + newControllable.usePanel);
        }

        if (!newControllable.usePanel)
        {
            return;
        }

        //First we create a panel for the controllable
        var newControllableHolder = Instantiate(PanelPrefab);

        newControllableHolder.transform.GetChild(0).GetComponent <Image>().color = newControllable.BarColor;
        newControllableHolder.transform.SetParent(MainPanel.transform);

        var newPanel = newControllableHolder.transform.GetChild(1).gameObject;

        newPanel.GetComponentInChildren <Text>().text = newControllable.id;
        newPanel.transform.GetChild(0).GetChild(0).GetComponentInChildren <Image>().color = newControllable.BarColor;

        _panels.Add(newControllable.id, newControllableHolder);

        //Read all properties and add associated UI
        foreach (var property in newControllable.Fields)
        {
            var         propertyType = property.Value.FieldType;
            OSCProperty attribute    = Attribute.GetCustomAttribute(property.Value, typeof(OSCProperty)) as OSCProperty;

            //Check if needs to be in UI
            if (!attribute.ShowInUI)
            {
                continue;
            }

            if (showDebug)
            {
                Debug.Log("[UI] Adding control for (" + newControllable.GetType() + ") : " + property.Value.Name + " of type : " + propertyType.ToString());
            }

            bool propertyDrawn = false;

            //Add header if it exists
            var headerAttribut = (HeaderAttribute[])property.Value.GetCustomAttributes(typeof(HeaderAttribute), false);
            if (headerAttribut.Length != 0)
            {
                CreateHeaderText(newPanel.transform, newControllable, headerAttribut[0].header);
            }

            //Create list
            if (attribute.TargetList != "" && attribute.TargetList != null && !propertyDrawn)
            {
                var associatedListFieldInfo = newControllable.getFieldInfoByName(attribute.TargetList);
                CreateDropDown(newPanel.transform, newControllable, associatedListFieldInfo, property.Value);

                propertyDrawn = true;
                //continue;
            }
            //property.Value.Attributes O
            if (propertyType.ToString() == "System.Single" || propertyType.ToString() == "System.Int32" && !propertyDrawn)
            {
                var rangeAttribut = (RangeAttribute[])property.Value.GetCustomAttributes(typeof(RangeAttribute), false);

                bool isFloat = propertyType.ToString() != "System.Int32";

                if (rangeAttribut.Length == 0)
                {
                    CreateInput(newPanel.transform, newControllable, property.Value, attribute.isInteractible);
                }
                else
                {
                    CreateSlider(newPanel.transform, newControllable, property.Value, rangeAttribut[0], attribute.isInteractible, isFloat);
                }

                propertyDrawn = true;
                //continue;
            }
            if (propertyType.ToString() == "System.Boolean" && !propertyDrawn)
            {
                CreateCheckbox(newPanel.transform, newControllable, property.Value, attribute.isInteractible);

                propertyDrawn = true;
                //continue;
            }
            if ((propertyType.ToString() == "System.Int32" || propertyType.ToString() == "System.Float" || propertyType.ToString() == "System.String") && !propertyDrawn)
            {
                CreateInput(newPanel.transform, newControllable, property.Value, attribute.isInteractible);

                propertyDrawn = true;
                //continue;
            }

            if (propertyType.ToString() == "UnityEngine.Color" && !propertyDrawn)
            {
                CreateColor(newPanel.transform, newControllable, property.Value, attribute.isInteractible);

                propertyDrawn = true;
                //continue;
            }

            if (propertyType.ToString() == "UnityEngine.Vector3" && !propertyDrawn)
            {
                CreateVector3(newPanel.transform, newControllable, property.Value, attribute.isInteractible);

                propertyDrawn = true;
                //continue;
            }

            //Add tooltip if it exists
            var tooltipAttribut = (TooltipAttribute[])property.Value.GetCustomAttributes(typeof(TooltipAttribute), false);
            if (tooltipAttribut.Length != 0)
            {
                CreateTooltipText(newPanel.transform, newControllable, tooltipAttribut[0].tooltip);
            }
        }

        //Read all methods and add button
        foreach (var method in newControllable.Methods)
        {
            if (showDebug)
            {
                Debug.Log("[UI] Adding button for (" + newControllable.GetType() + ") : " + method.Value.Name);
            }

            CreateButton(newPanel.transform, newControllable, method.Value);
        }

        CleanGeneratedUI(newControllable.id, newControllable);
    }
Example #6
0
    public void setFieldProp(FieldInfo info, List <object> values, bool isEnum = false)
    {
        OSCProperty attribute = Attribute.GetCustomAttribute(info, typeof(OSCProperty)) as OSCProperty;

        if (attribute.isInteractible == false)
        {
            return;
        }

        string typeString = info.FieldType.ToString();

        if (debug)
        {
            Debug.Log("Setting attribut " + info.Name + " of type " + typeString + " (enum?" + isEnum + ") with " + values.Count + " value(s)");
        }

        // if we detect any attribute print out the data.

        if (isEnum)
        {
            if (values.Count >= 1)
            {
                info.SetValue(this, TypeConverter.getIndexInEnum(Enum.GetNames(Type.GetType(typeString)).ToList(), (string)values[0]));
            }
        }
        else
        {
            if (typeString == "System.Single")
            {
                if (values.Count >= 1)
                {
                    info.SetValue(this, TypeConverter.getFloat(values[0]));
                }
            }
            else if (typeString == "System.Boolean")
            {
                if (values.Count >= 1)
                {
                    info.SetValue(this, TypeConverter.getBool(values[0]));
                }
            }
            else if (typeString == "System.Int32")
            {
                if (values.Count >= 1)
                {
                    info.SetValue(this, TypeConverter.getInt(values[0]));
                }
            }
            else if (typeString == "UnityEngine.Vector2")
            {
                if (values.Count == 1)
                {
                    info.SetValue(this, (Vector2)values[0]);
                }
                if (values.Count >= 2)
                {
                    info.SetValue(this, new Vector2(TypeConverter.getFloat(values[0]), TypeConverter.getFloat(values[1])));
                }
            }
            else if (typeString == "UnityEngine.Vector2Int")
            {
                if (values.Count == 1)
                {
                    info.SetValue(this, (Vector2Int)values[0]);
                }
                if (values.Count >= 2)
                {
                    info.SetValue(this, new Vector2Int(TypeConverter.getInt(values[0]), TypeConverter.getInt(values[1])));
                }
            }
            else if (typeString == "UnityEngine.Vector3")
            {
                if (values.Count == 1)
                {
                    info.SetValue(this, (Vector3)values[0]);
                }
                if (values.Count >= 3)
                {
                    info.SetValue(this, new Vector3(TypeConverter.getFloat(values[0]), TypeConverter.getFloat(values[1]), TypeConverter.getFloat(values[2])));
                }
            }
            else if (typeString == "UnityEngine.Vector3Int")
            {
                if (values.Count == 1)
                {
                    info.SetValue(this, (Vector3Int)values[0]);
                }
                if (values.Count >= 3)
                {
                    info.SetValue(this, new Vector3Int(TypeConverter.getInt(values[0]), TypeConverter.getInt(values[1]), TypeConverter.getInt(values[2])));
                }
            }
            else if (typeString == "UnityEngine.Color")
            {
                if (values.Count == 1)
                {
                    info.SetValue(this, (Color)values[0]);
                }
                else if (values.Count >= 4)
                {
                    info.SetValue(this, new Color(TypeConverter.getFloat(values[0]), TypeConverter.getFloat(values[1]), TypeConverter.getFloat(values[2]), TypeConverter.getFloat(values[3])));
                }
                else if (values.Count >= 3)
                {
                    info.SetValue(this, new Color(TypeConverter.getFloat(values[0]), TypeConverter.getFloat(values[1]), TypeConverter.getFloat(values[2]), 1));
                }
            }
            else if (typeString == "System.String")
            {
                // Debug.Log("String received : " + values.ToString());
                info.SetValue(this, values[0].ToString());
            }
        }
        if (uiValueChanged != null)
        {
            uiValueChanged(info.Name);
        }
    }
Example #7
0
    public void CreateUI(Controllable newControllable)
    {
        if (showDebug)
        {
            Debug.Log("Adding " + newControllable.id + ", use panel : " + newControllable.usePanel);
        }

        if (!newControllable.usePanel)
        {
            return;
        }

        //First we create a panel for the controllable
        var newPanel = Instantiate(PanelPrefab);

        newPanel.transform.SetParent(Panel.transform);
        newPanel.GetComponentInChildren <Text>().text      = newControllable.id;
        newPanel.GetComponent <RectTransform>().localScale = new Vector3(1.0f, 1.0f, 1.0f);
        _panels.Add(newControllable.id, newPanel);

        //Read all methods and add button
        foreach (var method in newControllable.Methods)
        {
            CreateButton(newPanel.transform, newControllable, method.Value);
        }

        //Read all properties and add associated UI
        foreach (var property in newControllable.Properties)
        {
            var         propertyType = property.Value.FieldType;
            OSCProperty attribute    = Attribute.GetCustomAttribute(property.Value, typeof(OSCProperty)) as OSCProperty;

            //Check if needs to be in UI
            if (!attribute.ShowInUI)
            {
                continue;
            }

            //Create list
            if (attribute.TargetList != "" && attribute.TargetList != null)
            {
                var associatedListFieldInfo = newControllable.getFieldInfoByName(attribute.TargetList);
                CreateDropDown(newPanel.transform, newControllable, associatedListFieldInfo, property.Value);
                continue;
            }
            //property.Value.Attributes O
            if (propertyType.ToString() == "System.Single" || propertyType.ToString() == "System.Int32")
            {
                var rangeAttribut = (RangeAttribute[])property.Value.GetCustomAttributes(typeof(RangeAttribute), false);

                bool isFloat = propertyType.ToString() != "System.Int32";

                if (rangeAttribut.Length == 0)
                {
                    CreateInput(newPanel.transform, newControllable, property.Value, attribute.isInteractible);
                }
                else
                {
                    CreateSlider(newPanel.transform, newControllable, property.Value, rangeAttribut[0], attribute.isInteractible, isFloat);
                }
                continue;
            }
            if (propertyType.ToString() == "System.Boolean")
            {
                CreateCheckbox(newPanel.transform, newControllable, property.Value, attribute.isInteractible);
                continue;
            }
            if (propertyType.ToString() == "System.Int32" || propertyType.ToString() == "System.Float" || propertyType.ToString() == "System.String")
            {
                CreateInput(newPanel.transform, newControllable, property.Value, attribute.isInteractible);
                continue;
            }
            //Debug.Log("Property type : " + propertyType + " of " + property.Key);
        }

        //Order Save and Load preset buttons
        var allText = newPanel.GetComponentsInChildren <Text>();

        foreach (var text in allText)
        {
            if (text.text == "SavePreset" || text.text == "LoadPreset")
            {
                text.transform.parent.SetSiblingIndex(newPanel.transform.childCount - 2); //last index being the preset list
            }
        }
    }
Example #8
0
    public virtual void Awake()
    {
        //PROPERTIES
        Properties = new Dictionary <string, FieldInfo>();
        PreviousPropertiesValues = new List <object>();

        Type t = GetType();

        FieldInfo[] objectFields = t.GetFields(BindingFlags.Instance | BindingFlags.Public);

        for (int i = 0; i < objectFields.Length; i++)
        {
            FieldInfo   info      = objectFields[i];
            OSCProperty attribute = Attribute.GetCustomAttribute(info, typeof(OSCProperty)) as OSCProperty;
            if (attribute != null)
            {
                if (info.Name == "currentPreset" && !usePresets)
                {
                    continue;
                }

                Properties.Add(info.Name, info);
                //Debug.Log("Intializing " + info.Name + " with " + info.GetValue(this));
                PreviousPropertiesValues.Add(info.GetValue(this));
            }
        }

        //METHODS

        Methods = new Dictionary <string, MethodInfo>();

        MethodInfo[] methodFields = t.GetMethods(BindingFlags.Instance | BindingFlags.Public);

        for (int i = 0; i < methodFields.Length; i++)
        {
            MethodInfo info      = methodFields[i];
            OSCMethod  attribute = Attribute.GetCustomAttribute(info, typeof(OSCMethod)) as OSCMethod;
            if (attribute != null)
            {
                if ((info.Name == "SavePreset" || info.Name == "LoadPreset") && !usePresets)
                {
                    continue;
                }

                Methods.Add(info.Name, info);
            }
        }

        if (string.IsNullOrEmpty(id))
        {
            id = gameObject.name;
        }
        sourceScene = SceneManager.GetActiveScene().name;

        ControllableMaster.Register(this);

        presetList = new List <string>();
        ReadFileList();

        if (presetList.Count > 1)
        {
            currentPreset = presetList[0];
        }
        LoadLatestUsedPreset();
    }
Example #9
0
    public virtual void Awake()
    {
        this.scriptValueChanged += OnScriptValueChanged;
        this.uiValueChanged     += OnUiValueChanged;

        //PROPERTIES
        Properties               = new Dictionary <string, MemberInfo>();
        TargetProperties         = new Dictionary <string, MemberInfo>();
        PreviousPropertiesValues = new List <object>();

        MemberInfo[] objectProperties = GetType().GetMembers(BindingFlags.GetProperty | BindingFlags.GetField | BindingFlags.Instance | BindingFlags.Public);
        MemberInfo[] scriptProperties = objectProperties;


        if (TargetScript == null)
        {
            TargetScript = this;
        }
        else
        {
            objectProperties = TargetScript.GetType().GetMembers(BindingFlags.GetProperty | BindingFlags.GetField | BindingFlags.Instance | BindingFlags.Public);
        }

        Debug.Log("Found " + objectProperties.Length + " fields and properties in " + id);

        for (int i = 0; i < objectProperties.Length; i++)
        {
            MemberInfo  info      = objectProperties[i];
            OSCProperty attribute = Attribute.GetCustomAttribute(info, typeof(OSCProperty)) as OSCProperty;
            if (attribute != null)
            {
                if (info.Name == "currentPreset" && !usePresets)
                {
                    continue;
                }

                String propName = attribute.customName == null ? info.Name : attribute.customName;

                Properties.Add(propName, info);

                for (int j = 0; j < scriptProperties.Length; j++)
                {
                    if (scriptProperties[j].Name == info.Name)
                    {
                        TargetProperties.Add(propName, scriptProperties[j]);
                        break;
                    }
                }

                PreviousPropertiesValues.Add(getInfoValue(info));
            }
        }

        //METHODS

        Methods = new Dictionary <string, MethodInfo>();

        MethodInfo[] methodFields = GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public);

        for (int i = 0; i < methodFields.Length; i++)
        {
            MethodInfo info      = methodFields[i];
            OSCMethod  attribute = Attribute.GetCustomAttribute(info, typeof(OSCMethod)) as OSCMethod;
            if (attribute != null)
            {
                if ((info.Name == "Save" || info.Name == "SaveAs" || info.Name == "Load" || info.Name == "Show") && !usePresets)
                {
                    continue;
                }
                Methods.Add(info.Name, info);
            }
        }

        if (string.IsNullOrEmpty(id))
        {
            id = TargetScript.GetType().Name;
        }

        sourceScene = SceneManager.GetActiveScene().name;
    }