Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
KeyWongQD
Unity3DTraining
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Unity3DTraining
/
lua
/
LuaTraining
/
function.lua
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
65 lines (55 loc) · 1.63 KB
master
Breadcrumbs
Unity3DTraining
/
lua
/
LuaTraining
/
function.lua
Copy path
Top
File metadata and controls
Code
Blame
65 lines (55 loc) · 1.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--以下实例定义了函数 max(),参数为 num1, num2,用于比较两值的大小,并返回最大值
function max(num1, num2)
if (num1 > num2) then
result = num1;
else
result = num2;
end
return result;
end
-- 调用函数
print("Max is: ",max(10,4))
print("Min is: ",max(5,6))
--Lua 中我们可以将函数作为参数传递给函数
myprint = function(param)
print("This is print fucntion - ##",param,"##")
end
function add(num1,num2,functionPrint)
result = num1 + num2
-- 调用传递的函数参数
functionPrint(result)
end
myprint(10)
-- myprint 函数作为参数传递
add(2,5,myprint)
--Lua函数可以返回多个结果值,比如string.find
--其返回匹配串"开始和结束的下标"(如果不存在匹配串返回nil)
s, e = string.find("www.runoob.com", "runoob")
print(s, e) -->5 10
--Lua函数中,在return后列出要返回的值得列表即可返回多值
function maximum (a)
local mi = 1 -- 最大值索引
local m = a[mi] -- 最大值
for i,val in ipairs(a) do
if val > m then
mi = i
m = val
end
end
return m, mi
end
print(maximum({8,10,23,12,5}))
--[[
Lua函数可以接受可变数目的参数,和C语言类似在函数参数列表中使用三点(...) 表示函数有可变的参数
Lua将函数的参数放在一个叫arg的表中,#arg 表示传入参数的个数
]]--
function average(...)
result = 0
local arg={...}
for i,v in ipairs(arg) do
result = result + v
end
print("Total args count : " .. #arg)
return result/#arg
end
print("Average is: ",average(10,5,3,4,5,6))
You can’t perform that action at this time.