using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetVertX3 : MonoBehaviour
{
public GameObject[] someObjects;
public Vector3 thePosition;
List<GameObject> pin = new List<GameObject>();
void Start()
{
MeshGenerator();
}
void MeshGenerator()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
//점의 좌표를 가져옴
Vector3[] vertices = mesh.vertices;
int v = 0;
while (v < vertices.Length)
{
thePosition = transform.TransformPoint(vertices[v]);
//vector3 방향 !
Quaternion rot = Quaternion.LookRotation(transform.position - thePosition);
//someObject는 랜덤하게 생성
var someObject = someObjects[Random.Range(0, someObjects.Length)];
//quad는 점의 위치에 중심을 바라보고 someObject를 생성시킨 gameObject
GameObject quad = Instantiate(someObject, thePosition, rot);
//새롭게 생성된 quad를 현재 gameObject에 차일드 시킴 (하위항목으로)
quad.transform.SetParent(transform);
//pin이라는 리스트에 quad 추가함
pin.Add(quad);
v++;
}
foreach(GameObject tmp in pin)
{
try// 예외처리
{
//추가된 someobject인 quad가 pin의 리스트 그 pin 중 하나의 gameobject가 tmp
//여기에서 tmp의 하위항목을 가져오는 것은 someobject의 animation이 빈게임 오브젝트
//하위항목에 있기 때문 일반적인 경우에는 getchild를 할 필요 없음
Animation anim = tmp.transform.GetChild(0).GetComponent<Animation>();
//anim길이 받아옴
float p = anim["anim"].length;
//anim 길이까지 랜덤하게 키 생성
float t = Random.Range(0, p);
//anim 시간을 랜덤 키 위치에 생성 (시작키)
anim["anim"].time = t;
anim.Play();
} catch{
Debug.Log("no animation");
}
}
}
}