BasePlayerGui
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
BasePlayerGui là một lớp trừu tượng mà tất cả các lớp lưu trữ vẽ GUI thừa kế từ.
Tóm Tắt
Phương Pháp
Trả về một danh sách tất cả GuiObject các ví dụ chiếm điểm được cho trên màn hình.
Thuộc Tính
Phương Pháp
GetGuiObjectsAtPosition
Lấy vị trí màn hình và trả lại một danh sách tất cả các GuiObject đang chiếm vị trí màn hình đó, sắp xếp theo thứ tự xuất hiện trên màn hình từ trên xuống dưới như là chỉ mục đầu tiên và cuối cùng, lần lượt.
Trường hợp sử dụng chính là nhận được các đối tượng GUI dưới chuột hoặc các đầu vào chạm của người chơi để làm những thứ như cho phép chọn hoặc làm nổi bật.Các hiệu ứng này có thể đã được thực hiện bằng cách sử dụng GuiObject.MouseEnter và GuiObject.MouseLeave nhưng điều này yêu cầu nhà phát triển theo dõi các sự kiện này cho các đối tượng UI của họ suốt thời gian ngay cả khi họ chỉ cần chức năng này trong các trường hợp cụ thể.
Vì lớp con của BasePlayerGui kế thừa chức năng này, nó có thể bị kích hoạt bởi các đối tượng lớp như PlayerGui và StarterGui các thư mục.
Tham Số
Vị trí x trên màn hình so với góc trên cùng bên trái sau khi áp dụng GuiInset được áp dụng.
Lợi Nhuận
Một bảng các GuiObject ví dụ chiếm không gian màn hình được cung cấp.
Mẫu mã
All GUIs returned by BasePlayerGui:GetGuiObjectsAtPosition() are 'cloned' by the highlightAsFrame() local function, which creates a Frame GUI positioned on top of the specified GUI that is semi-transparent and the same GuiBase2d.AbsoluteSize and GuiBase2d.AbsolutePosition.
These highlights are added to the HighlightsContainer ScreenGui in the Highlight folder of the player's PlayerGui folder. Both are created by the code sample. All highlight GUIs are Instance:Destroy()ed every time highlightGui() executes.
Note that HighlightContainer's ScreenGui.DisplayOrder is 99999, a large number, so that it is unlikely any other GUI will render on top of the highlight GUIs.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
-- Create a Folder and ScreenGui to contain the highlight Frames
local highlights = Instance.new("Folder")
highlights.Name = "Highlights"
highlights.Parent = playerGui
local highlightsContainer = Instance.new("ScreenGui")
highlightsContainer.Name = "Container"
highlightsContainer.Parent = highlights
highlightsContainer.DisplayOrder = 99999
-- Creates a semi-transparent yellow Frame on top of the gui with the same AbsoluteSize and AbsolutePosition
local function highlightAsFrame(gui)
local highlight = Instance.new("Frame")
highlight.Name = "Highlight"
highlight.Parent = highlightsContainer
highlight.Size = UDim2.new(0, gui.AbsoluteSize.X, 0, gui.AbsoluteSize.Y)
highlight.Position = UDim2.new(0, gui.AbsolutePosition.X, 0, gui.AbsolutePosition.Y)
highlight.BackgroundColor3 = Color3.fromRGB(255, 255, 10) -- Yellow
highlight.BackgroundTransparency = 0.75
highlight.BorderSizePixel = 0
highlight.LayoutOrder = gui.LayoutOrder - 1
end
-- Use GetGuiObjectsAtPosition to get and highlight all GuiObjects at the input's position
local function highlightGui(input, _gameProcessed)
local pos = input.Position
local guisAtPosition = playerGui:GetGuiObjectsAtPosition(pos.X, pos.Y)
highlightsContainer:ClearAllChildren()
for _, gui in ipairs(guisAtPosition) do
if gui:IsA("GuiObject") then
highlightAsFrame(gui)
end
end
end
-- Fire highlightGui on InputBegan if input is of type MouseButton1 of Touch
local function InputBegan(input, gameProcessed)
local inputType = input.UserInputType
local touch = Enum.UserInputType.Touch
local mouse1 = Enum.UserInputType.MouseButton1
if inputType == touch or inputType == mouse1 then
highlightGui(input, gameProcessed)
end
end
UserInputService.InputBegan:Connect(InputBegan)