为什么这个关于改变变量的脚本不起作用?统一 2D - C#

问题描述 投票:0回答:2

我正在重新创建 FNF,这是 FNF 中出现的音符。

问题: 'Type' 变量的开关根本不起作用,它只适用于 'Left' Type。 是的,我检查过,所有游戏对象都有 Type var 和一些东西。

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

public class Note : MonoBehaviour
{
    public float position;
    public KeyCode keyCode;
    public float speed;
    public string Type;
    public Color color;
    void Update()
    {
        transform.position += new Vector3(0, speed * Time.deltaTime, 0);
        
        if (string.IsNullOrEmpty(Type))
        {
            switch (Type)
            {
                case "Left":
                    keyCode = KeyCode.A;
                    gameObject.GetComponent<SpriteRenderer>().color = Color.magenta;
                    color = Color.magenta; 
                    break;
            
                case "Right":
                    keyCode = KeyCode.D;
                    gameObject.GetComponent<SpriteRenderer>().color = Color.red;
                    color = Color.red; 
                    break;
                case "Up":
                    keyCode = KeyCode.W;
                    gameObject.GetComponent<SpriteRenderer>().color = Color.green;
                    color = Color.green; 
                    break;
            
                case "Down":
                    keyCode = KeyCode.S;
                    gameObject.GetComponent<SpriteRenderer>().color = Color.cyan;
                    color = Color.cyan; 
                    break;
            }
        }
       
        if (transform.position.y > 6)
        {
            Destroy(gameObject);
            GameObject.Find("chart_loader").GetComponent<chart>().notes.Remove(gameObject.GetComponent<Note>());
        }
    }
}

我尝试将其更改为 if's,但它仅适用于 Left 类型。

c# unity3d
2个回答
0
投票
if (string.IsNullOrEmpty(Type))

在这里你说如果你的字符串

Type
是空的或者空的那么只有你去切换案例但是你需要像这样做相反的事情

if (!string.IsNullOrEmpty(Type))

你也可以像这样做得更干净

if (string.IsNullOrEmpty(Type))
    return;

switch (Type)
{
    case "Left":
        keyCode = KeyCode.A;
        gameObject.GetComponent<SpriteRenderer>().color = Color.magenta;
        color = Color.magenta; 
        break;
...

0
投票

没用。只更改了最后生成的注释,如果有帮助,这里是 chart_loader:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class chart : MonoBehaviour
{   
      public List<Note> notes;
      public string MusicName;
      public List<AudioClip> voices;
      public List<AudioClip> backs;
      public AudioClip voice;
      public AudioClip back;
      public GameObject setinha;
      public TextAsset music_chart;
      public string c0;
      public string c1;
      public float beat;
      public float speed;
      public string difficulty;

public List<GameObject> arrows;

void Start()
{
    LoadChart(music_chart);
}

void Update()
{
    beat += Time.deltaTime;
    foreach (var note in notes)
    {
        note.speed = speed;
        if (beat - note.position-speed/2 < 1.5)
        {
            note.gameObject.SetActive(true);
            note.transform.position = new Vector3(note.transform.position.x, -5.78f, 0);
            
        }
    }
}

public void LoadChart(TextAsset chartFile)
{
    // Limpa a lista de notas existente
    notes.Clear();

    // Carrega o arquivo de texto do Chart
    string[] lines = chartFile.text.Split('\n');

    // Lê cada linha do arquivo e cria uma nova nota
    foreach (string line in lines)
    {
        // Divide a linha em partes separadas por vírgulas
        string[] parts = line.Split(':');
        if (parts[0] == "note")
        {
            GameObject nSetinha = Instantiate(setinha);

            // Cria uma nova nota e define suas propriedades
            Note note = nSetinha.GetComponent<Note>();
            note.Type = parts[2];
            nSetinha.name = note.Type;
            note.position = float.Parse(parts[1]);
            //note.keyCode = (KeyCode)System.Enum.Parse(typeof(KeyCode), parts[2]);
            switch (nSetinha.name)
            {
                case "Left":
                    note.keyCode = KeyCode.A;
                    nSetinha.GetComponent<SpriteRenderer>().color = Color.magenta;
                    note.color = Color.magenta; 
                    print("yes001");
                    break;
        
                case "Right":
                    note.keyCode = KeyCode.D;
                    nSetinha.GetComponent<SpriteRenderer>().color = Color.red;
                    note.color = Color.red; 
                    print("yes002");
                    break;
                case "Up":
                    note.keyCode = KeyCode.W;
                    nSetinha.GetComponent<SpriteRenderer>().color = Color.green;
                    note.color = Color.green; 
                    print("yes003");
                    break;
        
                case "Down":
                    note.keyCode = KeyCode.S;
                    nSetinha.GetComponent<SpriteRenderer>().color = Color.cyan;
                    note.color = Color.cyan; 
                    print("yes004");
                    break;
            }
            print(note.Type);
            print(parts[2]);
            if (note.position - speed / 2 - beat > 1.5)
            {
                nSetinha.SetActive(false);
            }
            else
            {
                nSetinha.SetActive(true);
            }


            foreach (var arrow in arrows)
            {
                if (arrow.name ==nSetinha.name)
                {
                    print("new arrow");
                    nSetinha.transform.position = new Vector3(arrow.transform.position.x, -6.8f, 0);
                }
            }
            notes.Add(note);
            
        }

        if (parts[0] == "music")
        {
            MusicName = parts[1];
            foreach (var musicss in voices)
            {
                print("voices: "+musicss.ToString());
                print(MusicName);
                print("is equal?? "+musicss.ToString()==MusicName);
                if (musicss.ToString() == MusicName)
                {
                    voice = musicss;
                    print("true");
                }
            }
            
            foreach (var musicss in backs)
            {
                print("instrumental: "+musicss.ToString());
                print(MusicName);
                print("is equal?? "+musicss.ToString()==MusicName);
                if (musicss.ToString() == MusicName)
                {
                    back = musicss;
                    print("true");
                }
            }
        }

        if (parts[0] == "chars")
        {
            c0 = parts[1];
            c1 = parts[2];
        }
        // Adiciona a nota à lista de notas
        if (parts[0] == "speed")
        {
            speed = float.Parse(parts[1]);
        }
        if (parts[0] == "diff")
        {
            difficulty = parts[1];
        }
    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.