Trong hiệu ứng động, tweening là quá trình tạo khung trung gian giữa hai điểm chính trong một chuỗi.Khi thiết kế giao diện người dùng, bạn có thể sử dụng tweening để chuyển sang một trạng thái khác một cách mượt mà, chẳng hạn như:
- Tăng kích thước của nút một cách mượt mà khi người dùng chọn nó.
- Tùy chọn menu trượt trong và ra khỏi các cạnh màn hình.
- Dần dần hoạt hình một thanh sức khỏe giữa hai chiều khi người dùng nhận được một sức khỏe tăng.
Thiếu niên một thuộc tính
Vị trí
Để tween vị trí của một : :
- Đặt AnchorPoint cho đối tượng.
- Chuyển một TweenInfo và vị trí mục tiêu sang TweenService:Create().
- Chơi tween với Tween:Play() .
Các đoạn mã sau di chuyển một ImageLabel bên trong một ScreenGui vào trung tâm chính xác của màn hình:
UI Tween - Vị trí
local TweenService = game:GetService("TweenService")local Players = game:GetService("Players")local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")local ScreenGui = PlayerGui:WaitForChild("ScreenGui")local object = ScreenGui:WaitForChild("ImageLabel")object.AnchorPoint = Vector2.new(0.5, 0.5)local targetPosition = UDim2.new(0.5, 0, 0.5, 0)local tweenInfo = TweenInfo.new(2)local tween = TweenService:Create(object, tweenInfo, {Position = targetPosition})tween:Play()
Kích thước
Để tween kích thước của một :
- Gắn một UIAspectRatioConstraint vào đối tượng để duy trì tỷ lệ phần trăm thiết kế khi chuyển đổi.
- Chuyển một TweenInfo và kích thước mục tiêu sang TweenService:Create() .
- Chơi tween với Tween:Play() .
Các đoạn mã sau đây thu nhỏ phần ImageLabel bên trong một ScreenGui đến 40% chiều rộng hoặc chiều cao của màn hình (trong đó nhỏ hơn) từ điểm neo trung tâm của đối tượng:
UI Tween - Kích thước
local TweenService = game:GetService("TweenService")local Players = game:GetService("Players")local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")local ScreenGui = PlayerGui:WaitForChild("ScreenGui")local object = ScreenGui:WaitForChild("ImageLabel")object.AnchorPoint = Vector2.new(0.5, 0.5)local aspectRatioConstraint = Instance.new("UIAspectRatioConstraint")aspectRatioConstraint.Parent = objectlocal targetSize = UDim2.new(0.4, 0, 0.4, 0)local tweenInfo = TweenInfo.new(2)local tween = TweenService:Create(object, tweenInfo, {Size = targetSize})tween:Play()
Vòng xoay
Để tween the xoay của một GuiObject :
- Đặt AnchorPoint cho đối tượng để xoay quanh.
- Xác định mục tiêu Rotation cho đối tượng.
- Chuyển một TweenInfo và vòng xoay mục tiêu sang TweenService:Create().
- Chơi tween với Tween:Play() .
UI Tween - Kích thước
local TweenService = game:GetService("TweenService")local Players = game:GetService("Players")local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")local ScreenGui = PlayerGui:WaitForChild("ScreenGui")local object = ScreenGui:WaitForChild("ImageLabel")object.AnchorPoint = Vector2.new(0.5, 0.5)local targetRotation = 45local tweenInfo = TweenInfo.new(2)local tween = TweenService:Create(object, tweenInfo, {Rotation = targetRotation})tween:Play()
Độ trong suốt
Nhiều thuộc tính kiểm soát độ trong suốt của giao diện người dùng, tùy thuộc vào loại đánh máytượng.Bạn có thể chuyển tiếp mỗi thuộc tính này riêng lẻ hoặc kết hợp thông qua một chuyển tiếp nhiều thuộc tính .Ngoài ra, bạn có thể tween sự minh bạch tổng thể của một đối tượng bằng cách đặt nó bên trong một CanvasGroup và tween nhóm của nó GroupTransparency .
UI Tween - Bản minh bạch hình ảnh
local TweenService = game:GetService("TweenService")local Players = game:GetService("Players")local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")local ScreenGui = PlayerGui:WaitForChild("ScreenGui")local object = ScreenGui:WaitForChild("ImageLabel")local targetTransparency = 0.8local tweenInfo = TweenInfo.new(2)local tween = TweenService:Create(object, tweenInfo, {ImageTransparency = targetTransparency})tween:Play()
UI Tween - Bản minh bạch của Nhóm Canvas
local TweenService = game:GetService("TweenService")local Players = game:GetService("Players")local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")local ScreenGui = PlayerGui:WaitForChild("ScreenGui")local canvasGroup = ScreenGui:WaitForChild("CanvasGroup")local targetTransparency = 0.8local tweenInfo = TweenInfo.new(2)local tween = TweenService:Create(canvasGroup, tweenInfo, {GroupTransparency = targetTransparency})tween:Play()
Màu
Nhiều thuộc tính kiểm soát màu UI, tùy thuộc vào loại đánh máytượng.Bạn có thể chuyển tiếp mỗi thuộc tính này riêng lẻ hoặc kết hợp thông qua một chuyển tiếp nhiều thuộc tính .Ngoài ra, bạn có thể tween màu tổng thể của một đối tượng bằng cách đặt nó bên trong một CanvasGroup và tween nhóm của nó GroupColor3 .
UI Tween - Màu hình ảnh
local TweenService = game:GetService("TweenService")local Players = game:GetService("Players")local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")local ScreenGui = PlayerGui:WaitForChild("ScreenGui")local object = ScreenGui:WaitForChild("ImageLabel")local targetColor = Color3.fromRGB(255, 0, 0)local tweenInfo = TweenInfo.new(2)local tween = TweenService:Create(object, tweenInfo, {ImageColor3 = targetColor})tween:Play()
UI Tween - Màu nhóm Canvas
local TweenService = game:GetService("TweenService")local Players = game:GetService("Players")local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")local ScreenGui = PlayerGui:WaitForChild("ScreenGui")local canvasGroup = ScreenGui:WaitForChild("CanvasGroup")local targetColor = Color3.fromRGB(255, 0, 0)local tweenInfo = TweenInfo.new(2)local tween = TweenService:Create(canvasGroup, tweenInfo, {GroupColor3 = targetColor})tween:Play()
Đường viền
Nhiều điều khiển biên giới UI của nhiều thuộc tính, tùy thuộc vào loại đối đánh máy.
Ngoài ra, bạn có thể áp dụng một đứa con UIStroke và tween độ dày, màu sắc và/hoặc sự minh bạch của nó.
Đối tượng UI | Thuộc tính |
---|---|
UIStroke | Color , Thickness , Transparency |
UI Tween - Màu & Độ dày của UIStroke
local TweenService = game:GetService("TweenService")local Players = game:GetService("Players")local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")local ScreenGui = PlayerGui:WaitForChild("ScreenGui")local object = ScreenGui:WaitForChild("TextLabel")local stroke = Instance.new("UIStroke")stroke.Color = Color3.fromRGB(255, 255, 255)stroke.Thickness = 5stroke.Parent = objectlocal targetColor = Color3.fromRGB(255, 0, 0)local targetThickness = 10local tweenInfo = TweenInfo.new(2)local tween = TweenService:Create(stroke, tweenInfo, {Color = targetColor, Thickness = targetThickness})tween:Play()
Teen nhiều thuộc tính
Bạn có thể kết hợp bất kỳ ai trong số thiếu niên một thuộc tính duy nhất vào thiếu niên phức tạp hơn bằng cách chuyển nhiều thuộc tính mục tiêu đến TweenService:Create() , ví dụ vị trí + quay hoặc kích thước + sự minh bạch .
UI Tween - Vị trí & Rotation
local TweenService = game:GetService("TweenService")local Players = game:GetService("Players")local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")local ScreenGui = PlayerGui:WaitForChild("ScreenGui")local object = ScreenGui:WaitForChild("ImageLabel")object.AnchorPoint = Vector2.new(0.5, 0.5)local targetPosition = UDim2.new(0.5, 0, 0.5, 0)local targetRotation = 45local tweenInfo = TweenInfo.new(2)local tween = TweenService:Create(object, tweenInfo, {Position = targetPosition, Rotation = targetRotation})tween:Play()
UI Tween - Kích thước & Sự minh bạch
local TweenService = game:GetService("TweenService")local Players = game:GetService("Players")local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")local ScreenGui = PlayerGui:WaitForChild("ScreenGui")local object = ScreenGui:WaitForChild("ImageLabel")object.AnchorPoint = Vector2.new(0.5, 0.5)local aspectRatioConstraint = Instance.new("UIAspectRatioConstraint")aspectRatioConstraint.Parent = objectlocal targetSize = UDim2.new(0.4, 0, 0.4, 0)local targetTransparency = 0.8local tweenInfo = TweenInfo.new(2)local tween = TweenService:Create(object, tweenInfo, {Size = targetSize, ImageTransparency = targetTransparency})tween:Play()
Chuỗi trẻ vịt
Bạn có thể chuỗi các hoạt hình UI xảy ra theo nhau bằng cách chơi các thiếu niên tiếp theo trên sự kiện Completed của thiếu niên trước.Ví dụ, kịch bản sau đây di chuyển một đối tượng vào trung tâm của màn hình, sau đó xoay nó 45°.
Thứ tự Tween UI
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
local ScreenGui = PlayerGui:WaitForChild("ScreenGui")
local object = ScreenGui:WaitForChild("ImageLabel")
object.AnchorPoint = Vector2.new(0.5, 0.5)
local targetPosition = UDim2.new(0.5, 0, 0.5, 0)
local targetRotation = 45
local tweenInfo = TweenInfo.new(2)
local positionTween = TweenService:Create(object, tweenInfo, {Position = targetPosition})
local rotationTween = TweenService:Create(object, tweenInfo, {Rotation = targetRotation})
-- Lúc đầu chơi vị trí tween
positionTween:Play()
-- Chơi tween quay khi hoàn thành tween vị trí
positionTween.Completed:Connect(function()
rotationTween:Play()
end)
Tùy chọn dễ dàng
Sử dụng các tùy chọn nới lỏng của TweenInfo, bạn có thể kiểm soát kiểu nới lỏng style và hướng direction của hoạt hình UI.
Phong cách
Enum.EasingStyle đặt tỷ lệ interpolation từ đầu đến kết thúc. Mặc định, kiểu dễ dàng được đặt thành Enum.EasingStyle.Quad .
Phong cách | Mô tả |
---|---|
Liên tục | Di chuyển với tốc độ không đổi. |
Sinh | Tốc độ được xác định bởi một làn sóng sine cho một chuyển động nhẹ nhàng. |
Bốn | Tương tự như Sine nhưng với một đường cong sắc nét hơn một chút dựa trên sự phân tích hình học bậc hai. |
Khối | Tương tự như Quad nhưng với một đường cong sắc nét hơn một chút dựa trên sự phân tán hình học. |
Phần tư | Tương tự như Hình khối nhưng với một đường cong sắc bén hơn dựa trên sự phân tích bậc thứ tư. |
Quint nhân | Tương tự như Quart nhưng với một đường cong sắc bén hơn dựa trên quintic interpolation. |
Hoạt động nhân lên | Đường cong sắc nhọn dựa trên sự phân tán lũy thừa. |
Vòng lặp | Theo một hình cung tròn, như vậy tốc độ gia tăng sẽ nhanh hơn và tốc độ giảm chậm hơn so với Quint hoặc Exponential . |
Quay lại | Hơi vượt quá mục tiêu, sau đó quay trở lại vị địa điểm. |
Bật lên | Bật lên lùi nhiều lần sau khi đạt đến mục tiêu, trước khi cuối cùng được giải quyết. |
Chắc chắn | Di chuyển như thể được gắn vào một dây cao su, vượt quá mục tiêu vài lần. |

