Skip to content

Commit 27b3f6d

Browse files
Added Editor Prefs Utilities and refactored.
1 parent 1ae13ec commit 27b3f6d

File tree

6 files changed

+98
-69
lines changed

6 files changed

+98
-69
lines changed
Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
//
2-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
32
// Licensed under the MIT License. See LICENSE in the project root for license information.
4-
//
53

64
using System.IO;
7-
using UnityEditor;
85
using UnityEngine;
96

107
namespace HoloToolkit.Unity
@@ -24,8 +21,8 @@ public static class BuildDeployPrefs
2421

2522
public static string BuildDirectory
2623
{
27-
get { return GetEditorPref(EditorPrefs_BuildDir, "UWP"); }
28-
set { SetEditorPref(EditorPrefs_BuildDir, value); }
24+
get { return EditorPrefsUtility.GetEditorPref(EditorPrefs_BuildDir, "UWP"); }
25+
set { EditorPrefsUtility.SetEditorPref(EditorPrefs_BuildDir, value); }
2926
}
3027

3128
public static string AbsoluteBuildDirectory
@@ -35,82 +32,50 @@ public static string AbsoluteBuildDirectory
3532

3633
public static string MsBuildVersion
3734
{
38-
get { return GetEditorPref(EditorPrefs_MSBuildVer, BuildDeployTools.DefaultMSBuildVersion); }
39-
set { SetEditorPref(EditorPrefs_MSBuildVer, value); }
35+
get { return EditorPrefsUtility.GetEditorPref(EditorPrefs_MSBuildVer, BuildDeployTools.DefaultMSBuildVersion); }
36+
set { EditorPrefsUtility.SetEditorPref(EditorPrefs_MSBuildVer, value); }
4037
}
4138

4239
public static string BuildConfig
4340
{
44-
get { return GetEditorPref(EditorPrefs_BuildConfig, "Debug"); }
45-
set { SetEditorPref(EditorPrefs_BuildConfig, value); }
41+
get { return EditorPrefsUtility.GetEditorPref(EditorPrefs_BuildConfig, "Debug"); }
42+
set { EditorPrefsUtility.SetEditorPref(EditorPrefs_BuildConfig, value); }
4643
}
4744

4845
public static bool ForceRebuild
4946
{
50-
get { return GetEditorPref(EditorPrefs_ForceRebuild, false); }
51-
set { SetEditorPref(EditorPrefs_ForceRebuild, value); }
47+
get { return EditorPrefsUtility.GetEditorPref(EditorPrefs_ForceRebuild, false); }
48+
set { EditorPrefsUtility.SetEditorPref(EditorPrefs_ForceRebuild, value); }
5249
}
5350

5451
public static bool IncrementBuildVersion
5552
{
56-
get { return GetEditorPref(EditorPrefs_IncrementBuildVersion, true); }
57-
set { SetEditorPref(EditorPrefs_IncrementBuildVersion, value); }
53+
get { return EditorPrefsUtility.GetEditorPref(EditorPrefs_IncrementBuildVersion, true); }
54+
set { EditorPrefsUtility.SetEditorPref(EditorPrefs_IncrementBuildVersion, value); }
5855
}
5956

6057
public static string TargetIPs
6158
{
62-
get { return GetEditorPref(EditorPrefs_TargetIPs, "127.0.0.1"); }
63-
set { SetEditorPref(EditorPrefs_TargetIPs, value); }
59+
get { return EditorPrefsUtility.GetEditorPref(EditorPrefs_TargetIPs, "127.0.0.1"); }
60+
set { EditorPrefsUtility.SetEditorPref(EditorPrefs_TargetIPs, value); }
6461
}
6562

6663
public static string DeviceUser
6764
{
68-
get { return GetEditorPref(EditorPrefs_DeviceUser, ""); }
69-
set { SetEditorPref(EditorPrefs_DeviceUser, value); }
65+
get { return EditorPrefsUtility.GetEditorPref(EditorPrefs_DeviceUser, ""); }
66+
set { EditorPrefsUtility.SetEditorPref(EditorPrefs_DeviceUser, value); }
7067
}
7168

