Fix prefab overwrite.

This commit is contained in:
max 2021-01-18 22:59:57 +01:00
parent f9a50c8c3a
commit 94f97c218a
4 changed files with 66 additions and 44 deletions

View File

@ -43,5 +43,10 @@ namespace TAO.VertexAnimation.Editor
} }
} }
} }
public static bool HasAsset(string path, System.Type type)
{
return (AssetDatabase.LoadAssetAtPath(path, type) ? true : false);
}
} }
} }

View File

@ -7,22 +7,65 @@ namespace TAO.VertexAnimation.Editor
{ {
public static GameObject Create(string path, string name, Mesh[] meshes, Material material, float[] lodTransitions) public static GameObject Create(string path, string name, Mesh[] meshes, Material material, float[] lodTransitions)
{ {
// Create parent. GameObject parent = null;
GameObject parent = new GameObject(name, typeof(LODGroup), typeof(VA_AnimatorComponentAuthoring), typeof(Unity.Entities.ConvertToEntity)); if (AssetDatabaseUtils.HasAsset(path, typeof(GameObject)))
{
// Load existing parent.
parent = PrefabUtility.LoadPrefabContents(path);
// Check setup.
if (!parent.TryGetComponent(out LODGroup _))
{
parent.AddComponent<LODGroup>();
}
if (!parent.TryGetComponent(out VA_AnimatorComponentAuthoring _))
{
parent.AddComponent<VA_AnimatorComponentAuthoring>();
}
if (!parent.TryGetComponent(out Unity.Entities.ConvertToEntity _))
{
parent.AddComponent<Unity.Entities.ConvertToEntity>();
}
}
else
{
// Create parent.
parent = new GameObject(name, typeof(LODGroup), typeof(VA_AnimatorComponentAuthoring), typeof(Unity.Entities.ConvertToEntity));
}
// Create all LODs. // Create all LODs.
LOD[] lods = new LOD[meshes.Length]; LOD[] lods = new LOD[meshes.Length];
for (int i = 0; i < meshes.Length; i++) for (int i = 0; i < meshes.Length; i++)
{ {
GameObject lod = new GameObject(string.Format("{0}_LOD{1}", name, i), typeof(MeshFilter), typeof(MeshRenderer)); string childName = string.Format("{0}_LOD{1}", name, i);
var mf = lod.GetComponent<MeshFilter>();
mf.sharedMesh = meshes[i];
var mr = lod.GetComponent<MeshRenderer>();
mr.sharedMaterial = material;
lod.transform.SetParent(parent.transform); GameObject child;
{
Transform t = parent.transform.Find(childName);
if (t)
{
child = t.gameObject;
}
else
{
child = new GameObject(childName, typeof(MeshFilter), typeof(MeshRenderer));
}
}
if (child.TryGetComponent(out MeshFilter mf))
{
mf.sharedMesh = meshes[i];
}
if (child.TryGetComponent(out MeshRenderer mr))
{
mr.sharedMaterial = material;
}
child.transform.SetParent(parent.transform);
lods[i] = new LOD(lodTransitions[i], new Renderer[1] { mr }); lods[i] = new LOD(lodTransitions[i], new Renderer[1] { mr });
} }

View File

@ -39,31 +39,19 @@ namespace TAO.VertexAnimation.Editor
private void BakeGUI() private void BakeGUI()
{ {
EditorGUILayout.PropertyField(serializedObject.FindProperty("saveBakedDataToAsset")); EditorGUILayout.PropertyField(serializedObject.FindProperty("lodSettings").FindPropertyRelative("lodSettings"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("generateAnimationBook"));
int il = EditorGUI.indentLevel; using (new EditorGUILayout.HorizontalScope())
if (modelBaker.saveBakedDataToAsset)
{ {
EditorGUI.indentLevel++; EditorGUILayout.PropertyField(serializedObject.FindProperty("generatePrefab"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("generateAnimationBook")); EditorGUILayout.PropertyField(serializedObject.FindProperty("materialShader"), new GUIContent(""));
EditorGUILayout.PropertyField(serializedObject.FindProperty("lodSettings"));
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("generatePrefab"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("materialShader"), new GUIContent(""));
}
} }
EditorGUI.indentLevel = il;
if (GUILayout.Button("Bake", GUILayout.Height(32))) if (GUILayout.Button("Bake", GUILayout.Height(32)))
{ {
modelBaker.Bake(); modelBaker.Bake();
modelBaker.SaveAssets();
if (modelBaker.saveBakedDataToAsset)
{
modelBaker.SaveAssets();
}
} }
if (GUILayout.Button("Delete", EditorStyles.miniButtonRight)) if (GUILayout.Button("Delete", EditorStyles.miniButtonRight))

View File

@ -16,7 +16,6 @@ namespace TAO.VertexAnimation.Editor
public int textureWidth = 512; public int textureWidth = 512;
public LODSettings lodSettings = new LODSettings(); public LODSettings lodSettings = new LODSettings();
public bool saveBakedDataToAsset = true;
public bool generateAnimationBook = true; public bool generateAnimationBook = true;
public bool generatePrefab = true; public bool generatePrefab = true;
public Shader materialShader = null; public Shader materialShader = null;
@ -34,7 +33,6 @@ namespace TAO.VertexAnimation.Editor
[System.Serializable] [System.Serializable]
public class LODSettings public class LODSettings
{ {
public bool generate = true;
public LODSetting[] lodSettings = new LODSetting[3] { new LODSetting(1, .4f), new LODSetting(.6f, .15f), new LODSetting(.3f, .01f) }; public LODSetting[] lodSettings = new LODSetting[3] { new LODSetting(1, .4f), new LODSetting(.6f, .15f), new LODSetting(.3f, .01f) };
public float[] GetQualitySettings() public float[] GetQualitySettings()
@ -91,15 +89,7 @@ namespace TAO.VertexAnimation.Editor
bakedData = target.Bake(animationClips, fps, textureWidth); bakedData = target.Bake(animationClips, fps, textureWidth);
positionMap = VA_Texture2DArrayUtils.CreateTextureArray(bakedData.positionMaps.ToArray(), false, true, TextureWrapMode.Repeat, FilterMode.Point, 1, string.Format("{0}_PositionMap", name), true); positionMap = VA_Texture2DArrayUtils.CreateTextureArray(bakedData.positionMaps.ToArray(), false, true, TextureWrapMode.Repeat, FilterMode.Point, 1, string.Format("{0}_PositionMap", name), true);
meshes = bakedData.mesh.GenerateLOD(lodSettings.LODCount(), lodSettings.GetQualitySettings());
if (lodSettings.generate)
{
meshes = bakedData.mesh.GenerateLOD(lodSettings.LODCount(), lodSettings.GetQualitySettings());
}
else
{
meshes = new Mesh[1] { bakedData.mesh };
}
DestroyImmediate(target); DestroyImmediate(target);
} }
@ -114,10 +104,6 @@ namespace TAO.VertexAnimation.Editor
} }
AssetDatabase.AddObjectToAsset(positionMap, this); AssetDatabase.AddObjectToAsset(positionMap, this);
//foreach (var pm in bakedData.positionMaps)
//{
// AssetDatabase.AddObjectToAsset(pm, this);
//}
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();
@ -161,7 +147,7 @@ namespace TAO.VertexAnimation.Editor
AssetDatabase.Refresh(); AssetDatabase.Refresh();
} }
public void GeneratePrefab() private void GeneratePrefab()
{ {
string path = AssetDatabase.GetAssetPath(this); string path = AssetDatabase.GetAssetPath(this);
int start = path.LastIndexOf('/'); int start = path.LastIndexOf('/');
@ -186,7 +172,7 @@ namespace TAO.VertexAnimation.Editor
prefab = AnimationPrefab.Create(path, name, meshes, material, lodSettings.GetTransitionSettings()); prefab = AnimationPrefab.Create(path, name, meshes, material, lodSettings.GetTransitionSettings());
} }
public void GenerateBook() private void GenerateBook()
{ {
if (!book) if (!book)
{ {