Phong cách dễ dàng - Hình khối
local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Cubic)local tween = TweenService:Create(object, tweenInfo, {Rotation = 45})
Hướng
Enum.EasingDirection xác định cách phép biến đổi kiểu dễ dàng áp dụng cho một đối tượng, với một mặc định là Thoát .Lưu ý rằng một thiếu niên với kiểu Linear dễ dàng phong cách không bị ảnh hưởng, vì phương trình nhân tuyến tính là không thay đổi từ đầu đến kết thúc.
Hướng | Mô tả |
---|---|
In | Phong cách nới lỏng áp dụng theo hướng tiến. |
Thoát | Phong cách làm dịu áp dụng theo chiều ngược lại. |
Thoát | Phong cách nới lỏng áp dụng trước cho nửa đầu tiên và ngược lại cho nửa thứ hai. |
Hướng dễ dàng - InOut
local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)local tween = TweenService:Create(object, tweenInfo, {Rotation = 45})
Hoạt hình văn bản
Bạn có thể dễ dàng cải thiện giao diện người dùng dựa trên văn bản, chẳng hạn như banner cảnh cắt, hướng dẫn người chơi và lời nhắc, với hiệu ứng hoạt hình.
Hiệu ứng máy chữ
Hiệu ứng "máy đánh chữ" là lý tưởng cho TextLabels nói một câu chuyện, xuất cuộc trò chuyện NPC, v.v.
Tạo một mới ModuleScript trong ReplicatedStorage .
Đổi tên kịch bản mới AnimateUI .
Sao chép mã sau vào kịch bản:
ModuleScript - Hoạt hình UIlocal LocalizationService = game:GetService("LocalizationService")local Players = game:GetService("Players")local SOURCE_LOCALE = "en"local translator = nillocal AnimateUI = {}function AnimateUI.loadTranslator()pcall(function()translator = LocalizationService:GetTranslatorForPlayerAsync(Players.LocalPlayer)end)if not translator thenpcall(function()translator = LocalizationService:GetTranslatorForLocaleAsync(SOURCE_LOCALE)end)endendfunction AnimateUI.typeWrite(guiObject, text, delayBetweenChars)guiObject.Visible = trueguiObject.AutoLocalize = falselocal displayText = text-- Dịch văn bản nếu có thểif translator thendisplayText = translator:Translate(guiObject, text)end-- Thay thế các thẻ phá dòng để vòng lặp grapheme sẽ không bỏ lỡ những nhân vật đódisplayText = displayText:gsub("<br%s*/>", "\n")-- Loại bỏ các thẻ RichText vì hoạt hình char-by-char sẽ phá vỡ các thẻdisplayText = displayText:gsub("<[^<>]->", "")-- Đặt văn bản được dịch/đã chỉnh sửa trên chaguiObject.Text = displayTextlocal index = 0for first, last in utf8.graphemes(displayText) doindex += 1guiObject.MaxVisibleGraphemes = indextask.wait(delayBetweenChars)endendreturn AnimateUITạo một TextLabel ở một vị trí thích hợp, chẳng hạn như trong một ScreenGui có cha mẹ là StarterGui .
Thêm một cái mới LocalScript như một con trực tiếp của nhãn và dán vào mã sau.Lưu ý rằng mỗi tin nhắn được xuất bằng cách gọi AnimateUI.typeWrite() với các tham số cho đối tượng cha, chuỗi để xuất và thời gian trễ giữa các ký tự.
Tập lệnh địa phươnglocal ReplicatedStorage = game:GetService("ReplicatedStorage")local AnimateUI = require(ReplicatedStorage:WaitForChild("AnimateUI"))local label = script.Parent-- Tải máy dịch nếu trò chơi được dịch lịch địa phương--AnimateUI.loadTranslator()local message1 = [[Beyond this door is the<br /><font size="46" color="rgb(255,50,25)">Great Zorgoth...</font> <font size="40">🗡</font>]]AnimateUI.typeWrite(label, message1, 0.05)task.wait(1)local message2 = [[...who rules this dungeon <font color="rgb(255,200,50)">unchallenged!</font> <font size="30">😈</font>]]AnimateUI.typeWrite(label, message2, 0.05)