223 lines
6.5 KiB
C#
223 lines
6.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Artngame.SKYMASTER
|
|
{
|
|
public class ControlCombineChildrenSKYMASTER : MonoBehaviour
|
|
{
|
|
public bool generateTriangleStrips = true;
|
|
|
|
public bool Auto_Disable;
|
|
|
|
public int skip_every_N_frame;
|
|
|
|
public bool MakeActive;
|
|
|
|
private List<GameObject> Destroy_list;
|
|
|
|
private int count_frames;
|
|
|
|
private List<Vector3> Positions_list;
|
|
|
|
private List<Quaternion> Rotations_list;
|
|
|
|
private List<Vector3> Scale_list;
|
|
|
|
private List<Transform> Children_list;
|
|
|
|
public bool Self_dynamic_enable;
|
|
|
|
public bool Self_dynamic_check_rot;
|
|
|
|
public bool Self_dynamic_check_scale;
|
|
|
|
private void Start()
|
|
{
|
|
if (Destroy_list == null)
|
|
{
|
|
Destroy_list = new List<GameObject>();
|
|
}
|
|
Component[] componentsInChildren = GetComponentsInChildren(typeof(MeshFilter));
|
|
if (Self_dynamic_enable && Children_list != null && componentsInChildren.Length != Children_list.Count)
|
|
{
|
|
Children_list.Clear();
|
|
Positions_list.Clear();
|
|
if (Self_dynamic_check_rot)
|
|
{
|
|
Rotations_list.Clear();
|
|
}
|
|
if (Self_dynamic_check_scale)
|
|
{
|
|
Scale_list.Clear();
|
|
}
|
|
}
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
Renderer component = componentsInChildren[i].GetComponent<Renderer>();
|
|
if (Self_dynamic_enable && Children_list != null && componentsInChildren.Length != Children_list.Count)
|
|
{
|
|
Children_list.Add(componentsInChildren[i].gameObject.transform);
|
|
Positions_list.Add(componentsInChildren[i].gameObject.transform.position);
|
|
if (Self_dynamic_check_rot)
|
|
{
|
|
Rotations_list.Add(componentsInChildren[i].gameObject.transform.rotation);
|
|
}
|
|
if (Self_dynamic_check_scale)
|
|
{
|
|
Scale_list.Add(componentsInChildren[i].gameObject.transform.localScale);
|
|
}
|
|
}
|
|
if (component != null && !component.enabled)
|
|
{
|
|
component.enabled = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (Self_dynamic_enable)
|
|
{
|
|
if (Children_list != null)
|
|
{
|
|
for (int num = Children_list.Count - 1; num >= 0; num--)
|
|
{
|
|
if (Children_list[num] != null && Children_list[num].gameObject == null)
|
|
{
|
|
Children_list.RemoveAt(num);
|
|
Positions_list.RemoveAt(num);
|
|
}
|
|
}
|
|
for (int num2 = Children_list.Count - 1; num2 >= 0; num2--)
|
|
{
|
|
if (Children_list[num2] != null)
|
|
{
|
|
if (Children_list[num2].position != Positions_list[num2])
|
|
{
|
|
MakeActive = true;
|
|
Positions_list[num2] = Children_list[num2].position;
|
|
}
|
|
if (Self_dynamic_check_rot && Rotations_list[num2] != Children_list[num2].rotation)
|
|
{
|
|
MakeActive = true;
|
|
Rotations_list[num2] = Children_list[num2].rotation;
|
|
}
|
|
if (Self_dynamic_check_scale && Scale_list[num2] != Children_list[num2].localScale)
|
|
{
|
|
MakeActive = true;
|
|
Scale_list[num2] = Children_list[num2].localScale;
|
|
}
|
|
}
|
|
}
|
|
if (base.transform.childCount != Children_list.Count)
|
|
{
|
|
MakeActive = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Children_list = new List<Transform>();
|
|
Positions_list = new List<Vector3>();
|
|
if (Self_dynamic_check_rot)
|
|
{
|
|
Rotations_list = new List<Quaternion>();
|
|
}
|
|
if (Self_dynamic_check_scale)
|
|
{
|
|
Scale_list = new List<Vector3>();
|
|
}
|
|
}
|
|
}
|
|
if (!MakeActive)
|
|
{
|
|
return;
|
|
}
|
|
if (Auto_Disable)
|
|
{
|
|
MakeActive = false;
|
|
}
|
|
if (skip_every_N_frame > 0)
|
|
{
|
|
if (count_frames >= skip_every_N_frame)
|
|
{
|
|
count_frames = 0;
|
|
return;
|
|
}
|
|
count_frames++;
|
|
}
|
|
Start();
|
|
MeshFilter meshFilter = base.gameObject.GetComponent(typeof(MeshFilter)) as MeshFilter;
|
|
if (meshFilter != null)
|
|
{
|
|
UnityEngine.Object.DestroyImmediate(meshFilter.sharedMesh, allowDestroyingAssets: true);
|
|
UnityEngine.Object.DestroyImmediate(meshFilter, allowDestroyingAssets: true);
|
|
}
|
|
else if (Destroy_list.Count > 0)
|
|
{
|
|
for (int i = 0; i < Destroy_list.Count; i++)
|
|
{
|
|
MeshFilter meshFilter2 = Destroy_list[i].GetComponent(typeof(MeshFilter)) as MeshFilter;
|
|
if (meshFilter2 != null)
|
|
{
|
|
UnityEngine.Object.DestroyImmediate(meshFilter2.sharedMesh, allowDestroyingAssets: true);
|
|
UnityEngine.Object.DestroyImmediate(meshFilter2, allowDestroyingAssets: true);
|
|
}
|
|
}
|
|
for (int num3 = Destroy_list.Count - 1; num3 >= 0; num3--)
|
|
{
|
|
UnityEngine.Object.DestroyImmediate(Destroy_list[num3]);
|
|
Destroy_list.RemoveAt(num3);
|
|
}
|
|
}
|
|
Component[] componentsInChildren = GetComponentsInChildren(typeof(MeshFilter));
|
|
Matrix4x4 worldToLocalMatrix = base.transform.worldToLocalMatrix;
|
|
Hashtable hashtable = new Hashtable();
|
|
for (int j = 0; j < componentsInChildren.Length; j++)
|
|
{
|
|
MeshFilter meshFilter3 = (MeshFilter)componentsInChildren[j];
|
|
Renderer component = componentsInChildren[j].GetComponent<Renderer>();
|
|
MeshCombineUtilitySKYMASTER.MeshInstance meshInstance = new MeshCombineUtilitySKYMASTER.MeshInstance
|
|
{
|
|
mesh = meshFilter3.sharedMesh
|
|
};
|
|
if (!(component != null) || !component.enabled || !(meshInstance.mesh != null))
|
|
{
|
|
continue;
|
|
}
|
|
meshInstance.transform = worldToLocalMatrix * meshFilter3.transform.localToWorldMatrix;
|
|
Material[] sharedMaterials = component.sharedMaterials;
|
|
for (int k = 0; k < sharedMaterials.Length; k++)
|
|
{
|
|
meshInstance.subMeshIndex = Math.Min(k, meshInstance.mesh.subMeshCount - 1);
|
|
ArrayList arrayList = (ArrayList)hashtable[sharedMaterials[k]];
|
|
if (arrayList != null)
|
|
{
|
|
arrayList.Add(meshInstance);
|
|
continue;
|
|
}
|
|
arrayList = new ArrayList();
|
|
arrayList.Add(meshInstance);
|
|
hashtable.Add(sharedMaterials[k], arrayList);
|
|
}
|
|
component.enabled = false;
|
|
}
|
|
foreach (DictionaryEntry item in hashtable)
|
|
{
|
|
MeshCombineUtilitySKYMASTER.MeshInstance[] combines = (MeshCombineUtilitySKYMASTER.MeshInstance[])((ArrayList)item.Value).ToArray(typeof(MeshCombineUtilitySKYMASTER.MeshInstance));
|
|
GameObject gameObject = new GameObject("Combined mesh");
|
|
gameObject.transform.parent = base.transform;
|
|
gameObject.transform.localScale = Vector3.one;
|
|
gameObject.transform.localRotation = Quaternion.identity;
|
|
gameObject.transform.localPosition = Vector3.zero;
|
|
gameObject.AddComponent(typeof(MeshFilter));
|
|
gameObject.AddComponent<MeshRenderer>();
|
|
gameObject.GetComponent<Renderer>().material = (Material)item.Key;
|
|
((MeshFilter)gameObject.GetComponent(typeof(MeshFilter))).mesh = MeshCombineUtilitySKYMASTER.Combine(combines, generateTriangleStrips);
|
|
Destroy_list.Add(gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|