今日気づきました。
UnityEventはpublicじゃないとnull referenceになってしまいますね。
なんでだろ、publicにして[System.NonSerialized]も使えない。
Inspectorからの操作を誤操作を無くしたくprivateにしたいのですが…
[HideInspector]でいけました!
スコアに応じてパーセント表示を更新するコードを書きました。
ちょっと読みにくくなっているので綺麗に整形できればいいのですが..
UnityEventに慣れる為labelクラスのメソッドはUnityEventに登録させて呼びました。
今回はシーンに一つだけのクラスなので直にメソッドを読んでもよかったかもしれません。
UpdateLevelPercentageメソッドのUpdatePercentage.Invoke();でLabelManagerクラスのメソッドを呼びNGUI UISpriteの表示を変えています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
GameControllerクラス public UnityEvent UpdatePercentage; public static int NextLevelPercentage{private set; get;} void Start() { score.AddListener(AddScore); UpdateScoreAndLevel.AddListener(labelManager.updateLabelAction); UpdatePercentage.AddListener(labelManager.updatePercentageAction); } void AddScore(int chickScore) { Score += chickScore; Point += chickScore * 10; Debug.Log("Score: " + Score); UpdateLevelPercentage(); UpdateLevel(); //After update score and level you can call label action UpdateScoreAndLevel.Invoke(); } void UpdateLevelPercentage() { int nextLevel = Level + 1; float nextLevelScore = (float)(DATA.NextLevelScore[nextLevel]); float score = (float)Score; float untilNextScorePercentage = score / nextLevelScore; //Debug.Log("untilNextScorePercentage: " + untilNextScorePercentage); int storeNextLevelPercentage = NextLevelPercentage; //整数一桁を返す if(untilNextScorePercentage > 0.9 && untilNextScorePercentage < 1.0) NextLevelPercentage = 8; else NextLevelPercentage = Mathf.RoundToInt((untilNextScorePercentage) * 10); //整数を返す 0,2,4,6,8,10 if(NextLevelPercentage % 2 != 0) NextLevelPercentage -= 1; //割り切れた時を考慮 , It"s level up timing if(NextLevelPercentage >= 10) { NextLevelPercentage = 10; } Debug.Log("NextLevelPercentage: " + NextLevelPercentage); //更新した時だけラベル更新 またはレベルアップした次にかごに入る時 if(storeNextLevelPercentage < NextLevelPercentage || NextLevelPercentage == 10 || storeNextLevelPercentage == 10) UpdatePercentage.Invoke(); } void OnDestroy() { Debug.Log("GameController OnDestroy"); score.Dispose(); createCharNum.Dispose(); touchPos.Dispose(); UpdateScoreAndLevel.RemoveAllListeners(); UpdatePercentage.RemoveAllListeners(); } |