Tanoda
DefinitionBase.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.Collections;
10using System.Collections.Generic;
11using UnityEngine;
12using UnityEngine.Serialization;
13#if UNITY_EDITOR
14using UnityEditor;
15#endif
16
17namespace Leap.Unity.Packaging {
18
19 public class DefinitionBase : ScriptableObject {
20 private const string PACKAGE_EXPORT_FOLDER_KEY = "LeapPackageDefExportFolder";
21
22 [SerializeField]
23 [FormerlySerializedAs("_packageName")]
24 protected string _definitionName;
25
26 [FormerlySerializedAs("_generateBuildDropdown")]
27 [SerializeField]
28 protected bool _showInBuildMenu = false;
29
30 public string DefinitionName {
31 get {
32 return _definitionName;
33 }
34 }
35
36 public bool ShowInBuildMenu {
37 get {
38 return _showInBuildMenu;
39 }
40 }
41
42#if UNITY_EDITOR
43 [ContextMenu("Reset Export Folder")]
44 public void ResetExportFolder() {
45 EditorPrefs.DeleteKey(getExportFolderKey());
46 }
47
52 public bool PrompUserToSetExportPath() {
53 string promptFolder;
54 if (!TryGetPackageExportFolder(out promptFolder, promptIfNotDefined: false)) {
55 promptFolder = Application.dataPath;
56 }
57
58 string chosenFolder = EditorUtility.OpenFolderPanel("Select export folder for " + DefinitionName, promptFolder, "Packages");
59 if (string.IsNullOrEmpty(chosenFolder)) {
60 return false;
61 }
62
63 EditorPrefs.SetString(getExportFolderKey(), chosenFolder);
64 return true;
65 }
66
70 public bool HasExportFolderBeenDefined() {
71 string key = getExportFolderKey();
72 return EditorPrefs.HasKey(key);
73 }
74
80 public bool TryGetPackageExportFolder(out string folder, bool promptIfNotDefined) {
81 string key = getExportFolderKey();
82 if (!EditorPrefs.HasKey(key)) {
83 if (!promptIfNotDefined || !PrompUserToSetExportPath()) {
84 folder = null;
85 return false;
86 }
87 }
88
89 folder = EditorPrefs.GetString(key);
90 return true;
91 }
92
93 private string getExportFolderKey() {
94 //Tie the key to the guid of the asset, as it will never change for the duration of the asset's life and will be unique for
95 //a given computer.
96 return PACKAGE_EXPORT_FOLDER_KEY + "_" + AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(this));
97 }
98#endif
99 }
100}