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

@ -6,23 +6,66 @@ namespace TAO.VertexAnimation.Editor
public static class AnimationPrefab
{
public static GameObject Create(string path, string name, Mesh[] meshes, Material material, float[] lodTransitions)
{
GameObject parent = null;
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.
GameObject parent = new GameObject(name, typeof(LODGroup), typeof(VA_AnimatorComponentAuthoring), typeof(Unity.Entities.ConvertToEntity));
parent = new GameObject(name, typeof(LODGroup), typeof(VA_AnimatorComponentAuthoring), typeof(Unity.Entities.ConvertToEntity));
}
// Create all LODs.
LOD[] lods = new LOD[meshes.Length];
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>();
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];
var mr = lod.GetComponent<MeshRenderer>();
mr.sharedMaterial = material;
}
lod.transform.SetParent(parent.transform);
if (child.TryGetComponent(out MeshRenderer mr))
{
mr.sharedMaterial = material;
}
child.transform.SetParent(parent.transform);
lods[i] = new LOD(lodTransitions[i], new Renderer[1] { mr });
}

View File

@ -39,32 +39,20 @@ namespace TAO.VertexAnimation.Editor
private void BakeGUI()
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("saveBakedDataToAsset"));
int il = EditorGUI.indentLevel;
if (modelBaker.saveBakedDataToAsset)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(serializedObject.FindProperty("lodSettings").FindPropertyRelative("lodSettings"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("generateAnimationBook"));
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)))
{
modelBaker.Bake();
if (modelBaker.saveBakedDataToAsset)
{
modelBaker.SaveAssets();
}
}
if (GUILayout.Button("Delete", EditorStyles.miniButtonRight))
{

View File

@ -16,7 +16,6 @@ namespace TAO.VertexAnimation.Editor
public int textureWidth = 512;
public LODSettings lodSettings = new LODSettings();
public bool saveBakedDataToAsset = true;
public bool generateAnimationBook = true;
public bool generatePrefab = true;
public Shader materialShader = null;
@ -34,7 +33,6 @@ namespace TAO.VertexAnimation.Editor
[System.Serializable]
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 float[] GetQualitySettings()
@ -91,15 +89,7 @@ namespace TAO.VertexAnimation.Editor
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);
if (lodSettings.generate)
{
meshes = bakedData.mesh.GenerateLOD(lodSettings.LODCount(), lodSettings.GetQualitySettings());
}
else
{
meshes = new Mesh[1] { bakedData.mesh };
}
DestroyImmediate(target);
}
@ -114,10 +104,6 @@ namespace TAO.VertexAnimation.Editor
}
AssetDatabase.AddObjectToAsset(positionMap, this);
//foreach (var pm in bakedData.positionMaps)
//{
// AssetDatabase.AddObjectToAsset(pm, this);
//}
AssetDatabase.SaveAssets();
@ -161,7 +147,7 @@ namespace TAO.VertexAnimation.Editor
AssetDatabase.Refresh();
}
public void GeneratePrefab()
private void GeneratePrefab()
{
string path = AssetDatabase.GetAssetPath(this);
int start = path.LastIndexOf('/');
@ -186,7 +172,7 @@ namespace TAO.VertexAnimation.Editor
prefab = AnimationPrefab.Create(path, name, meshes, material, lodSettings.GetTransitionSettings());
}
public void GenerateBook()
private void GenerateBook()
{
if (!book)
{