유니티/기능
유니티 편집모드에서 오브젝트 이름 바꾸기
MJ_119
2024. 9. 4. 01:13
using UnityEngine;
using TMPro;
[ExecuteAlways]
public class Coordinate : MonoBehaviour
{
TextMeshPro label;
Vector2Int coordinate = new Vector2Int();
void Awake()
{
label = GetComponent<TextMeshPro>();
}
void Update()
{
// 유니티에서 플레이 화면말고 편집화면일때 아래 구문 실행
if( !Application.isPlaying )
{
coordinate.x = Mathf.RoundToInt(transform.parent.position.x / UnityEditor.EditorSnapSettings.move.x);
coordinate.y = Mathf.RoundToInt(transform.parent.position.z / UnityEditor.EditorSnapSettings.move.z);
label.text = coordinate.x + "," + coordinate.y;
UpdateObjectName();
}
}
void UpdateObjectName()
{
transform.parent.name = coordinate.ToString();
}
}
[ExecuteAlways]
- 이 속성은 스크립트가 플레이 모드와 편집 모드 모두에서 실행되도록 만듭니다. 즉, Unity 에디터에서 오브젝트가 선택되거나 변경될 때마다 이 스크립트가 실행됩니다.