7269
public static string DevicePassword
7370
{
74-
get { return GetEditorPref(EditorPrefs_DevicePwd, ""); }
75-
set { SetEditorPref(EditorPrefs_DevicePwd, value); }
71+
get { return EditorPrefsUtility.GetEditorPref(EditorPrefs_DevicePwd, ""); }
72+
set { EditorPrefsUtility.SetEditorPref(EditorPrefs_DevicePwd, value); }
7673
}
7774

7875
public static bool FullReinstall
7976
{
80-
get { return GetEditorPref(EditorPrefs_FullReinstall, true); }
81-
set { SetEditorPref(EditorPrefs_FullReinstall, value); }
82-
}
83-
84-
private static void SetEditorPref(string key, string value)
85-
{
86-
EditorPrefs.SetString(Application.productName + key, value);
87-
}
88-
89-
private static void SetEditorPref(string key, bool value)
90-
{
91-
EditorPrefs.SetBool(Application.productName + key, value);
92-
}
93-
94-
private static string GetEditorPref(string key, string defaultValue)
95-
{
96-
if (EditorPrefs.HasKey(Application.productName + key))
97-
{
98-
return EditorPrefs.GetString(Application.productName + key);
99-
}
100-
101-
EditorPrefs.SetString(Application.productName + key, defaultValue);
102-
return defaultValue;
103-
}
104-
105-
private static bool GetEditorPref(string key, bool defaultValue)
106-
{
107-
if (EditorPrefs.HasKey(Application.productName + key))
108-
{
109-
return EditorPrefs.GetBool(Application.productName + key);
110-
}
111-
112-
EditorPrefs.SetBool(Application.productName + key, defaultValue);
113-
return defaultValue;
77+
get { return EditorPrefsUtility.GetEditorPref(EditorPrefs_FullReinstall, true); }
78+
set { EditorPrefsUtility.SetEditorPref(EditorPrefs_FullReinstall, value); }
11479
}
11580
}
11681
}

Assets/HoloToolkit/SpatialMapping/Scripts/RemoteMapping/Editor/RoomMeshExporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static string ExportDirectory
1818
{
1919
get
2020
{
21-
return EditorPrefs.GetString(ExportDirectoryKey, ExportDirectoryDefault);
21+
return EditorPrefsUtility.GetEditorPref(ExportDirectoryKey, ExportDirectoryDefault);
2222
}
2323
set
2424
{
@@ -27,7 +27,7 @@ public static string ExportDirectory
2727
value = ExportDirectoryDefault;
2828
}
2929

30-
EditorPrefs.SetString(Application.productName + ExportDirectoryKey, value);
30+
EditorPrefsUtility.SetEditorPref(ExportDirectoryKey, value);
3131
}
3232
}
3333

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace HoloToolkit.Unity
8+
{
9+
public static class EditorPrefsUtility
10+
{
11+
public static void SetEditorPref(string key, string value)
12+
{
13+
EditorPrefs.SetString(Application.productName + key, value);
14+
}
15+
16+
public static void SetEditorPref(string key, bool value)
17+
{
18+
EditorPrefs.SetBool(Application.productName + key, value);
19+
}
20+
21+
public static string GetEditorPref(string key, string defaultValue)
22+
{
23+
if (EditorPrefs.HasKey(Application.productName + key))
24+
{
25+
return EditorPrefs.GetString(Application.productName + key);
26+
}
27+
28+
EditorPrefs.SetString(Application.productName + key, defaultValue);
29+
return defaultValue;
30+
}
31+
32+
public static bool GetEditorPref(string key, bool defaultValue)
33+
{
34+
if (EditorPrefs.HasKey(Application.productName + key))
35+
{
36+
return EditorPrefs.GetBool(Application.productName + key);
37+
}
38+
39+
EditorPrefs.SetBool(Application.productName + key, defaultValue);
40+
return defaultValue;
41+
}
42+
}
43+
}

