2013-04-15 67 views
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也是如此。先謝謝了!!!

回答

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將在檢查中顯示了。至於下降的東西,我建議閱讀extending the Unity editor。這是一個bit much深入到一個SO問題。

+0

很酷的工作表示感謝!我如何得到實際值顯示,它只顯示0的一切。我應該把公共baseStats mainChar;在啓動功能? – user2279258

+0

當您運行預覽時,這些值將更改爲您在「Start()」函數中分配的值。在啓動函數中分配值的唯一問題是它們將始終覆蓋您在檢查器中設置的值。由於您在檢查器中設置了值,因此可以從'Start'中刪除baseStats構造函數,並將這些值複製到檢查器中的字段中。這些值將會保存。 – Jerdak

+0

它永遠不會改變爲我分配的D:哦,我想我會生病繼續嘗試的東西......儘管如此謝謝! – user2279258