Exemple #1
0
    public void OnGUI()
    {
        target           = this;
        serializedObject = new SerializedObject(target);

        Init();

        float width = EditorGUIUtility.currentViewWidth;

        scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false);

        serializedObject.Update();

        GUIStyle label = new GUIStyle(GUI.skin.label)
        {
            fontSize  = 30,
            fontStyle = FontStyle.Bold,
            alignment = TextAnchor.UpperCenter
        };

        EditorGUI.HelpBox(new Rect(10, 5, width - 25, 40), "", MessageType.None);
        EditorGUI.LabelField(new Rect(0, 5, width, 30), name, label);

        GUILayout.Space(88);

        if (unitFile != null)
        {
            overrideFile = EditorGUI.Toggle(new Rect(width - 17, 70, 16, 16), new GUIContent(""), overrideFile);
            EditorGUI.LabelField(new Rect(width - 103, 70, 100, 16), new GUIContent("Override File?"));
        }

        EditorGUI.PropertyField(new Rect(14, 50, width * 2 / 3, 16), nameProperty, new GUIContent("Name"));
        EditorGUI.PropertyField(new Rect(14 + width * 3.7f / 5, 50, width / 4.5f, 16), unitFileProperty, new GUIContent(""));
        EditorGUI.LabelField(new Rect(18 + width * 2 / 3, 50, width * 2 / 3, 16), "File");

        EditorGUILayout.PropertyField(classProperty, new GUIContent("Class"));
        EditorGUILayout.PropertyField(sexProperty, new GUIContent("Sex"));
        EditorGUILayout.PropertyField(alignmentProperty, new GUIContent("Alignment"));

        EditorGUILayout.PropertyField(useableWeaponsProperty, new GUIContent("Useable Weapon Types"));

        EditorGUILayout.PropertyField(affinityProperty, new GUIContent("Affinity"));

        displayStats = EditorGUILayout.Foldout(displayStats, "Stats");
        if (displayStats)
        {
            int boxHeight = 172;

            {
                EditorGUI.indentLevel++;

                Rect position = GUILayoutUtility.GetLastRect();
                position.x += 4;
                position.y += 5;

                EditorGUI.HelpBox(new Rect(position.x + 8, position.y + 11, width - 31, boxHeight), "", MessageType.None);

                float i = 1;

                EditorGUILayout.BeginVertical();

                EditorGUILayout.BeginHorizontal();

                Exp = EditorGUI.IntSlider(new Rect(position.x, position.y + 16 * i, width - 30, 16), new GUIContent("EXP"), Exp, 0, StatsCalc.ExperienceToNextLevel(stats.level + 1));

                EditorGUILayout.EndHorizontal();

                i++;


                EditorGUILayout.BeginHorizontal();

                EditorGUI.PropertyField(new Rect(position.x, position.y + 16 * i, width / 2f - 30, 16), levelProperty);
                EditorGUI.PropertyField(new Rect(position.x + width / 2f, position.y + 16 * i, width / 2f - 30, 16), hpProperty);

                EditorGUILayout.EndHorizontal();

                i++;

                EditorGUILayout.BeginHorizontal();

                EditorGUI.PropertyField(new Rect(position.x, position.y + 16 * i, width / 2f - 30, 16), strengthProperty);
                EditorGUI.PropertyField(new Rect(position.x + width / 2f, position.y + 16 * i, width / 2f - 30, 16), magicProperty);

                EditorGUILayout.EndHorizontal();

                i++;

                EditorGUILayout.BeginHorizontal();

                EditorGUI.PropertyField(new Rect(position.x, position.y + 16 * i, width / 2f - 30, 16), skillProperty);
                EditorGUI.PropertyField(new Rect(position.x + width / 2f, position.y + 16 * i, width / 2f - 30, 16), speedProperty);

                EditorGUILayout.EndHorizontal();

                i++;

                EditorGUILayout.BeginHorizontal();

                EditorGUI.PropertyField(new Rect(position.x, position.y + 16 * i, width / 2f - 30, 16), luckProperty);
                EditorGUI.PropertyField(new Rect(position.x + width / 2f, position.y + 16 * i, width / 2f - 30, 16), defenseProperty);

                EditorGUILayout.EndHorizontal();

                i++;

                EditorGUILayout.BeginHorizontal();

                EditorGUI.PropertyField(new Rect(position.x, position.y + 16 * i, width / 2f - 30, 16), resistanceProperty);
                EditorGUI.PropertyField(new Rect(position.x + width / 2f, position.y + 16 * i, width / 2f - 30, 16), constitutionProperty);

                EditorGUILayout.EndHorizontal();

                i++;

                EditorGUILayout.BeginHorizontal();

                EditorGUI.PropertyField(new Rect(position.x, position.y + 16 * i, width / 2f - 30, 16), movementProperty);
                EditorGUI.PropertyField(new Rect(position.x + width / 2f, position.y + 16 * i, width / 2f - 30, 16), pccProperty, new GUIContent("Critical Coefficient"));

                EditorGUILayout.EndHorizontal();

                i++;

                EditorGUILayout.BeginHorizontal();

                EditorGUI.PropertyField(new Rect(position.x, position.y + 16 * i, width / 2f - 30, 16), aidProperty);
                //EditorGUI.PropertyField(new Rect(position.x + width / 2f, position.y + 16 * i, width / 2f - 30, 16), pccProperty, new GUIContent("Critical Coefficient"));

                EditorGUILayout.EndHorizontal();

                i += 2;

                EditorGUILayout.BeginHorizontal();

                EditorGUI.LabelField(new Rect(position.x, position.y + 16 * i, width / 3.34f - 30, 16), new GUIContent("SP"));
                EditorGUI.PropertyField(new Rect(position.x + width / 5, position.y + 16 * i, width / 3.34f - 30, 16), spProperty, new GUIContent(""));

                EditorGUI.LabelField(new Rect(position.x + width / 2f, position.y + 16 * i, width / 2f - 30, 16), new GUIContent(TotalEXP.ToString()), new GUIStyle {
                    alignment = TextAnchor.MiddleRight
                });
                EditorGUI.LabelField(new Rect(position.x + width / 2f, position.y + 16 * i, width / 2f - 30, 16), new GUIContent("Total experience: "), new GUIStyle {
                    alignment = TextAnchor.MiddleLeft
                });

                EditorGUILayout.EndHorizontal();

                EditorGUILayout.EndVertical();
            }

            GUILayout.Space(boxHeight);
            displayGrowth = EditorGUILayout.Foldout(displayGrowth, "Stat Growth");
            if (displayGrowth)
            {
                Rect pos = GUILayoutUtility.GetLastRect();
                EditorGUI.indentLevel++;
                pos.x += 4;
                pos.y += 5;

                EditorGUI.HelpBox(new Rect(pos.x + 24, pos.y + 11, width - 40, 74), "", MessageType.None);

                float i = 1;

                EditorGUILayout.BeginVertical();


                EditorGUILayout.BeginHorizontal();
                EditorGUI.PropertyField(new Rect(pos.x, pos.y + 16 * i, width / 1.95f - 30, 16), hpGrowthProperty, new GUIContent("HP Growth"));
                EditorGUI.PropertyField(new Rect(pos.x + width / 2f, pos.y + 16 * i, width / 1.95f - 30, 16), strengthGrowthProperty, new GUIContent("Strength Growth"));

                EditorGUILayout.EndHorizontal();

                i++;

                EditorGUILayout.BeginHorizontal();

                EditorGUI.PropertyField(new Rect(pos.x, pos.y + 16 * i, width / 1.95f - 30, 16), magicGrowthProperty, new GUIContent("Magic Growth"));
                EditorGUI.PropertyField(new Rect(pos.x + width / 2f, pos.y + 16 * i, width / 1.95f - 30, 16), skillGrowthProperty, new GUIContent("Skill Growth"));

                EditorGUILayout.EndHorizontal();

                i++;

                EditorGUILayout.BeginHorizontal();

                EditorGUI.PropertyField(new Rect(pos.x, pos.y + 16 * i, width / 1.95f - 30, 16), speedGrowthProperty, new GUIContent("Speed Growth"));
                EditorGUI.PropertyField(new Rect(pos.x + width / 2f, pos.y + 16 * i, width / 1.95f - 30, 16), luckGrowthProperty, new GUIContent("Luck Growth"));

                EditorGUILayout.EndHorizontal();

                i++;

                EditorGUILayout.BeginHorizontal();

                EditorGUI.PropertyField(new Rect(pos.x, pos.y + 16 * i, width / 1.95f - 30, 16), defenseGrowthProperty, new GUIContent("Defense Growth"));
                EditorGUI.PropertyField(new Rect(pos.x + width / 2f, pos.y + 16 * i, width / 1.95f - 30, 16), resistanceGrowthProperty, new GUIContent("Resistance Growth"));

                EditorGUILayout.EndHorizontal();

                EditorGUILayout.EndVertical();

                EditorGUI.indentLevel--;
                GUILayout.Space(74);
            }

            EditorGUI.indentLevel--;
        }

        displayRelations = EditorGUILayout.Foldout(displayRelations, "Relationships");
        if (displayRelations && relationships != null)
        {
            int h = 0;

            EditorGUI.indentLevel++;

            Rect position = GUILayoutUtility.GetLastRect();
            position.x += 4;
            position.y += 5;

            int height = relationships.Count * 16 + 23;
            GUILayout.Space(height);
            EditorGUI.HelpBox(new Rect(position.x + 8, position.y + 11, width - 31, height), "", MessageType.None);

            if (relationships != null)
            {
                Unit[] keys = new Unit[relationships.Count];
                Unit.Relationship[] values = new Unit.Relationship[relationships.Count];

                relationships.Keys.CopyTo(keys, 0);
                relationships.Values.CopyTo(values, 0);

                for (int i = 0; i < keys.Length; i++)
                {
                    GUILayout.BeginHorizontal();

                    keys[i].name   = EditorGUI.TextField(new Rect(position.x, position.y + 16 + 16 * h, width / 2, 16), new GUIContent((i + 1).ToString()), keys[i].name);
                    values[i].rank = (Unit.Relationship.Rank)EditorGUI.EnumPopup(new Rect(position.x + width / 1.75f, position.y + 16 + 16 * h, width / 8f, 16), new GUIContent(""), values[i].rank);
                    EditorGUI.LabelField(new Rect(position.x + width / 2, position.y + 16 + 16 * h, width / 3.25f, 16), new GUIContent("Rank"));

                    bool remove = false;
                    remove = EditorGUI.Foldout(new Rect(width - width / 4, position.y + 16 + 16 * h, width / 2, 16), remove, new GUIContent("Remove"));
                    if (remove)
                    {
                        relationships.Remove(keys[i]);
                    }

                    GUILayout.EndHorizontal();

                    h++;
                }
            }

            //position = GUILayoutUtility.GetLastRect();

            bool add = false;
            add = EditorGUI.Foldout(new Rect(position.x, position.y + 16 + 16 * h, width / 2, 16), add, new GUIContent("Add"));
            if (add)
            {
                relationships.Add(CreateInstance <Unit>(), new Unit.Relationship(Unit.Relationship.Rank.None));
            }

            EditorGUI.indentLevel--;
        }

        displayInventory = EditorGUILayout.Foldout(displayInventory, "Inventory");
        if (displayInventory)
        {
            EditorGUI.indentLevel++;

            Rect position = GUILayoutUtility.GetLastRect();
            position.x += 4;
            position.y += 5;

            int height = Inventory.Size * 16 + 18;
            GUILayout.Space(height);
            EditorGUI.HelpBox(new Rect(position.x + 8, position.y + 11, width / 2, height), "", MessageType.None);

            position.y += 20;

            int w = 295 - 36;
            for (int i = 0; i < Inventory.Size; i++)
            {
                Item item = (Item)AssetDatabase.LoadAssetAtPath("Assets/Data/Items/" + _items[i] + ".asset", typeof(Item));
                if (item != null)
                {
                    EditorGUI.DrawPreviewTexture(new Rect(position.x + 16, position.y + 16 * i, 16, 16), DataManager.ConvertSpriteToTexture(item.icon));
                }

                int _width = item != null && item.unbreakable ? w : w - 35;

                _items[i] = EditorGUI.TextField(new Rect(width / 2 - w, position.y + 16 * i, _width, 16), _items[i]);
                if (item != null && !item.unbreakable)
                {
                    _uses[i] = EditorGUI.IntField(new Rect(width / 2 - 48, position.y + 16 * i, 48, 16), _uses[i]);
                }
            }
        }

        GUILayout.BeginHorizontal();

        Unit control = CreateInstance <Unit>(), unit = CreateInstance <Unit>();

        unit.name              = name;
        unit.unitClass         = unitClass;
        unit.unitSex           = unitSex;
        unit.alignment         = alignment;
        unit.useable           = useable;
        unit.affinity          = affinity;
        unit.stats             = stats;
        unit.stats.growthRates = stats.growthRates;
        unit.relationships     = relationships;

        for (int i = 0; i < _items.Length; i++)
        {
            Item item = (Item)AssetDatabase.LoadAssetAtPath("Assets/Data/Items/" + _items[i] + ".asset", typeof(Item));

            if (item != null && !item.unbreakable)
            {
                item.uses = _uses[i];
            }
            if (item != null)
            {
                unit.inventory.Add(item);
            }
        }

        if (name != null && name != "" && GUILayout.Button("Export to file"))
        {
            DataManager.UnitToFile(unit, overrideFile);
        }
        if (unitFile != null && GUILayout.Button("Read from file"))
        {
            control = DataManager.FileToUnit(unitFile, true, true);

            name          = control.name;
            unitClass     = control.unitClass;
            unitSex       = control.unitSex;
            alignment     = control.alignment;
            useable       = control.useable;
            affinity      = control.affinity;
            relationships = control.relationships;
            stats         = new Unit.Stats(control.stats, control.stats.growthRates);
            inventory     = control.inventory.Items;

            if (control.inventory.Items == null)
            {
                control.inventory.Items = new List <Item>();
            }

            for (int i = 0; i < _items.Length; i++)
            {
                _items[i] = "";
                _uses[i]  = 0;
            }

            for (int i = 0; i < control.inventory.Items.Count; i++)
            {
                if (control.inventory.Items[i] != null)
                {
                    _items[i] = control.inventory.Items[i].name;
                    if (!control.inventory.Items[i].unbreakable)
                    {
                        _uses[i] = control.inventory.Items[i].uses;
                    }
                }
            }
        }

        GUILayout.EndHorizontal();

        EditorGUILayout.EndScrollView();

        serializedObject.ApplyModifiedProperties();
    }
