Unity
Undo 기능 만들기
Guk-blog
2019. 8. 21. 09:36
728x90
반응형
복잡한 코드는 빡대가리인 나는 모르기에 심플한 코드들로 짜봤슴당!
Undo 저장 함수
public static List<GameObject> UndoList = new List<GameObject>();
void UndoSave()
{
if (!m_CurrentObj || !m_CurrentObj.Equals(CanvasController.m_GrabObject))
{//처음 저장하거나 같은 오브젝트 컨트롤하는 것이 아니라면
if (UndoList.Count != 0) UndoList.Clear();
m_CurrentObj = CanvasController.m_GrabObject;
m_firstUndo = false;
}
GameObject g = new GameObject();
g.name = "Undo_" + UndoList.Count;
UndoList.Add(g);
g.transform.parent = Undo_Parent.transform;
g.transform.position = CanvasController.m_GrabObject.transform.position;
g.transform.rotation = CanvasController.m_GrabObject.transform.rotation;
g.transform.localScale = CanvasController.m_GrabObject.transform.lossyScale;
}
Undo 실행 함수
저는 오브젝트를 드래그해서 옮기거나 확대, 회전을 하게 되면 위 함수가 실행되서 언도를 저장하게 만들었습니다.
입맛대로 만드셔서 사용하시면 될 듯합니당
public void UndoAct()
{
if (m_CurrentObj)//스크립트로 넘겨받은 인스턴스 오브젝트 온오프
{
if (UndoList.Count < 3)
{
#if UNITY_EDITOR
Debug.Log("Undo : Can't Undo");
#endif
return;
}
else
{
if (!m_firstUndo)//마지막 Undo는 현재 오브젝트의 위치임으로 삭제 후 그 전 위치값으로 이동
{ // 처음 언도 실행
temp = UndoList[UndoList.Count - 1];
UndoList.RemoveAt(UndoList.Count - 1);
Destroy(temp);
m_firstUndo = true;
}
#if UNITY_EDITOR
Debug.Log("Undo : Count : " + UndoList.Count);
Debug.Log(m_CurrentObj.transform.position + "\n" + UndoList[UndoList.Count - 1].transform.position);
#endif
m_CurrentObj.transform.position = UndoList[UndoList.Count - 1].transform.position;
m_CurrentObj.transform.rotation = UndoList[UndoList.Count - 1].transform.rotation;
m_CurrentObj.transform.localScale = UndoList[UndoList.Count - 1].transform.lossyScale;
temp = UndoList[UndoList.Count - 1];
UndoList.RemoveAt(UndoList.Count - 1);
Destroy(temp);
return;
}
}
}
처음 언도가 실행 하면 마지막 언도는 현재 위치와 같기 때문에 삭제해주고 그 전 언도의 위치와 회전, 크기 값을 가져와
오브젝트에 적용시켜줍니다.(지금 생각한 건데 저장시기를 옮기려고 할 때 저장해주면 첫 언도 삭제 기능은 빼도 되겠네요 전 귀찮아서 안할거임 키득)
728x90
반응형