using UnityEditor;
using UnityEngine;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Reflection;
public class CreateCableFromElements
{
[MenuItem("Macros/CreateCableFromElements")]
public static void CreateCable()
{
// выделенный в редакторе объект
GameObject GO = (GameObject)(Selection.objects[0]);
// количество потомков выделенного объекта
int GO_child_count = GO.transform.childCount;
// нахождение необходимого физического материала из готовых наборов
UnityEngine.Object[] PMs = GameObject.FindObjectsOfTypeIncludingAssets(typeof(PhysicMaterial));
PhysicMaterial cablePM = null;
for (int i = 0; i < PMs.Length; i++)
if (((PhysicMaterial)(PMs[i])).name == "CableMaterial")
{
cablePM = (PhysicMaterial)(PMs[i]);
break;
}
// конфигурирование объектов-потомков (создание компонент)
for (int i = 0; i < GO_child_count; i++)
{
GameObject GO_child = GO.transform.GetChild(i).gameObject; // текущий потомок
SphereCollider compSC = (SphereCollider)(GO_child.AddComponent("SphereCollider"));
compSC.radius = 0.03f;
compSC.material = cablePM;
ConfigurableJoint compCJ = (ConfigurableJoint)(GO_child.AddComponent("ConfigurableJoint"));
compCJ.anchor = new Vector3(0f, 0f, 0f);
compCJ.axis = new Vector3(1f, 0f, 0f);
compCJ.angularXMotion = ConfigurableJointMotion.Free;
compCJ.angularYMotion = ConfigurableJointMotion.Locked;
compCJ.angularZMotion = ConfigurableJointMotion.Free;
compCJ.xMotion = ConfigurableJointMotion.Locked;
compCJ.yMotion = ConfigurableJointMotion.Locked;
compCJ.zMotion = ConfigurableJointMotion.Locked;
compCJ.projectionMode = JointProjectionMode.PositionAndRotation;
compCJ.projectionDistance = 0f;
compCJ.projectionAngle = 0f;
compCJ.configuredInWorldSpace = true;
Rigidbody compRB = (Rigidbody)(GO_child.GetComponent("Rigidbody"));
compRB.useGravity = true;
compRB.mass = 5f;
compRB.drag = 1f;
compRB.angularDrag = 2f;
}
// конфигурирование объектов-потомков (создание связей)
Rigidbody compRB_prev = (Rigidbody)(GO.transform.GetChild(0).gameObject.GetComponent("Rigidbody"));
for (int i = 1; i < GO_child_count; i++)
{
GameObject GO_child = GO.transform.GetChild(i).gameObject; // текущий потомок
ConfigurableJoint compCJ = (ConfigurableJoint)(GO_child.GetComponent("ConfigurableJoint"));
compCJ.connectedBody = compRB_prev;
compRB_prev = (Rigidbody)(GO_child.GetComponent("Rigidbody"));
}
}
[MenuItem("Macros/DestroyCableProperties")]
public static void DestroyCable()
{
// выделенный в редакторе объект
GameObject GO = (GameObject)(Selection.objects[0]);
// количество потомков выделенного объекта
int GO_child_count = GO.transform.childCount;
// удаление связей в точках соединений
for (int i = 0; i < GO_child_count; i++)
{
GameObject GO_child = GO.transform.GetChild(i).gameObject; // текущий потомок
((ConfigurableJoint)(GO_child.GetComponent("ConfigurableJoint"))).connectedBody = null;
}
// удаление компонент
for (int i = 0; i < GO_child_count; i++)
{
GameObject GO_child = GO.transform.GetChild(i).gameObject; // текущий потомок
GameObject.DestroyImmediate(GO_child.GetComponent("SphereCollider"));
GameObject.DestroyImmediate(GO_child.GetComponent("ConfigurableJoint"));
GameObject.DestroyImmediate(GO_child.GetComponent("Rigidbody"));
}
}
}
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 2