VertexAnimation/Editor/Scripts/ModelBaker/AnimationPrefab.cs

114 lines
2.9 KiB
C#
Raw Normal View History

2022-12-04 15:47:07 +01:00
using System.IO;
using UnityEngine;
using UnityEditor;
namespace TAO.VertexAnimation.Editor
{
public static class AnimationPrefab
{
public static GameObject Create(string path, string name, Mesh[] meshes, Material material, float[] lodTransitions)
{
2021-01-18 22:59:57 +01:00
GameObject parent = null;
if (AssetDatabaseUtils.HasAsset(path, typeof(GameObject)))
{
// Load existing parent.
parent = PrefabUtility.LoadPrefabContents(path);
// Check setup.
2022-12-04 15:47:07 +01:00
if (!parent.TryGetComponent(out LODGroup _))
{
parent.AddComponent<LODGroup>();
}
2022-12-04 04:18:35 +01:00
2022-12-04 15:47:07 +01:00
if (!parent.TryGetComponent(out VA_AnimationLibraryComponentAuthoring _))
{
parent.AddComponent<VA_AnimationLibraryComponentAuthoring>();
}
2022-12-04 04:18:35 +01:00
//if (!parent.TryGetComponent(out Unity.Entities.ConvertToEntity _))
//{
// parent.AddComponent<Unity.Entities.ConvertToEntity>();
//}
2021-01-18 22:59:57 +01:00
}
else
{
// Create parent.
2022-12-04 15:47:07 +01:00
parent = new GameObject(name, typeof(LODGroup), typeof(VA_AnimationLibraryComponentAuthoring));
2021-01-18 22:59:57 +01:00
}
// Create all LODs.
LOD[] lods = new LOD[meshes.Length];
2022-12-04 15:47:07 +01:00
//string meshPath = "Assets/Mesh" + parent.name;
//int index = 0;
//foreach ( Mesh mesh in meshes )
//{
// if ( !AssetDatabaseUtils.HasAsset( meshPath + index + ".asset", typeof( Mesh ) ) )
// {
// AssetDatabase.CreateAsset( mesh, meshPath + index + ".asset" );
// }
//
// index++;
//}
AssetDatabase.SaveAssets();
for (int i = 0; i < meshes.Length; i++)
{
2021-01-18 22:59:57 +01:00
string childName = string.Format("{0}_LOD{1}", name, i);
GameObject child;
{
Transform t = parent.transform.Find(childName);
if (t)
{
child = t.gameObject;
}
else
{
2022-12-04 15:47:07 +01:00
child = new GameObject(childName, typeof(MeshFilter), typeof(MeshRenderer), typeof(VA_AnimationDataComponentAuthoring));
2021-01-18 22:59:57 +01:00
}
}
2022-12-04 15:47:07 +01:00
if (!child.TryGetComponent(out VA_AnimationDataComponentAuthoring ad))
{
child.AddComponent<VA_AnimationDataComponentAuthoring>();
}
2021-01-18 22:59:57 +01:00
if (child.TryGetComponent(out MeshFilter mf))
{
mf.sharedMesh = meshes[i];
}
if (child.TryGetComponent(out MeshRenderer mr))
{
mr.sharedMaterial = material;
}
child.transform.SetParent(parent.transform);
2022-12-04 15:47:07 +01:00
lods[i] = new LOD(lodTransitions[i], new Renderer[1] { mr });
}
2022-12-04 15:47:07 +01:00
var lodGroup = parent.GetComponent<LODGroup>();
lodGroup.SetLODs(lods);
lodGroup.RecalculateBounds();
// Create prefab.
GameObject prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(parent, path, InteractionMode.AutomatedAction);
2022-12-04 04:18:35 +01:00
//GameObject.DestroyImmediate(parent);
return prefab;
}
public static GameObject Create(string path, string name, Mesh[] meshes, Material material, AnimationCurve lodTransitions)
{
float[] lt = new float[meshes.Length];
for (int i = 0; i < lt.Length; i++)
{
lt[i] = lodTransitions.Evaluate((1.0f / lt.Length) * (i + 1));
}
return Create(path, name, meshes, material, lt);
}
}
}