You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

需求:为Memory Shift游戏脚本添加淘汰与获胜播报功能

Adding Elimination & Winner Voice Announcements to Memory Shift

Got it, let's get your Memory Shift game to announce when players are eliminated and when a winner is crowned. I'll modify your existing script with clear, targeted changes to add these voice features:

Modified Script with Voice Announcements

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

public class MemoryShift2 : BaseShift {
    [SerializeField] public int memoryCount = 3;
    public Dictionary<Player, int> playerVsMemCout = new Dictionary<Player, int>();
    public TileColorType[] tileColTypes = new TileColorType[0];
    public int nxtTileIndex = 0;
    public AudioSource stickAudioSource;
    public AudioClip[] gameMusicLoop;
    private int bkSoundIndex = 0;
    private int turnPlayerCount = 0;
    private Tile lastTileOnGameOver;
    public AudioSource audioSource;
    private MusicType[] currentColors;
    public int Gamemode;
    public Canvas GameModeCanvas;

    public override ShiftType shiftType {
        get { return ShiftType.Memory; }
    }

    protected override void StartGame() {
        if (PlayerPrefs.GetInt("MemoryShiftUnlockEndless", 0) == 1) {
            GameModeCanvas.gameObject.SetActive(true);
        } else if (PlayerPrefs.GetInt("MemoryShiftUnlockNormal", 0) == 1) {
            GameModeCanvas.gameObject.SetActive(true);
            EndlessMode.gameObject.SetActive(false);
        } else {
            GameModeCanvas.gameObject.SetActive(false);
            playmode = 2;
            for (var i = 0; i < getTotalPlayer.Count; i++) {
                playerVsMemCout.Add(getTotalPlayer[i], memoryCount);
            }
            StartPlayerTurn();
            GameLogic();
        }
    }

    public void ClickONSelectModeOfGame(int Gamemode) {
        this.Gamemode = Gamemode;
        playmode = 2;
        for (var i = 0; i < getTotalPlayer.Count; i++) {
            playerVsMemCout.Add(getTotalPlayer[i], memoryCount);
        }
        StartPlayerTurn();
        GameLogic();
    }

    protected sealed override void OnMoved(Tile tile) {
        if (currTileColor == tile.tileColor) {
            nxtTileIndex++;
            if (nxtTileIndex >= tileColTypes.Length) {
                if (GameManager.shiftersCount != 1 && getTotalPlayer.Count == 1) {
                    // Trigger winner announcement when only one player remains
                    AnnounceWinner();
                    return;
                }
                if (PlayerPrefs.GetInt("MemoryShiftUnlockNormal", 0) == 0 && currPlayer.playerScore >= 15) {
                    var musics = new List<MusicType>();
                    musics.Add(MusicType.EndlessModeUnlocked);
                    musics.Add(MusicType.ExplainEndlessMode);
                    AudioManger.PlayAudio(musics.ToArray(), onFinished: () => { Debug.Log("Game Win");});
                    AnnounceWinner();
                    PlayerPrefs.SetInt("MemoryShiftUnlockNormal",1);
                    return;
                } else if (Gamemode == 1 && currPlayer.playerScore >= 15) {
                    AnnounceWinner();
                    return;
                }
                if (PlayerPrefs.GetInt("MemoryShiftUnlockEndless", 0) == 0 && currPlayer.playerScore >= 20) {
                    var musics = new List<MusicType>();
                    musics.Add(MusicType.YouComplete);
                    musics.Add(currentGameModeIntro());
                    AudioManger.PlayAudio(musics.ToArray(), onFinished: () => { Debug.Log("Game Win"); });
                    PlayerPrefs.SetInt("MemoryShiftUnlockEndless", 1);
                    AnnounceWinner();
                    return;
                } else if (Gamemode == 1 && currPlayer.playerScore >= 15) {
                    AnnounceWinner();
                    return;
                }
                AudioManger.instance.increasePitch();
                if (playerVsMemCout[currPlayer] <= 9999) {
                    playerVsMemCout[currPlayer] += 1;
                }
                currPlayer.playerScore++;
                ScoreChange(currPlayer.playerScore);
                PlayerTurnChange();
            }
            currTileColor = tileColTypes[nxtTileIndex];
            currPlayer.OnHandleMove += OnHandleMove;
        } else if (getTotalPlayer.Count > 1) {
            var nxtTile = getTileForTileType(tileColTypes[nxtTileIndex]);
            HintToMissTile(nxtTile);
            // Eliminate the current player and announce it
            OnPlayerDestroy(currPlayer);
        } else if (getTotalPlayer.Count == 1) {
            var nxtTile = getTileForTileType(tileColTypes[nxtTileIndex]);
            HintToMissTile(nxtTile);
            GameOver();
        }
    }

