簡體   English   中英

unity C#:多次更改標簽的 state

[英]Unity C# : Changing state of tags multiple time

我對統一和 c# 都是新手。 我正在嘗試制作一個基於文本的游戲來進行練習。 問題是不同的 state 游戲有不同數量的按鈕,我想用標簽在這些之間切換,但它只適用於第一個 state 然后當我嘗試轉到下一個 Z9ED39E2EA931586B6A985A6942EF57 按鈕時

這是代碼:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Textadvanture0 : MonoBehaviour
{

    [SerializeField]
    Text text0;

    [SerializeField]
    Text text1;

    [SerializeField]
    Text text2;

    [SerializeField]
    Text text3;

    [SerializeField]
    Text text4;

    [SerializeField]
    State Startingstate;


    State state;

    string Flag;

    public void ManageStates(int cnt)
    {
        var nextstate = state.GetNextStates();
        if (cnt == 1) //left
        {
            state= nextstate[0];
        }
    }

    void Start()
    {
        state = Startingstate;
        FlagCheck();

    }


    void Update()
    {

       Debug.Log(state.GetChoiceN());

       FlagCheck();


        text0.text = state.GetStateStory(0);
        text1.text = state.GetStateStory(1);
        text2.text = state.GetStateStory(2);
        text3.text = state.GetStateStory(3);
        text4.text = state.GetStateStory(4);

    }


    void FlagDown()
    {
        GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag(Flag);

        foreach (GameObject go in gameObjectArray)
        {
            go.SetActive(false);
        }
    }
    void FlagOn()
    {
        GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag(Flag);

        foreach (GameObject go in gameObjectArray)
        {
            go.SetActive(true);
        }
    }

    void FlagCheck()
    {

        if (state.Choice == 1)
        {

            Flag = ("Mono Choice");
            FlagOn();

            Flag = ("Dual Choice");
            FlagDown();

            Flag = ("Triple Choice");
            FlagDown();

        }
        else if (state.Choice == 2)
        {
            Flag = ("Mono Choice");
            FlagDown();

            Flag = ("Dual Choice");
            FlagOn();

            Flag = ("Triple Choice");
            FlagDown();


        }
        else if (state.Choice == 3)
        {
            Flag = ("Mono Choice");
            FlagDown();

            Flag = ("Dual Choice");
            FlagDown();

            Flag = ("Triple Choice");
            FlagOn();

        }
    }
}

和 State class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "State")]
public class State : ScriptableObject
{
  [TextArea(10,14)]  [SerializeField] string[] StoryText;
  [SerializeField]
   public int Choice;
  [SerializeField]
  State [] Nextstates;



  public string GetStateStory(int Counter)
  {

      return StoryText[Counter];
  }


  public int GetChoiceN()
  {
      return Choice;
  }
  public State[] GetNextStates()
  {
      return Nextstates;
  }

}

順便說一句,我得到的索引也超出了數組錯誤的范圍,但它似乎不會以任何方式影響游戲,任何解決方案都會受到贊賞:)

您遇到的異常可能是由於您傳遞給TextArea的 arguments ,即 10 到 14。如果您有小文本塊,則可能沒有 14 行。

在沒有看到你如何在 Unity 中使用這些類的情況下,我猜測為什么沒有更新狀態是因為你在 2 個地方設置了state的值, Start方法只在 object 啟用后調用一次,在您的ManageStates方法中,該方法僅用於處理cnt1

如果 state.GetNextStates()[0] 的值與已加載的 state 的值相同,則不會看到更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM