创建可隐藏的音乐播放器 UI

本教程将指导您在 Roblox Studio 中创建一个简单的音乐播放器 UI,并实现隐藏/显示功能,让您的 UI 更具交互性。

1. 创建 UI 元素

local ScreenGui = Instance.new('ScreenGui')
ScreenGui.Name = 'MusicPlayer'

local Frame = Instance.new('Frame')
Frame.Position = UDim2.new(0.5, -50, 0.5, -25)
Frame.Size = UDim2.new(0, 100, 0, 90)
Frame.BackgroundColor3 = Color3.new(1, 1, 1)
Frame.BackgroundTransparency = 0.5
Frame.Active = true
Frame.Draggable = true
Frame.Parent = ScreenGui

-- 创建文本框
local TextBox = Instance.new('TextBox')
TextBox.Size = UDim2.new(1, -20, 0, 30)
TextBox.Position = UDim2.new(0, 10, 0, 10)
TextBox.PlaceholderText = '输入音乐代码'
TextBox.ClearTextOnFocus = false
TextBox.Parent = Frame

-- 创建播放按钮
local PlayButton = Instance.new('TextButton')
PlayButton.Text = '播放音乐'
PlayButton.Size = UDim2.new(1, -20, 0, 30)
PlayButton.Position = UDim2.new(0, 10, 1, -40)
PlayButton.Parent = Frame

-- 创建隐藏按钮
local HideButton = Instance.new('TextButton')
HideButton.Text = '隐藏'
HideButton.Size = UDim2.new(0, 50, 0, 30)
HideButton.Position = UDim2.new(0, 0, 0, 50)
HideButton.Parent = Frame

-- 创建展开/隐藏按钮
local ToggleButton = Instance.new('TextButton')
ToggleButton.Text = '展开'
ToggleButton.Size = UDim2.new(0, 50, 0, 30)
ToggleButton.Position = UDim2.new(0, 0, 0, 0)
ToggleButton.Parent = Frame

-- 添加播放器脚本
local MusicPlayer = Instance.new('Sound', game.Workspace)
MusicPlayer.Volume = 1
MusicPlayer.Playing = false

2. 添加隐藏/显示逻辑

-- 创建函数用于隐藏UI
local function HideUI()
    for _, child in ipairs(ScreenGui:GetChildren()) do
        if child ~= HideButton and child ~= ToggleButton then
            child.Visible = false
        end
    end
end

-- 创建函数用于显示UI
local function ShowUI()
    for _, child in ipairs(ScreenGui:GetChildren()) do
        child.Visible = true
    end
end

-- 添加点击隐藏按钮的事件
HideButton.MouseButton1Click:Connect(function()
    HideUI()
end)

-- 添加点击展开/隐藏按钮的事件
local isHidden = false

ToggleButton.MouseButton1Click:Connect(function()
    if isHidden then
        ShowUI()
        ToggleButton.Text = '隐藏'
        isHidden = false
    else
        HideUI()
        ToggleButton.Text = '展开'
        isHidden = true
    end
end)

3. 添加音乐播放功能

PlayButton.MouseButton1Click:Connect(function()
    local Code = TextBox.Text
    if Code ~= '' then
        MusicPlayer.SoundId = 'rbxassetid://' .. Code
        MusicPlayer:Play()
    end
end)

4. 将 UI 放置在屏幕最左边

Frame.Position = UDim2.new(0, 0, 0.5, -25)

5. 添加到游戏

ScreenGui.Parent = game.Players.LocalPlayer:WaitForChild('PlayerGui')

完成

现在你已经创建了一个可隐藏的音乐播放器 UI。当你运行游戏时,你会看到一个包含文本框、播放按钮、隐藏按钮和展开/隐藏按钮的 UI。点击隐藏按钮可以隐藏 UI,点击展开/隐藏按钮可以切换 UI 的显示状态。

提示:

  • 你可以使用 Roblox 资源库找到你想要的音乐代码。
  • 你可以根据自己的需要调整 UI 的大小和位置。
  • 你还可以添加其他功能,例如暂停音乐、调整音量等。

希望这个教程对你有帮助!

Roblox Studio: 创建可隐藏的音乐播放器 UI

原文地址: https://www.cveoy.top/t/topic/narT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录