    MusicType currentGameModeIntro() {
        if (shiftType == ShiftType.Memory) {
            return MusicType.MemoryIntro;
        }
        return MusicType.WhoIntro;
    }

    protected override void GameWin() {
        AudioManger.instance.resetPitch();
        base.GameWin();
    }

    protected virtual void PlayerTurnChange() {
        if (getTotalPlayer.Count == 1) {
            bkSoundIndex += 1;
        }
        if (getTotalPlayer.Count > 1) {
            AudioManger.instance.resetPitch();
            turnPlayerCount += 1;
            if (turnPlayerCount >= getTotalPlayer.Count) {
                bkSoundIndex += 1;
                turnPlayerCount = 0;
            }
        }
        if (stickAudioSource != null) {
            stickAudioSource.clip = gameMusicLoop[bkSoundIndex];
            if (bkSoundIndex <= 20) {
                stickAudioSource.clip = gameMusicLoop[bkSoundIndex];
            } else {
                stickAudioSource.clip = gameMusicLoop[20];
            }
            stickAudioSource.Play();
        }
        nxtTileIndex = 0;
        currPlayer = GetNxtPlayer();
        StartPlayerTurn();
        GameLogic();
    }

    private void HintToMissTile(Tile tile) {
        lastTileOnGameOver = tile;
        var musics = new List<MusicType>();
        musics.Add(MusicHandler.GetMusForTileType(tile.tileColor));
        AudioManger.PlayAudio(MusicHandler.GetPlayerFailShoud(currPlayer.playerType), onFinished: () => {
            AudioManger.PlayAudio(musics.ToArray(), onFinished: () => {
                PlayerTurnChange();
            });
        });
        tile.InVokeIndicate();
    }

    protected override void GameLogic() {
        var count = playerVsMemCout[currPlayer];
        currentColors = new MusicType[count];
        tileColTypes = new TileColorType[count];
        for (var i = 0; i < count; i++) {
            var ranType = GetRanTiColType();
            tileColTypes[i] = ranType;
            currentColors[i] = MusicHandler.GetMusForTileType(ranType, true);
        }
        currTileColor = tileColTypes[0];
        PlayLogicMusic(currentColors);
    }

    private void PlayLogicMusic(MusicType[] musics) {
        var allMusics = new List<MusicType>();
        allMusics.Add(currentPlayAnnounce());
        allMusics.Add(MusicType.Shift);
        allMusics.AddRange(musics);
        AudioManger.PlayAudio(allMusics.ToArray(), onStartCurrMusic: (type) => {
            OnTileCallFunc(type);
        }, onFinished: () => {
            OnTileDone?.Invoke();
            currPlayer.OnHandleMove += OnHandleMove;
            GameManager.CanPlay = true;
        });
    }

