405 lines
14 KiB
C#
405 lines
14 KiB
C#
using UnityEngine;
|
|
|
|
public class MegaCopyObject
|
|
{
|
|
private static GameObject CopyMesh(GameObject subject)
|
|
{
|
|
GameObject gameObject = Object.Instantiate(subject);
|
|
MeshFilter[] componentsInChildren = subject.GetComponentsInChildren<MeshFilter>();
|
|
MeshFilter[] componentsInChildren2 = gameObject.GetComponentsInChildren<MeshFilter>();
|
|
MeshCollider[] componentsInChildren3 = gameObject.GetComponentsInChildren<MeshCollider>();
|
|
MeshCollider[] componentsInChildren4 = gameObject.GetComponentsInChildren<MeshCollider>();
|
|
int num = componentsInChildren.Length;
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
MeshFilter meshFilter = componentsInChildren[i];
|
|
MeshFilter meshFilter2 = componentsInChildren2[i];
|
|
Mesh sharedMesh = meshFilter.sharedMesh;
|
|
Mesh mesh = new Mesh();
|
|
mesh.vertices = sharedMesh.vertices;
|
|
mesh.uv2 = sharedMesh.uv2;
|
|
mesh.uv2 = sharedMesh.uv2;
|
|
mesh.uv = sharedMesh.uv;
|
|
mesh.normals = sharedMesh.normals;
|
|
mesh.tangents = sharedMesh.tangents;
|
|
mesh.colors = sharedMesh.colors;
|
|
mesh.subMeshCount = sharedMesh.subMeshCount;
|
|
for (int j = 0; j < sharedMesh.subMeshCount; j++)
|
|
{
|
|
mesh.SetTriangles(sharedMesh.GetTriangles(j), j);
|
|
}
|
|
mesh.boneWeights = sharedMesh.boneWeights;
|
|
mesh.bindposes = sharedMesh.bindposes;
|
|
mesh.name = sharedMesh.name + "_copy";
|
|
mesh.RecalculateBounds();
|
|
meshFilter2.sharedMesh = mesh;
|
|
for (int k = 0; k < componentsInChildren3.Length; k++)
|
|
{
|
|
MeshCollider meshCollider = componentsInChildren3[k];
|
|
if (meshCollider.sharedMesh == sharedMesh)
|
|
{
|
|
componentsInChildren4[k].sharedMesh = mesh;
|
|
}
|
|
}
|
|
}
|
|
return gameObject;
|
|
}
|
|
|
|
private static void CopyBlendShapes(Mesh mesh, Mesh clonemesh)
|
|
{
|
|
}
|
|
|
|
private static GameObject CopyMesh(GameObject subject, MegaModifyObject mod)
|
|
{
|
|
GameObject gameObject = new GameObject();
|
|
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
|
|
SkinnedMeshRenderer component = subject.GetComponent<SkinnedMeshRenderer>();
|
|
SkinnedMeshRenderer skinnedMeshRenderer = null;
|
|
if ((bool)component)
|
|
{
|
|
skinnedMeshRenderer = gameObject.AddComponent<SkinnedMeshRenderer>();
|
|
skinnedMeshRenderer.sharedMaterials = component.sharedMaterials;
|
|
}
|
|
else
|
|
{
|
|
MeshRenderer component2 = subject.GetComponent<MeshRenderer>();
|
|
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
|
|
meshRenderer.sharedMaterials = component2.sharedMaterials;
|
|
}
|
|
MeshFilter[] componentsInChildren = subject.GetComponentsInChildren<MeshFilter>();
|
|
MeshCollider[] componentsInChildren2 = gameObject.GetComponentsInChildren<MeshCollider>();
|
|
MeshCollider[] componentsInChildren3 = gameObject.GetComponentsInChildren<MeshCollider>();
|
|
int num = componentsInChildren.Length;
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
MeshFilter meshFilter2 = componentsInChildren[i];
|
|
Mesh sharedMesh = meshFilter2.sharedMesh;
|
|
Mesh mesh = new Mesh();
|
|
mesh.vertices = mod.verts;
|
|
mesh.uv2 = sharedMesh.uv2;
|
|
mesh.uv2 = sharedMesh.uv2;
|
|
mesh.uv = mod.uvs;
|
|
if (mod.NormalMethod == MegaNormalMethod.Mega && mod.norms != null && mod.norms.Length > 0)
|
|
{
|
|
mesh.normals = mod.norms;
|
|
}
|
|
else
|
|
{
|
|
mesh.normals = sharedMesh.normals;
|
|
}
|
|
mesh.tangents = sharedMesh.tangents;
|
|
mesh.colors = sharedMesh.colors;
|
|
mesh.subMeshCount = sharedMesh.subMeshCount;
|
|
for (int j = 0; j < sharedMesh.subMeshCount; j++)
|
|
{
|
|
mesh.SetTriangles(sharedMesh.GetTriangles(j), j);
|
|
}
|
|
CopyBlendShapes(sharedMesh, mesh);
|
|
mesh.boneWeights = sharedMesh.boneWeights;
|
|
mesh.bindposes = sharedMesh.bindposes;
|
|
mesh.name = sharedMesh.name + "_copy";
|
|
mesh.RecalculateBounds();
|
|
meshFilter.sharedMesh = mesh;
|
|
for (int k = 0; k < componentsInChildren2.Length; k++)
|
|
{
|
|
MeshCollider meshCollider = componentsInChildren2[k];
|
|
if (meshCollider.sharedMesh == sharedMesh)
|
|
{
|
|
componentsInChildren3[k].sharedMesh = mesh;
|
|
}
|
|
}
|
|
if ((bool)skinnedMeshRenderer && (bool)component)
|
|
{
|
|
skinnedMeshRenderer.sharedMesh = mesh;
|
|
}
|
|
}
|
|
return gameObject;
|
|
}
|
|
|
|
private static void CopyModObj(MegaModifyObject from, MegaModifyObject to)
|
|
{
|
|
if ((bool)from && (bool)to)
|
|
{
|
|
to.Enabled = from.Enabled;
|
|
to.recalcbounds = from.recalcbounds;
|
|
to.recalcCollider = from.recalcCollider;
|
|
to.recalcnorms = from.recalcnorms;
|
|
to.UpdateMode = from.UpdateMode;
|
|
to.dynamicMesh = from.dynamicMesh;
|
|
to.DrawGizmos = from.DrawGizmos;
|
|
to.NormalMethod = from.NormalMethod;
|
|
}
|
|
}
|
|
|
|
public static GameObject DoCopyObjects(GameObject from)
|
|
{
|
|
MegaModifyObject component = from.GetComponent<MegaModifyObject>();
|
|
GameObject gameObject = ((!component) ? CopyMesh(from) : CopyMesh(from, component));
|
|
MegaModifyObject megaModifyObject = gameObject.AddComponent<MegaModifyObject>();
|
|
CopyModObj(component, megaModifyObject);
|
|
MegaModifier[] components = from.GetComponents<MegaModifier>();
|
|
for (int i = 0; i < components.Length; i++)
|
|
{
|
|
Component component2 = CopyComponent(components[i], gameObject);
|
|
if ((bool)component2)
|
|
{
|
|
MegaModifier megaModifier = (MegaModifier)component2;
|
|
megaModifier.PostCopy(components[i]);
|
|
}
|
|
}
|
|
MegaWrap component3 = from.GetComponent<MegaWrap>();
|
|
if ((bool)component3)
|
|
{
|
|
CopyComponent(component3, gameObject);
|
|
}
|
|
if ((bool)megaModifyObject)
|
|
{
|
|
megaModifyObject.MeshUpdated();
|
|
}
|
|
gameObject.name = from.name + " - Copy";
|
|
return gameObject;
|
|
}
|
|
|
|
public static GameObject DoCopyObjectsChildren(GameObject from)
|
|
{
|
|
GameObject gameObject = DoCopyObjects(from);
|
|
for (int i = 0; i < from.transform.childCount; i++)
|
|
{
|
|
GameObject gameObject2 = from.transform.GetChild(i).gameObject;
|
|
GameObject gameObject3 = DoCopyObjectsChildren(gameObject2);
|
|
gameObject3.transform.parent = gameObject.transform;
|
|
}
|
|
return gameObject;
|
|
}
|
|
|
|
private static Component CopyComponent(Component from, GameObject to)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public static GameObject DeepCopy(GameObject subject)
|
|
{
|
|
GameObject gameObject = null;
|
|
if ((bool)subject)
|
|
{
|
|
gameObject = Object.Instantiate(subject);
|
|
SkinnedMeshRenderer[] componentsInChildren = subject.GetComponentsInChildren<SkinnedMeshRenderer>();
|
|
SkinnedMeshRenderer[] componentsInChildren2 = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
|
|
int num = componentsInChildren.Length;
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
Mesh sharedMesh = componentsInChildren[i].sharedMesh;
|
|
Mesh mesh = new Mesh();
|
|
mesh.vertices = sharedMesh.vertices;
|
|
mesh.uv2 = sharedMesh.uv2;
|
|
mesh.uv2 = sharedMesh.uv2;
|
|
mesh.uv = sharedMesh.uv;
|
|
mesh.normals = sharedMesh.normals;
|
|
mesh.tangents = sharedMesh.tangents;
|
|
mesh.colors = sharedMesh.colors;
|
|
mesh.subMeshCount = sharedMesh.subMeshCount;
|
|
for (int j = 0; j < sharedMesh.subMeshCount; j++)
|
|
{
|
|
mesh.SetTriangles(sharedMesh.GetTriangles(j), j);
|
|
}
|
|
CopyBlendShapes(sharedMesh, mesh);
|
|
mesh.boneWeights = sharedMesh.boneWeights;
|
|
mesh.bindposes = sharedMesh.bindposes;
|
|
mesh.name = sharedMesh.name + "_copy";
|
|
mesh.RecalculateBounds();
|
|
componentsInChildren2[i].sharedMesh = mesh;
|
|
}
|
|
MeshFilter[] componentsInChildren3 = subject.GetComponentsInChildren<MeshFilter>();
|
|
MeshFilter[] componentsInChildren4 = gameObject.GetComponentsInChildren<MeshFilter>();
|
|
MeshCollider[] componentsInChildren5 = gameObject.GetComponentsInChildren<MeshCollider>();
|
|
MeshCollider[] componentsInChildren6 = gameObject.GetComponentsInChildren<MeshCollider>();
|
|
for (int k = 0; k < componentsInChildren3.Length; k++)
|
|
{
|
|
MeshFilter meshFilter = componentsInChildren3[k];
|
|
MeshFilter meshFilter2 = componentsInChildren4[k];
|
|
Mesh sharedMesh2 = meshFilter.sharedMesh;
|
|
Mesh mesh2 = new Mesh();
|
|
mesh2.vertices = sharedMesh2.vertices;
|
|
mesh2.uv2 = sharedMesh2.uv2;
|
|
mesh2.uv2 = sharedMesh2.uv2;
|
|
mesh2.uv = sharedMesh2.uv;
|
|
mesh2.normals = sharedMesh2.normals;
|
|
mesh2.tangents = sharedMesh2.tangents;
|
|
mesh2.colors = sharedMesh2.colors;
|
|
mesh2.subMeshCount = sharedMesh2.subMeshCount;
|
|
for (int l = 0; l < sharedMesh2.subMeshCount; l++)
|
|
{
|
|
mesh2.SetTriangles(sharedMesh2.GetTriangles(l), l);
|
|
}
|
|
CopyBlendShapes(sharedMesh2, mesh2);
|
|
mesh2.boneWeights = sharedMesh2.boneWeights;
|
|
mesh2.bindposes = sharedMesh2.bindposes;
|
|
mesh2.name = sharedMesh2.name + "_copy";
|
|
mesh2.RecalculateBounds();
|
|
meshFilter2.sharedMesh = mesh2;
|
|
for (int m = 0; m < componentsInChildren5.Length; m++)
|
|
{
|
|
MeshCollider meshCollider = componentsInChildren5[m];
|
|
Mesh mesh3 = (meshCollider.sharedMesh = sharedMesh2);
|
|
if ((bool)mesh3)
|
|
{
|
|
componentsInChildren6[m].sharedMesh = mesh2;
|
|
}
|
|
}
|
|
}
|
|
MegaModifyObject[] componentsInChildren7 = gameObject.GetComponentsInChildren<MegaModifyObject>();
|
|
for (int n = 0; n < componentsInChildren7.Length; n++)
|
|
{
|
|
componentsInChildren7[n].MeshUpdated();
|
|
}
|
|
}
|
|
return gameObject;
|
|
}
|
|
|
|
public static GameObject InstanceObject(GameObject obj)
|
|
{
|
|
GameObject gameObject = null;
|
|
if ((bool)obj)
|
|
{
|
|
MeshFilter component = obj.GetComponent<MeshFilter>();
|
|
MeshRenderer component2 = obj.GetComponent<MeshRenderer>();
|
|
if ((bool)component)
|
|
{
|
|
gameObject = new GameObject();
|
|
gameObject.name = obj.name + " MegaInstance";
|
|
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
|
|
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
|
|
meshFilter.sharedMesh = component.sharedMesh;
|
|
meshRenderer.sharedMaterials = component2.sharedMaterials;
|
|
}
|
|
}
|
|
return gameObject;
|
|
}
|
|
|
|
public static Mesh DupMesh(Mesh mesh)
|
|
{
|
|
Mesh mesh2 = new Mesh();
|
|
mesh2.vertices = mesh.vertices;
|
|
mesh2.uv2 = mesh.uv2;
|
|
mesh2.uv2 = mesh.uv2;
|
|
mesh2.uv = mesh.uv;
|
|
mesh2.normals = mesh.normals;
|
|
mesh2.tangents = mesh.tangents;
|
|
mesh2.colors = mesh.colors;
|
|
mesh2.subMeshCount = mesh.subMeshCount;
|
|
for (int i = 0; i < mesh.subMeshCount; i++)
|
|
{
|
|
mesh2.SetTriangles(mesh.GetTriangles(i), i);
|
|
}
|
|
CopyBlendShapes(mesh, mesh2);
|
|
mesh2.boneWeights = mesh.boneWeights;
|
|
mesh2.bindposes = mesh.bindposes;
|
|
mesh2.name = mesh.name + "_copy";
|
|
mesh2.RecalculateBounds();
|
|
return mesh2;
|
|
}
|
|
|
|
public static Mesh DupMesh(Mesh mesh, string suffix)
|
|
{
|
|
Mesh mesh2 = new Mesh();
|
|
mesh2.vertices = mesh.vertices;
|
|
mesh2.uv2 = mesh.uv2;
|
|
mesh2.uv2 = mesh.uv2;
|
|
mesh2.uv = mesh.uv;
|
|
mesh2.normals = mesh.normals;
|
|
mesh2.tangents = mesh.tangents;
|
|
mesh2.colors = mesh.colors;
|
|
mesh2.subMeshCount = mesh.subMeshCount;
|
|
for (int i = 0; i < mesh.subMeshCount; i++)
|
|
{
|
|
mesh2.SetTriangles(mesh.GetTriangles(i), i);
|
|
}
|
|
CopyBlendShapes(mesh, mesh2);
|
|
mesh2.boneWeights = mesh.boneWeights;
|
|
mesh2.bindposes = mesh.bindposes;
|
|
mesh2.name = mesh.name + suffix;
|
|
mesh2.RecalculateBounds();
|
|
return mesh2;
|
|
}
|
|
|
|
public static GameObject DuplicateObject(GameObject from)
|
|
{
|
|
GameObject gameObject = null;
|
|
if ((bool)from)
|
|
{
|
|
gameObject = Object.Instantiate(from);
|
|
if ((bool)gameObject)
|
|
{
|
|
MeshFilter[] componentsInChildren = gameObject.GetComponentsInChildren<MeshFilter>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].sharedMesh = DupMesh(componentsInChildren[i].sharedMesh);
|
|
}
|
|
SkinnedMeshRenderer[] componentsInChildren2 = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
|
|
for (int j = 0; j < componentsInChildren2.Length; j++)
|
|
{
|
|
componentsInChildren2[j].sharedMesh = DupMesh(componentsInChildren2[j].sharedMesh);
|
|
}
|
|
MegaModifyObject[] componentsInChildren3 = gameObject.GetComponentsInChildren<MegaModifyObject>();
|
|
for (int k = 0; k < componentsInChildren3.Length; k++)
|
|
{
|
|
componentsInChildren3[k].MeshUpdated();
|
|
}
|
|
MegaModifier[] componentsInChildren4 = from.GetComponentsInChildren<MegaModifier>();
|
|
MegaModifier[] componentsInChildren5 = gameObject.GetComponentsInChildren<MegaModifier>();
|
|
for (int l = 0; l < componentsInChildren4.Length; l++)
|
|
{
|
|
componentsInChildren5[l].PostCopy(componentsInChildren4[l]);
|
|
}
|
|
MegaWrap[] componentsInChildren6 = gameObject.GetComponentsInChildren<MegaWrap>();
|
|
for (int m = 0; m < componentsInChildren6.Length; m++)
|
|
{
|
|
componentsInChildren6[m].SetMesh();
|
|
}
|
|
gameObject.name = from.name + " - Copy";
|
|
}
|
|
}
|
|
return gameObject;
|
|
}
|
|
|
|
public static GameObject DuplicateObjectForPrefab(GameObject from)
|
|
{
|
|
GameObject gameObject = null;
|
|
if ((bool)from)
|
|
{
|
|
gameObject = Object.Instantiate(from);
|
|
if ((bool)gameObject)
|
|
{
|
|
MeshFilter[] componentsInChildren = gameObject.GetComponentsInChildren<MeshFilter>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].sharedMesh = DupMesh(componentsInChildren[i].sharedMesh);
|
|
}
|
|
SkinnedMeshRenderer[] componentsInChildren2 = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
|
|
for (int j = 0; j < componentsInChildren2.Length; j++)
|
|
{
|
|
componentsInChildren2[j].sharedMesh = DupMesh(componentsInChildren2[j].sharedMesh);
|
|
}
|
|
MegaModifyObject[] componentsInChildren3 = gameObject.GetComponentsInChildren<MegaModifyObject>();
|
|
for (int k = 0; k < componentsInChildren3.Length; k++)
|
|
{
|
|
componentsInChildren3[k].MeshUpdated();
|
|
}
|
|
MegaModifier[] componentsInChildren4 = from.GetComponentsInChildren<MegaModifier>();
|
|
MegaModifier[] componentsInChildren5 = gameObject.GetComponentsInChildren<MegaModifier>();
|
|
for (int l = 0; l < componentsInChildren4.Length; l++)
|
|
{
|
|
componentsInChildren5[l].PostCopy(componentsInChildren4[l]);
|
|
}
|
|
MegaWrap[] componentsInChildren6 = gameObject.GetComponentsInChildren<MegaWrap>();
|
|
for (int m = 0; m < componentsInChildren6.Length; m++)
|
|
{
|
|
componentsInChildren6[m].SetMesh();
|
|
}
|
|
gameObject.name = from.name;
|
|
}
|
|
}
|
|
return gameObject;
|
|
}
|
|
}
|