253 lines
6.0 KiB
C#
253 lines
6.0 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using BitStrap;
|
|
using UnityEngine;
|
|
|
|
public class FishBuildHelper : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public struct stFishPart
|
|
{
|
|
public string name;
|
|
|
|
public Transform transform;
|
|
}
|
|
|
|
[Tooltip("parts: jaw, fin.l, fin.r, fin_bootom.l,fin_bootom.l, tail")]
|
|
public Transform[] fishParts;
|
|
|
|
public Vector3[] rotations;
|
|
|
|
public Vector3[] diferent;
|
|
|
|
public bool update;
|
|
|
|
private int lastNumParts;
|
|
|
|
public stFishPart[] fishParts2;
|
|
|
|
public FishAnimSwim[] fishAnims;
|
|
|
|
[Button]
|
|
public void GetPartsFromName(Transform t = null)
|
|
{
|
|
Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
|
|
if ((bool)t)
|
|
{
|
|
componentsInChildren = t.GetComponentsInChildren<Transform>();
|
|
}
|
|
for (int i = 0; i < fishParts2.Length; i++)
|
|
{
|
|
fishParts2[i].transform = null;
|
|
Transform[] array = componentsInChildren;
|
|
foreach (Transform transform in array)
|
|
{
|
|
if (transform.name == fishParts2[i].name)
|
|
{
|
|
fishParts2[i].transform = transform;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void CopyToDFromC()
|
|
{
|
|
for (int i = 0; i < fishParts2.Length; i++)
|
|
{
|
|
fishParts2[i].transform = null;
|
|
}
|
|
Animator[] componentsInChildren = GetComponentsInChildren<Animator>();
|
|
int num = -1;
|
|
int num2 = -1;
|
|
for (int j = 0; j < componentsInChildren.Length; j++)
|
|
{
|
|
if (componentsInChildren[j].name.IndexOf("_C") > 0)
|
|
{
|
|
num = j;
|
|
break;
|
|
}
|
|
}
|
|
for (int k = 0; k < componentsInChildren.Length; k++)
|
|
{
|
|
if (componentsInChildren[k].name.IndexOf("_D") > 0)
|
|
{
|
|
num2 = k;
|
|
break;
|
|
}
|
|
}
|
|
if (num == -1 || num2 == -1)
|
|
{
|
|
return;
|
|
}
|
|
GetPartsFromName(componentsInChildren[num2].transform);
|
|
componentsInChildren[num2].transform.position = componentsInChildren[num].transform.position;
|
|
componentsInChildren[num2].transform.rotation = componentsInChildren[num].transform.rotation;
|
|
componentsInChildren[num2].transform.localScale = componentsInChildren[num].transform.localScale;
|
|
FishPartAnim[] componentsInChildren2 = componentsInChildren[num].GetComponentsInChildren<FishPartAnim>();
|
|
Transform[] componentsInChildren3 = componentsInChildren[num2].GetComponentsInChildren<Transform>();
|
|
FishPartAnim[] array = componentsInChildren2;
|
|
foreach (FishPartAnim fishPartAnim in array)
|
|
{
|
|
for (int m = 0; m < componentsInChildren3.Length; m++)
|
|
{
|
|
if (fishPartAnim.name == componentsInChildren3[m].name)
|
|
{
|
|
CopyComponent(fishPartAnim, componentsInChildren3[m].gameObject);
|
|
SetOnFishPart2(componentsInChildren3[m]);
|
|
}
|
|
}
|
|
}
|
|
Fish component = GetComponent<Fish>();
|
|
Transform[] array2 = componentsInChildren3;
|
|
foreach (Transform transform in array2)
|
|
{
|
|
if (component.mouth.parent.name == transform.name)
|
|
{
|
|
component.mouth.parent = transform;
|
|
break;
|
|
}
|
|
}
|
|
FishAnimationController component2 = GetComponent<FishAnimationController>();
|
|
Transform[] array3 = componentsInChildren3;
|
|
foreach (Transform transform2 in array3)
|
|
{
|
|
if (component2.mouth.name == transform2.name)
|
|
{
|
|
component2.mouth = transform2.GetComponent<FishPartAnim>();
|
|
break;
|
|
}
|
|
}
|
|
SetTransforms();
|
|
}
|
|
|
|
private void SetOnFishPart2(Transform t)
|
|
{
|
|
for (int i = 0; i < fishParts2.Length; i++)
|
|
{
|
|
if (fishParts2[i].name == t.name)
|
|
{
|
|
fishParts2[i].transform = t;
|
|
}
|
|
}
|
|
}
|
|
|
|
private T CopyComponent<T>(T original, GameObject destination) where T : Component
|
|
{
|
|
Type type = original.GetType();
|
|
Component component = destination.GetComponent(type);
|
|
if (component == null)
|
|
{
|
|
component = destination.AddComponent(type);
|
|
}
|
|
FieldInfo[] fields = type.GetFields();
|
|
FieldInfo[] array = fields;
|
|
foreach (FieldInfo fieldInfo in array)
|
|
{
|
|
fieldInfo.SetValue(component, fieldInfo.GetValue(original));
|
|
}
|
|
return component as T;
|
|
}
|
|
|
|
[Button]
|
|
public void SetTransforms()
|
|
{
|
|
for (int i = 0; i < fishAnims.Length; i++)
|
|
{
|
|
for (int j = 0; j < 7; j++)
|
|
{
|
|
fishAnims[i].animParts[j].target = fishParts2[j].transform;
|
|
}
|
|
}
|
|
fishParts = new Transform[6];
|
|
fishParts[0] = fishParts2[7].transform;
|
|
fishParts[1] = fishParts2[8].transform;
|
|
fishParts[2] = fishParts2[9].transform;
|
|
fishParts[3] = fishParts2[10].transform;
|
|
fishParts[4] = fishParts2[11].transform;
|
|
fishParts[5] = fishParts2[6].transform;
|
|
}
|
|
|
|
[Button]
|
|
public void GetNameFromParts()
|
|
{
|
|
}
|
|
|
|
[Button]
|
|
public void AddAnimComponent()
|
|
{
|
|
if (fishParts == null || fishParts.Length == 0 || fishParts.Length == lastNumParts)
|
|
{
|
|
return;
|
|
}
|
|
int num = 0;
|
|
if (rotations != null)
|
|
{
|
|
num = rotations.Length;
|
|
}
|
|
Vector3[] array = new Vector3[fishParts.Length];
|
|
diferent = new Vector3[fishParts.Length];
|
|
int num2 = 0;
|
|
Transform[] array2 = fishParts;
|
|
foreach (Transform transform in array2)
|
|
{
|
|
FishPartAnim component = transform.GetComponent<FishPartAnim>();
|
|
if (!component)
|
|
{
|
|
component = transform.gameObject.AddComponent<FishPartAnim>();
|
|
component.enabled = false;
|
|
component.updateInEditor = true;
|
|
component.startRotation = transform.transform.localRotation.eulerAngles;
|
|
component.finishRotation = transform.transform.localRotation.eulerAngles;
|
|
}
|
|
if (num2 < num)
|
|
{
|
|
array[num2] = rotations[num2];
|
|
}
|
|
else
|
|
{
|
|
array[num2] = transform.transform.localRotation.eulerAngles;
|
|
}
|
|
num2++;
|
|
}
|
|
rotations = array;
|
|
FishAnimationController component2 = GetComponent<FishAnimationController>();
|
|
if ((bool)component2)
|
|
{
|
|
component2.mouth = fishParts2[7].transform.GetComponent<FishPartAnim>();
|
|
}
|
|
}
|
|
|
|
public void UpdateParts()
|
|
{
|
|
if (rotations == null || fishParts == null || rotations.Length != fishParts.Length)
|
|
{
|
|
return;
|
|
}
|
|
for (int i = 0; i < fishParts.Length; i++)
|
|
{
|
|
FishPartAnim component = fishParts[i].GetComponent<FishPartAnim>();
|
|
if ((bool)component)
|
|
{
|
|
component.enabled = update;
|
|
if (rotations[i] != component.finishRotation)
|
|
{
|
|
component.finishRotation = rotations[i];
|
|
component.transform.rotation = Quaternion.Euler(rotations[i]);
|
|
diferent[i] = component.finishRotation - component.startRotation;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
UpdateParts();
|
|
}
|
|
}
|