    // Restored and enhanced OnPlayerDestroy with elimination announcement
    protected sealed override void OnPlayerDestroy(Player player) {
        playerVsMemCout.Remove(player);
        base.OnPlayerDestroy(player);

        // Play elimination voice announcement
        var eliminationSounds = new List<MusicType>();
        eliminationSounds.Add(MusicType.SorryPlayer);
        eliminationSounds.Add(MusicHandler.GetPlayerNumber(player.playerType));
        eliminationSounds.Add(MusicType.YourOut);

        // If only one player remains after elimination, announce winner immediately
        if (getTotalPlayer.Count == 1) {
            eliminationSounds.Add(MusicType.AndTheWinnerIs);
            eliminationSounds.Add(MusicHandler.GetPlayerNumber(getTotalPlayer[0].playerType));
            eliminationSounds.Add(MusicType.YouWin);
            AudioManger.PlayAudio(eliminationSounds.ToArray(), onFinished: () => {
                GameWin();
            });
        } else {
            // Announce elimination and proceed to next turn
            eliminationSounds.Add(MusicType.NextPlayerTurn);
            AudioManger.PlayAudio(eliminationSounds.ToArray(), onFinished: () => {
                PlayerTurnChange();
            });
        }
    }

    protected sealed override void GameOver() {
        base.GameOver();
        if (GameManager.shiftersCount > 1) {
            var musics = new List<MusicType>();
            var playerNumber = MusicHandler.GetNumberSound(GameManager.shiftersCount);
            musics.Add(MusicType.SorryPlayer);
            musics.Add(playerNumber);
            musics.Add(MusicType.YourOut);
            musics.Add(MusicType.RepeatThatRoundAgain);
            AudioManger.PlayAudio(musics.ToArray(), onFinished: () => {
                GameLogic();
            });
        } else {
            var musics = new List<MusicType>();
            musics.Add(MusicType.Play1Fail);
            musics.AddRange(currentColors);
            musics.Add(MusicType.YouHave);
            musics.AddRange(MusicHandler.GetMusForNumber(currPlayer.playerScore));
            musics.Add(MusicType.Shifts);
            musics.Add(MusicType.GameOver);
            AudioManger.PlayAudio(musics.ToArray());
        }
    }

    MusicType currentPlayAnnounce() {
        var _ind = totalPlayers.IndexOf(currPlayer);
        if (_ind.Equals(0)) return MusicType.One;
        else if (_ind.Equals(1)) return MusicType.Two;
        else if (_ind.Equals(2)) return MusicType.Three;
        else if (_ind.Equals(3)) return MusicType.Four;
        else if (_ind.Equals(4)) return MusicType.Five;
        else return MusicType.Six;
    }

    // Enhanced winner announcement method
    void AnnounceWinner() {
        var winnerSounds = new List<MusicType>();
        winnerSounds.Add(MusicType.AndTheWinnerIs);
        winnerSounds.Add(MusicHandler.GetPlayerNumber(currPlayer.playerType));
        winnerSounds.Add(MusicType.YouWin);
        winnerSounds.Add(MusicType.CelebrationMusic); // Add a celebration track if you have one

        AudioManger.PlayAudio(winnerSounds.ToArray(), onFinished: () => {
            GameWin();
        });
    }
}

Key Changes Explained

  • Restored & Enhanced OnPlayerDestroy: This method now plays a clear elimination message ("Sorry Player X, you're out!"). If only one player is left after elimination, it immediately transitions to a winner announcement.
  • Enhanced AnnounceWinner: Updated to play a full winner sequence (e.g., "And the winner is... Player 1! You win!") followed by celebration audio (adjust MusicType.CelebrationMusic to match your actual audio enum).
  • Triggered Winner Announcement: Added calls to AnnounceWinner() in all scenarios where the game is won (score thresholds, last remaining player).
  • Smooth Flow: Ensured voice announcements play before proceeding to game end or next turn, so players hear the feedback clearly.

Make sure your MusicType enum includes all the required values like YourOut, AndTheWinnerIs, YouWin, NextPlayerTurn, and CelebrationMusic to match the script.

内容的提问来源于stack exchange,提问作者Samlaptop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:41:04