FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
UnityLibrary/Assets/Scripts/Utilities/SaveUtility.cs at master · UnityCommunity/UnityLibrary · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
UnityCommunity
/
UnityLibrary
Public
Notifications
You must be signed in to change notification settings
Fork
469
Star
4.4k
Code
Issues
13
Pull requests
0
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
UnityLibrary
/
Assets
/
Scripts
/
Utilities
/
SaveUtility.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
70 lines (55 loc) · 2.12 KB
Breadcrumbs
UnityLibrary
/
Assets
/
Scripts
/
Utilities
/
SaveUtility.cs
Copy path
File metadata and controls
70 lines (55 loc) · 2.12 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
66
67
68
69
70
using
System
;
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
UnityEngine
;
namespace
Utilities
{
public
static
class
SaveUtility
{
/* --- Usage ----
-- Lets say we have a serialized class called Save --
[System.Serializable]
public class Save
{
public int testInt;
public List<string> TestStrings = new List<string>();
}
-- For saving this class we would do the following -->> --
Save save = new Save();
save.testInt = 5;
save.TestStrings.Add("Hello");
SaveUtility.Save(save, "testPath");
-- For loading this class we would do the following --> --
Save save = SaveUtility.Load<Save, string>("testPath");
Important : Since save operation uses PlayerPrefs and does not contain any encrypting. Dont save&load any confidential data using this class.
--- End of Usage ---- */
/// <summary>
/// Saves any serialized class on the specified savepath.
/// </summary>
public
static
void
Save
<
T
>
(
this
T
save
,
string
savePath
)
where
T
:
class
{
var
data
=
JsonUtility
.
ToJson
(
save
)
;
PlayerPrefs
.
SetString
(
savePath
,
data
)
;
}
/// <summary>
/// Loads and returns the saved class on the specified savepath.
/// If no save is found, it will return a new instance of the class.
/// </summary>
/// <returns>Loaded serialized save class</returns>
public
static
T
Load
<
T
,
TSt
>
(
TSt
path
)
where
T
:
class
,
new
(
)
where
TSt
:
IComparable
,
ICloneable
,
IConvertible
,
IComparable
<
string
>
,
IEnumerable
<
char
>
,
IEnumerable
,
IEquatable
<
string
>
{
var
data
=
new
T
(
)
;
if
(
PlayerPrefs
.
HasKey
(
path
as
string
)
==
false
)
{
data
.
Save
(
path
as
string
)
;
}
{
data
=
JsonUtility
.
FromJson
<
T
>
(
PlayerPrefs
.
GetString
(
path
as
string
)
)
;
}
return
data
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL