0

我如何让 mainChars 统计信息显示在检查器中?

using UnityEngine;
using System.Collections;

public class BaseStats : MonoBehaviour {

    public struct baseStats {
        public string name;
        public int currentLevel;
        public int targetLevel;
        public int currentHp;
        public int maxHp;
        public int currentAp;
        public int maxAp;

        public int strength;
        public int toughness;
        public int agility;
        public int intelligence;
        public int willPower;
        public int luck;

        public int attack;
        public int hitPercentage;
        public int defence;
        public int evasionPercentage;
        public int abilityAttack;
        public int abilityDefence;
        public int abilityDefencePercentage;

        public int currentExp;
        public int targetExp;

        public baseStats(string Name, int CurrentLevel, int TargetLevel, int CurrentHp, int MaxHp, int CurrentAp, int MaxAp, int Strength, int Toughness, int Agility, int Intelligence, int WillPower, int Luck, int Attack, int HitPercentage, int Defence, int EvasionPercentage, int AbilityAttack, int AbilityDefence, int AbilityDefencePercentage, int CurrentExp, int TargetExp) {
            name = Name;
            currentLevel = CurrentLevel;
            targetLevel = TargetLevel;
            currentHp = CurrentHp;
            maxHp = MaxHp;
            currentAp = CurrentAp;
            maxAp = MaxAp;

            strength = Strength;
            toughness = Toughness;
            agility = Agility;
            intelligence = Intelligence;
            willPower = WillPower;
            luck = Luck;

            attack = Attack;
            hitPercentage = HitPercentage;
            defence = Defence;
            evasionPercentage = EvasionPercentage;
            abilityAttack = AbilityAttack;
            abilityDefence = AbilityDefence;
            abilityDefencePercentage = AbilityDefencePercentage;

            currentExp = CurrentExp;
            targetExp = TargetExp;
        }
    }

    void Start() {
        baseStats mainChar = new baseStats(
            "Truth",
            98,
            99,
            7554,
            9999,
            754,
            999,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            255,
            100,
            255,
            255, 
            100,
            7773473,
            7777777);

        print(mainChar.currentHp);
    }

}

我希望它可以让每个字符成为检查器中的下拉菜单。我也想要它,这样我就可以代替将 hp 设置为当前和最大值,我也可以将其设置为每个包含当前和最大值的字符的下拉列表,对于 ap 和 exp 也是相同的。先谢谢了!!!

4

1 回答 1

1

将 struct baseStats 更改为 class baseStats 并使其可序列化,Struct是一种值类型,Unity 不会对其进行序列化,因此更改为Class. 原因[System.Serializable]是编辑器需要知道在运行预览、关闭场景、打开场景等时如何保存和恢复类数据。

public class BaseStatComponent : MonoBehaviour {
    [System.Serializable]
    public class baseStats {
       public int Example;
    }
    public baseStats mainChar; //this will be accessible to inspector now.

    void Start() {
        // This will always create a new instance of baseStats that overrides the values
        // set in inspector.  Remove this line if you want the values set in the
        // inspector to remain.
        mainChar = new baseStats { Example = 12 };
    }
}

mainChar现在将显示在检查器中。至于下拉内容,我建议阅读有关扩展 Unity 编辑器的内容。深入研究一个 SO 问题有点多。

于 2013-04-15T00:42:09.227 回答