-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAssetWriter.cs
More file actions
97 lines (73 loc) · 2.45 KB
/
AssetWriter.cs
File metadata and controls
97 lines (73 loc) · 2.45 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AssetWriter : MonoBehaviour {
#if UNITY_EDITOR
public static void DeleteAsset(string Name)
{
var Path = "Assets/" + Name + ".asset";
UnityEditor.AssetDatabase.DeleteAsset (Path);
}
#endif
#if UNITY_EDITOR
public static T WriteAsset<T>(string Name,T Asset) where T : Object
{
if (Asset == null)
return null;
var Path = "Assets/" + Name + ".asset";
var ExistingAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(Path);
if ( ExistingAsset == null )
{
UnityEditor.AssetDatabase.CreateAsset( Asset, Path );
}
else
{
UnityEditor.EditorUtility.CopySerialized(Asset, ExistingAsset);
}
return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(Path);
}
#endif
#if UNITY_EDITOR
public static T SaveAsset<T>(T Asset) where T : Object
{
if (Asset == null)
return null;
var Path = UnityEditor.AssetDatabase.GetAssetPath (Asset);
if (string.IsNullOrEmpty (Path)) {
Path = UnityEditor.EditorUtility.SaveFilePanelInProject ("Save new asset...", Asset.name, "asset", "Save asset");
if (string.IsNullOrEmpty (Path))
throw new System.Exception ("Save aborted");
// gr: returns null
//Path = UnityEditor.FileUtil.GetProjectRelativePath (Path);
}
var ExistingAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(Path);
if ( ExistingAsset == null )
{
UnityEditor.AssetDatabase.CreateAsset( Asset, Path );
UnityEditor.AssetDatabase.SaveAssets();
}
else
{
UnityEditor.EditorUtility.CopySerialized(Asset, ExistingAsset);
}
return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(Path);
}
#endif
#if UNITY_EDITOR
public static T SaveAsNewAsset<T>(T ExistingAsset) where T : Object, new()
{
if (ExistingAsset == null)
return null;
// duplicate this way; https://github.com/pharan/Unity-MeshSaver/blob/master/MeshSaver/Editor/MeshSaverEditor.cs
// copyserialised was creating a garbage mesh (same number of points, but messed up)
var NewAsset = Object.Instantiate(ExistingAsset) as Mesh;
NewAsset.name = ExistingAsset.name + "_copy";
var Path = UnityEditor.EditorUtility.SaveFilePanelInProject("Save new asset...", NewAsset.name, "asset", "Save asset");
if (string.IsNullOrEmpty(Path))
throw new System.Exception("Save aborted");
UnityEditor.AssetDatabase.CreateAsset(NewAsset, Path);
UnityEditor.AssetDatabase.SaveAssets();
return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(Path);
}
#endif
}