Exemple #2
0
    public static Unit FileToUnit(TextAsset t, bool loadRelations, bool log = false)
    {
        Unit output = ScriptableObject.CreateInstance <Unit>();

        string[] lines = t.text.Split('\n');

        string name = "";

        Unit.UnitClass    unitClass = new Unit.UnitClass();
        Unit.Sex          sex       = 0;
        Unit.Alignment    alignment = Unit.Alignment.Player;
        Weapon.WeaponType useable   = Weapon.WeaponType.Sword;
        Element           affinity  = Element.None;

        Dictionary <Unit, Unit.Relationship> relationships = new Dictionary <Unit, Unit.Relationship>();
        List <Item> inventory = new List <Item>();

        Unit.Stats             stats  = new Unit.Stats();
        Unit.Stats.GrowthRates growth = new Unit.Stats.GrowthRates();

        int   aid = 0, exp = 0, sp = 0;
        float pcc = 0;

        int   level = 0, hp = 0, strength = 0, magic = 0, skill = 0, speed = 0, luck = 0, defense = 0, resistance = 0, constitution = 0, movement = 0;
        float hpGrowth = 0, strengthGrowth = 0, magicGrowth = 0, skillGrowth = 0, speedGrowth = 0, luckGrowth = 0, defenseGrowth = 0, resistanceGrowth = 0;

        for (int i = 0; i < lines.Length; i++)
        {
            string[] a     = lines[i].Split('=');
            string   value = a[a.Length - 1];

            if (a[0] == "Name")
            {
                name = value;
            }
            else if (a[0] == "Class")
            {
                Enum.TryParse(value, out unitClass);
            }
            else if (a[0] == "Sex")
            {
                Enum.TryParse(value, out sex);
            }
            else if (a[0] == "Alignment")
            {
                Enum.TryParse(value, out alignment);
            }
            else if (a[0] == "Useable_Weapon_Types")
            {
                string[] weapons = value.Split(',');
                int      val     = 0;

                foreach (string s in weapons)
                {
                    Enum.TryParse(s, out Weapon.WeaponType w);
                    val += (int)w;
                }

                useable = (Weapon.WeaponType)val;
            }
            else if (a[0] == "Affinity")
            {
                Enum.TryParse(value, out affinity);
            }

            else if (a[0] == "Aid")
            {
                int.TryParse(value, out aid);
            }
            else if (a[0] == "EXP")
            {
                int.TryParse(value, out exp);
            }
            else if (a[0] == "PursuitCriticalCoefficient")
            {
                float.TryParse(value, out pcc);
            }
            #region Stats
            else if (a[0] == "Level")
            {
                int.TryParse(value, out level);
            }
            else if (a[0] == "Hp")
            {
                int.TryParse(value, out hp);
            }
            else if (a[0] == "Strength")
            {
                int.TryParse(value, out strength);
            }
            else if (a[0] == "Magic")
            {
                int.TryParse(value, out magic);
            }
            else if (a[0] == "Skill")
            {
                int.TryParse(value, out skill);
            }
            else if (a[0] == "Speed")
            {
                int.TryParse(value, out speed);
            }
            else if (a[0] == "Luck")
            {
                int.TryParse(value, out luck);
            }
            else if (a[0] == "Defense")
            {
                int.TryParse(value, out defense);
            }
            else if (a[0] == "Resistance")
            {
                int.TryParse(value, out resistance);
            }
            else if (a[0] == "Constitution")
            {
                int.TryParse(value, out constitution);
            }
            else if (a[0] == "Movement")
            {
                int.TryParse(value, out movement);
            }

            else if (a[0] == "HpGrowth")
            {
                float.TryParse(value, out hpGrowth);
            }
            else if (a[0] == "StrengthGrowth")
            {
                float.TryParse(value, out strengthGrowth);
            }
            else if (a[0] == "MagicGrowth")
            {
                float.TryParse(value, out magicGrowth);
            }
            else if (a[0] == "SkillGrowth")
            {
                float.TryParse(value, out skillGrowth);
            }
            else if (a[0] == "SpeedGrowth")
            {
                float.TryParse(value, out speedGrowth);
            }
            else if (a[0] == "LuckGrowth")
            {
                float.TryParse(value, out luckGrowth);
            }
            else if (a[0] == "DefenseGrowth")
            {
                float.TryParse(value, out defenseGrowth);
            }
            else if (a[0] == "ResistanceGrowth")
            {
                float.TryParse(value, out resistanceGrowth);
            }
            #endregion
            else if (a[0] == "Relationships" && loadRelations)
            {
                string[] values = value.Split(',');

                foreach (string s in values)
                {
                    string[] vs = s.Split(':');
                    if (vs.Length < 2)
                    {
                        continue;
                    }

                    vs[0] = vs[0].Replace(" ", "");

                    string path = "Assets/Data/Units" + "/Unit_" + vs[0] + UnitExtention;
                    Enum.TryParse(vs[1], out Unit.Relationship.Rank rank);
                    TextAsset file = (TextAsset)UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(TextAsset));
                    Unit      unit = FileToUnit(file, false);

                    relationships.Add(unit, new Unit.Relationship(rank));
                }
            }
            else if (a[0] == "Inventory")
            {
                string[] values = value.Split(',');

                foreach (string s in values)
                {
                    string[] vs   = s.Split(':');
                    int      uses = -1;

                    if (vs.Length > 1)
                    {
                        int.TryParse(vs[1], out uses);
                    }

                    string path  = "Assets/Data/Items/" + vs[0] + ".asset";
                    Item   _item = (Item)UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(Item));

                    if (uses > -1)
                    {
                        _item.uses = uses;
                    }

                    if (_item != null)
                    {
                        inventory.Add(_item.CreateInstance());
                    }
                }
            }
        }

        growth = new Unit.Stats.GrowthRates(hpGrowth, strengthGrowth, magicGrowth, skillGrowth, speedGrowth, luckGrowth, defenseGrowth, resistanceGrowth);
        stats  = new Unit.Stats(strength, magic, skill, speed, luck, defense, resistance, constitution, movement, aid, pcc, growth);

        output.Exp = exp;
        output.SP  = sp;

        output.name              = name;
        output.unitClass         = unitClass;
        output.unitSex           = sex;
        output.alignment         = alignment;
        output.useable           = useable;
        output.affinity          = affinity;
        output.stats             = stats;
        output.stats.growthRates = growth;

        output.stats.level = level;
        output.stats.hp    = hp;

        output.relationships = relationships;

        foreach (Item i in inventory)
        {
            output.inventory.Add(i);
        }

        output.unitFile = t;

        if (log)
        {
            Debug.Log("Loaded from file " + t.name);
        }

        return(output);
    }