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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
using System;
namespace Cinemachine.Editor
{
public class ScriptableObjectUtility : ScriptableObject
{
public static string CinemachineInstallPath
{
get { return Path.GetFullPath(CinemachineInstallAssetPath); }
}
public static string CinemachineInstallAssetPath
{
get
{
ScriptableObject dummy = ScriptableObject.CreateInstance<ScriptableObjectUtility>();
string path = AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(dummy));
DestroyImmediate(dummy);
path = path.Substring(0, path.LastIndexOf("/Base"));
return path;
}
}
public static bool CinemachineIsPackage
{
get { return CinemachineInstallAssetPath.StartsWith("Packages"); }
}
public static bool AddDefineForAllBuildTargets(string k_Define)
{
bool added = false;
var targets = Enum.GetValues(typeof(BuildTargetGroup))
.Cast<BuildTargetGroup>()
.Where(x => x != BuildTargetGroup.Unknown)
.Where(x => !BuildTargetIsObsolete(x));
foreach (var target in targets)
{
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(target).Trim();
var list = defines.Split(';', ' ')
.Where(x => !string.IsNullOrEmpty(x))
.ToList();
if (!list.Contains(k_Define))
{
list.Add(k_Define);
defines = list.Aggregate((a, b) => a + ";" + b);
PlayerSettings.SetScriptingDefineSymbolsForGroup(target, defines);
added = true;
}
}
return added;
}
static bool BuildTargetIsObsolete(BuildTargetGroup group)
{
var attrs = typeof(BuildTargetGroup)
.GetField(group.ToString())
.GetCustomAttributes(typeof(ObsoleteAttribute), false);
return attrs != null && attrs.Length > 0;
}
public static void Create<T>(bool prependFolderName = false, bool trimName = true) where T : ScriptableObject
{
string className = typeof(T).Name;
string assetName = className;
string folder = GetSelectedAssetFolder();
if (trimName)
{
string[] standardNames = new string[] { "Asset", "Attributes", "Container" };
foreach (string standardName in standardNames)
{
assetName = assetName.Replace(standardName, "");
}
}
if (prependFolderName)
{
string folderName = Path.GetFileName(folder);
assetName = (string.IsNullOrEmpty(assetName) ? folderName : string.Format("{0}_{1}", folderName, assetName));
}
Create(className, assetName, folder);
}
public static T CreateAt<T>(string assetPath) where T : ScriptableObject
{
T asset = ScriptableObject.CreateInstance<T>();
if (asset == null)
{
Debug.LogError("failed to create instance of " + typeof(T).Name);
return null;
}
AssetDatabase.CreateAsset(asset, assetPath);
return asset;
}
private static ScriptableObject Create(string className, string assetName, string folder)
{
ScriptableObject asset = ScriptableObject.CreateInstance(className);
if (asset == null)
{
Debug.LogError("failed to create instance of " + className);
return null;
}
asset.name = assetName ?? className;
string assetPath = GetUnusedAssetPath(folder, asset.name);
AssetDatabase.CreateAsset(asset, assetPath);
return asset;
}
private static string GetSelectedAssetFolder()
{
if ((Selection.activeObject != null) && AssetDatabase.Contains(Selection.activeObject))
{
string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
string assetPathAbsolute = string.Format("{0}/{1}", Path.GetDirectoryName(Application.dataPath), assetPath);
if (Directory.Exists(assetPathAbsolute))
{
return assetPath;
}
else
{
return Path.GetDirectoryName(assetPath);
}
}
return "Assets";
}
private static string GetUnusedAssetPath(string folder, string assetName)
{
for (int n = 0; n < 9999; n++)
{
string assetPath = string.Format("{0}/{1}{2}.asset", folder, assetName, (n == 0 ? "" : n.ToString()));
string existingGUID = AssetDatabase.AssetPathToGUID(assetPath);
if (string.IsNullOrEmpty(existingGUID))
{
return assetPath;
}
}
return null;
}
}
}
|