-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_unity.R
88 lines (78 loc) · 2.5 KB
/
find_unity.R
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#' Find the Unity executable on a machine.
#'
#' If the path to Unity is not provided to a function, this function is invoked
#' to attempt to find it. To do so, it goes through the following steps:
#'
#' 1. Attempt to load the "unifir_unity_path" environment variable.
#' 2. Attempt to load the "unifir_unity_path" option.
#'
#' @param unity Character: If provided, this function will quote the provided
#' string (if necessary) and return it.
#' @param check_path Logical: If `TRUE`, this function will check if the Unity
#' executable provided as an argument, environment variable, or option exists.
#' If it does not, this function will then attempt to find one, and will error
#' if not found. If `FALSE`, this function will never error.
#'
#' @examples
#' if (interactive()) {
#' try(find_unity())
#' }
#' @family utilities
#'
#' @export
find_unity <- function(unity = NULL, check_path = TRUE) {
if (is.null(unity)) unity <- Sys.getenv("unifir_unity_path")
if (unity == "") unity <- options("unifir_unity_path")[[1]]
# Check OS standard locations...
if (is.null(unity) || (check_path && !file.exists(unity))) {
sysname <- tolower(Sys.info()[["sysname"]])
unity <- switch(sysname,
"windows" = windows_locations(),
"linux" = linux_locations(),
"darwin" = mac_locations(),
NULL
)
}
if (is.null(unity) || (check_path && !file.exists(unity))) {
stop(
"Couldn't find Unity executable at provided path. \n",
"Please make sure the path provided to 'unity' is correct."
)
}
if (!grepl("^\"", unity)) unity <- paste0('"', unity)
if (!grepl("\"$", unity)) unity <- paste0(unity, '"')
if (Sys.getenv("unifir_unity_path") == "") {
Sys.setenv("unifir_unity_path" = unity)
}
unity
}
windows_locations <- function() {
if (dir.exists("C:\\Program Files\\Unity\\Hub\\Editor")) {
unity <- utils::tail(
list.files("C:\\Program Files\\Unity\\Hub\\Editor",
full.names = TRUE
),
1
)
paste0(unity, "\\Editor\\Unity.exe")
}
}
linux_locations <- function() {
if (dir.exists("~/Unity/Hub/Editor")) {
unity <- utils::tail(
list.files("~/Unity/Hub/Editor", full.names = TRUE),
1
)
paste0(unity, "/Editor/Unity")
}
}
mac_locations <- function() {
if (dir.exists("/Applications/Unity/Hub/Editor")) {
# This works on at least one Mac
unity <- utils::tail(
list.files("/Applications/Unity/Hub/Editor", full.names = TRUE),
1
)
paste0(unity, "Unity.app/Contents/MacOS/Unity")
}
}