Assets/HoloToolkit/Utilities/Scripts/Editor/EditorPrefs.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/HoloToolkit/Utilities/Scripts/Editor/EnforceEditorSettings.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,25 @@ static EnforceEditorSettings()
7070
/// </remarks>
7171
private static bool IsNewEditorSession()
7272
{
73-
// Determine the last known launch date of the editor by loading it from the PlayerPrefs cache.
74-
DateTime lastLaunchDate;
75-
DateTime.TryParse(EditorPrefs.GetString(Application.productName + AssemblyReloadTimestampKey), out lastLaunchDate);
76-
7773
// Determine the launch date for this editor session using the current time, and the time since startup.
7874
DateTime thisLaunchDate = DateTime.UtcNow.AddSeconds(-EditorApplication.timeSinceStartup);
79-
EditorPrefs.SetString(Application.productName + AssemblyReloadTimestampKey, thisLaunchDate.ToString(CultureInfo.InvariantCulture));
75+
76+
// Determine the last known launch date of the editor by loading it from the PlayerPrefs cache.
77+
// If no key exists set the time to this session.
78+
string dateString = EditorPrefsUtility.GetEditorPref(AssemblyReloadTimestampKey, thisLaunchDate.ToString(CultureInfo.InvariantCulture));
79+
80+
DateTime lastLaunchDate;
81+
DateTime.TryParse(dateString, out lastLaunchDate);
8082

8183
// If the current session was launched later than the last known session start date, then this must be
8284
// a new session, and we can display the first-time prompt.
83-
return (thisLaunchDate - lastLaunchDate).Seconds > 0;
85+
if ((thisLaunchDate - lastLaunchDate).Seconds > 0)
86+
{
87+
EditorPrefsUtility.SetEditorPref(AssemblyReloadTimestampKey, dateString);
88+
return true;
89+
}
90+
91+
return false;
8492
}
8593
}
8694
}

Assets/HoloToolkit/Utilities/Scripts/Editor/SetIconsWindow.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.IO;
66
using System.Linq;
7-
using System.Reflection;
87
using UnityEditor;
98
using UnityEngine;
109

@@ -42,18 +41,20 @@ private static void OpenWindow()
4241
private void OnEnable()
4342
{
4443
// Load settings
45-
_originalAppIconPath = EditorPrefs.GetString(Application.productName + EditorPrefsKey_AppIcon);
46-
_originalSplashImagePath = EditorPrefs.GetString(Application.productName + EditorPrefsKey_SplashImage);
47-
_outputDirectoryName = EditorPrefs.GetString(Application.productName + EditorPrefsKey_DirectoryName);
44+
_originalAppIconPath = EditorPrefsUtility.GetEditorPref(EditorPrefsKey_AppIcon, _originalAppIconPath);
45+
_originalSplashImagePath = EditorPrefsUtility.GetEditorPref(EditorPrefsKey_SplashImage, _originalSplashImagePath);
46+
_outputDirectoryName = EditorPrefsUtility.GetEditorPref(EditorPrefsKey_DirectoryName, _outputDirectoryName);
4847

4948
if (!string.IsNullOrEmpty(_originalAppIconPath))
5049
{
5150
_originalAppIcon = AssetDatabase.LoadAssetAtPath<Texture2D>(_originalAppIconPath);
5251
}
52+
5353
if (!string.IsNullOrEmpty(_originalSplashImagePath))
5454
{
5555
_originalSplashImage = AssetDatabase.LoadAssetAtPath<Texture2D>(_originalSplashImagePath);
5656
}
57+
5758
if (string.IsNullOrEmpty(_outputDirectoryName))
5859
{
5960
_outputDirectoryName = Application.dataPath + "/" + InitialOutputDirectoryName;
@@ -112,9 +113,9 @@ private void OnGUI()
112113

113114
private static void SaveSettings()
114115
{
115-
EditorPrefs.SetString(Application.productName + EditorPrefsKey_AppIcon, _originalAppIconPath);
116-
EditorPrefs.SetString(Application.productName + EditorPrefsKey_SplashImage, _originalSplashImagePath);
117-
EditorPrefs.SetString(Application.productName + EditorPrefsKey_DirectoryName, _outputDirectoryName);
116+
EditorPrefsUtility.SetEditorPref(EditorPrefsKey_AppIcon, _originalAppIconPath);
117+
EditorPrefsUtility.SetEditorPref(EditorPrefsKey_SplashImage, _originalSplashImagePath);
118+
EditorPrefsUtility.SetEditorPref(EditorPrefsKey_DirectoryName, _outputDirectoryName);
118119
}
119120

120121
private static Texture2D CreateImageInput(string imageTitle, int width, int height, Texture2D texture, ref string path)

0 commit comments

Comments
 (0)